第 28 期:Git 协作 — 工作流版本控制与多环境部署

⏱ 预计阅读 7 分钟 更新于 2026/4/9

版本控制的必要性

工作流也是代码——没有版本控制就像裸奔。

graph LR
    subgraph "多环境部署流水线"
        Dev["🟢 Dev 环境
开发调试"] -->|"git push"| Staging["🟡 Staging 环境
集成测试"] Staging -->|"git push"| Prod["🔴 Production
生产上线"] end style Dev fill:#22c55e,stroke:#16a34a,color:#fff style Staging fill:#f59e0b,stroke:#d97706,color:#fff style Prod fill:#ef4444,stroke:#dc2626,color:#fff

1. n8n Source Control 配置

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# n8n 内置 Git 集成配置
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

# 在 n8n Settings → Source Control 中配置:
# 1. Repository URL: [email protected]:your-org/n8n-workflows.git
# 2. Branch: main (或 dev/staging/prod)
# 3. SSH Key: 生成并添加到 GitHub Deploy Keys

# 配置后可以:
# - Push: 将当前工作流推送到 Git 仓库
# - Pull: 从 Git 仓库拉取工作流到 n8n 实例
# - 查看变更差异 (类似 git diff)

2. 环境变量隔离

graph TB
    subgraph "Dev 环境 (.env.dev)"
        D1["API_URL=http://localhost:8080"]
        D2["LLM_MODEL=gpt-4o-mini"]
        D3["LOG_LEVEL=debug"]
    end
    
    subgraph "Prod 环境 (.env.prod)"
        P1["API_URL=https://api.company.com"]
        P2["LLM_MODEL=gpt-4o"]
        P3["LOG_LEVEL=error"]
    end
    
    style D1 fill:#22c55e,stroke:#16a34a,color:#fff
    style P1 fill:#ef4444,stroke:#dc2626,color:#fff
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 在工作流中使用环境变量 (而非硬编码)
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// ❌ 硬编码 (每个环境都要手动改)
const apiUrl = "https://api.company.com";

// ✅ 使用环境变量 (自动适配环境)
const apiUrl = $env.API_URL;          // Dev: localhost, Prod: api.company.com
const model = $env.LLM_MODEL;        // Dev: gpt-4o-mini, Prod: gpt-4o

// 在 docker-compose 中设置:
// environment:
//   - API_URL=${API_URL}              // 从 .env 文件读取

3. 团队协作工作流

sequenceDiagram
    participant Dev1 as 👤 开发者 A
    participant Dev2 as 👤 开发者 B
    participant Git as 📦 Git 仓库
    participant Staging as 🟡 Staging
    participant Prod as 🔴 Production
    
    Dev1->>Git: Push 新工作流 (branch: feature/new-agent)
    Dev2->>Git: Review & Approve PR
    Git->>Staging: Merge → 自动部署到 Staging
    
    Note over Staging: 集成测试通过
    
    Staging->>Prod: 手动 Pull 到生产环境
    Note over Prod: 灰度放量 → 全量上线

4. 工作流命名规范

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 团队协作的命名约定
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// 格式: [域]_[功能]_[版本]
// 示例:
// - crm_lead_scoring_v2        // CRM 域 → 线索评分 → 第 2 版
// - support_ticket_routing     // 客服域 → 工单路由
// - ai_rag_knowledge_query     // AI 域 → RAG 知识检索
// - _sub_email_sender          // 下划线前缀 = 子工作流 (不独立运行)
// - _error_global_handler      // 全局错误处理器

// 标签 (Tags):
// - "production" / "staging" / "deprecated"
// - "ai-agent" / "integration" / "internal"
// - "owner:alice" / "owner:bob"

下一步

Ep 29 将学习 多 Agent 协作架构——Supervisor Agent 管理多个专家 Agent 的分工与协作。