Development
CI/CD 和自动化
自动化 CI/CD 流水线设置。在设置或修改构建和部署流水线、自动化质量门控或建立部署策略时使用。
自动化质量门控,确保任何变更在未通过测试、lint、类型检查和构建之前都无法进入生产环境。
左移: 尽早捕获问题。在 lint 阶段发现的 bug 只需要几分钟修复;同样的 bug 在生产环境中发现则需要数小时。
更快更安全: 更小的批次和更频繁的发布会降低风险,而非增加风险。包含 3 个变更的部署比包含 30 个变更的更容易调试。
每个变更在合并前都要经过这些门控:
Pull Request 已创建
│
▼
┌─────────────────┐
│ LINT 检查 │ eslint, prettier
│ ↓ 通过 │
│ 类型检查 │ tsc --noEmit
│ ↓ 通过 │
│ 单元测试 │ jest/vitest
│ ↓ 通过 │
│ 构建 │ npm run build
│ ↓ 通过 │
│ 集成测试 │ API/DB 测试
│ ↓ 通过 │
│ E2E(可选) │ Playwright/Cypress
│ ↓ 通过 │
│ 安全审计 │ npm audit
│ ↓ 通过 │
│ 包大小检查 │ bundlesize check
└─────────────────┘
│
▼
准备就绪,等待审核
不能跳过任何门控。 如果 lint 失败,就修复 lint —— 不要禁用规则。
# .github/workflows/ci.yml
name: CI
on:
pull_request:
branches: [main]
push:
branches: [main]
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Type check
run: npx tsc --noEmit
- name: Test
run: npm test -- --coverage
- name: Build
run: npm run build
- name: Security audit
run: npm audit --audit-level=high
integration:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_DB: testdb
POSTGRES_USER: ci_user
POSTGRES_PASSWORD: ${{ secrets.CI_DB_PASSWORD }}
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'npm'
- run: npm ci
- name: Run migrations
run: npx prisma migrate deploy
env:
DATABASE_URL: postgresql://ci_user:${{ secrets.CI_DB_PASSWORD }}@localhost:5432/testdb
- name: Integration tests
run: npm run test:integration
env:
DATABASE_URL: postgresql://ci_user:${{ secrets.CI_DB_PASSWORD }}@localhost:5432/testdb
e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
cache: 'npm'
- run: npm ci
- name: Install Playwright
run: npx playwright install --with-deps chromium
- name: Build
run: npm run build
- name: Run E2E tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: failure()
with:
name: playwright-report
path: playwright-report/
当 CI 失败时:
CI 失败
│
▼
复制失败输出
│
▼
将其反馈给 agent:
"CI 流水线因以下错误失败:
[粘贴具体错误]
修复问题并在再次推送前本地验证。"
│
▼
Agent 修复 → 推送 → CI 重新运行
每个 PR 都会获得预览部署,用于手动测试:
deploy-preview:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- uses: actions/checkout@v4
- name: Deploy preview
run: npx vercel --token=${{ secrets.VERCEL_TOKEN }}
功能开关将部署与发布解耦:
// 简单的功能开关模式
if (featureFlags.isEnabled('new-checkout-flow', { userId })) {
return renderNewCheckout();
}
return renderLegacyCheckout();
PR 合并到 main
│
▼
预发布部署(自动)
│ 手动验证
▼
生产部署(手动触发)
│
▼
监控错误(15 分钟窗口)
│
├── 检测到错误 → 回滚
└── 无异常 → 完成
当流水线超过 10 分钟时:
CI 流水线太慢?
├── 缓存依赖
│ └── 使用 actions/cache 或 setup-node 缓存选项
├── 并行运行任务
│ └── 将 lint、类型检查、测试、构建拆分为独立任务
├── 仅运行变更相关的任务
│ └── 使用路径过滤跳过无关任务
├── 使用矩阵构建
│ └── 将测试套件分片到多个 runner
└── 优化测试套件
└── 从关键路径中移除慢测试
设置或修改 CI 后: