Ci cd and automation
Skill Hayatelin/agent-skills-zh-CN/skills/ci-cd-and-automation
自动化 CI/CD pipeline 的建置。Use when setting up or modifying build and deployment pipelines. Use when you need to automate quality gates, configure test runners in CI, or establish deployment strategies.From its SKILL.md
npx -y skills add Hayatelin/agent-skills-zh-CN --skill ci-cd-and-automationAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
11.1 KB, as published. Nobody here has run it
CI/CD and Automation
概观
把质量关卡自动化,让任何变更都必须通过测试、lint、类型检查与 build 才能进到 production。CI/CD 是其他所有 skill 的执法机制——它会抓到人类与 agent 漏掉的东西,而且对每一次变更都一致地运行。
Shift Left(把问题往左推): 尽量在 pipeline 越早的阶段抓到问题。lint 阶段抓到的 bug 只花几分钟;同一个 bug 到 production 才被抓到就要花好几小时。把检查往上游搬——静态分析先于测试,测试先于 staging,staging 先于 production。
越快越安全: 更小的批量、更频繁的发布会降低风险,而不是提高风险。含 3 个变更的部署比含 30 个的好调试。频繁发布本身也会累积对发布流程的信心。
使用时机
- 为新项目创建 CI pipeline
- 添加或修改自动化检查
- 设置部署 pipeline
- 某个变更应该触发自动化验证时
- 调试 CI 失败
质量关卡 pipeline
每次变更在 merge 前都要通过这些关卡:
Pull Request Opened
│
▼
┌─────────────────┐
│ LINT CHECK │ eslint, prettier
│ ↓ pass │
│ TYPE CHECK │ tsc --noEmit
│ ↓ pass │
│ UNIT TESTS │ jest/vitest
│ ↓ pass │
│ BUILD │ npm run build
│ ↓ pass │
│ INTEGRATION │ API/DB tests
│ ↓ pass │
│ E2E (optional) │ Playwright/Cypress
│ ↓ pass │
│ SECURITY AUDIT │ npm audit
│ ↓ pass │
│ BUNDLE SIZE │ bundlesize check
└─────────────────┘
│
▼
Ready for review
任何关卡都不能跳过。 lint 失败就修 lint——不要把规则关掉。测试失败就修代码——不要把测试跳过。
GitHub Actions 设置
基本 CI pipeline
# .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
注意: 就算只是 CI 专用的测试数据库,帐密也要用 GitHub Secrets,不要写死。这能养成好习惯,也能避免测试帐密不小心被拿去别的地方重用。
E2E 测试
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 失败喂回给 agent
CI 搭配 AI agent 的威力就在于这个回馈循环。当 CI 失败时:
CI fails
│
▼
Copy the failure output
│
▼
Feed it to the agent:
"The CI pipeline failed with this error:
[paste specific error]
Fix the issue and verify locally before pushing again."
│
▼
Agent fixes → pushes → CI runs again
关键模式:
Lint failure → Agent runs `npm run lint --fix` and commits
Type error → Agent reads the error location and fixes the type
Test failure → Agent follows debugging-and-error-recovery skill
Build error → Agent checks config and dependencies
部署策略
Preview 部署
每个 PR 都有一份 preview 部署可供人工测试:
# Deploy preview on PR (Vercel/Netlify/etc.)
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 }}
Feature flag
Feature flag 让“部署”与“发布”解耦。把未完成或高风险的功能藏在 flag 后面部署出去,你就能:
- 把代码出货但先不激活。 早点 merge 进 main,准备好了再激活。
- 不用重新部署就能回退。 关掉 flag,而不是 revert 代码。
- 对新功能做 canary。 先开给 1% 用户,再 10%,再 100%。
- 跑 A/B 测试。 比较有无该功能的行为差异。
// Simple feature flag pattern
if (featureFlags.isEnabled('new-checkout-flow', { userId })) {
return renderNewCheckout();
}
return renderLegacyCheckout();
Flag 的生命周期: 创建 → 开给测试 → canary → 全量推出 → 移除 flag 与死码。永远不死的 flag 会变成技术债——创建时就先订好清除日期。
分阶段推出
PR merged to main
│
▼
Staging deployment (auto)
│ Manual verification
▼
Production deployment (manual trigger or auto after staging)
│
▼
Monitor for errors (15-minute window)
│
├── Errors detected → Rollback
└── Clean → Done
回退计划
每次部署都应该是可逆的:
# Manual rollback workflow
name: Rollback
on:
workflow_dispatch:
inputs:
version:
description: 'Version to rollback to'
required: true
jobs:
rollback:
runs-on: ubuntu-latest
steps:
- name: Rollback deployment
run: |
# 部署指定的先前版本
npx vercel rollback ${{ inputs.version }}
环境管理
.env.example → Committed (template for developers)
.env → NOT committed (local development)
.env.test → Committed (test environment, no real secrets)
CI secrets → Stored in GitHub Secrets / vault
Production secrets → Stored in deployment platform / vault
CI 永远不该持有 production 的机密。CI 测试要用独立的一组机密。
CI 之外的自动化
Dependabot / Renovate
# .github/dependabot.yml
version: 2
updates:
- package-ecosystem: npm
directory: /
schedule:
interval: weekly
open-pull-requests-limit: 5
Build Cop 角色
指派一个人负责维持 CI 绿灯。build 坏掉时,Build Cop 的职责就是修好或 revert——而不是等那个把 build 弄坏的人来处理。这能避免大家都以为别人会修,导致坏掉的 build 一路累积。
PR 检查
- 必要 review: merge 前至少 1 个核准
- 必要状态检查: CI 必须通过才能 merge
- Branch protection: 禁止对 main 做 force-push
- Auto-merge: 所有检查通过且已核准时,自动 merge
CI 优化
当 pipeline 超过 10 分钟时,依照影响力大小依序套用这些策略:
Slow CI pipeline?
├── Cache dependencies
│ └── Use actions/cache or setup-node cache option for node_modules
├── Run jobs in parallel
│ └── Split lint, typecheck, test, build into separate parallel jobs
├── Only run what changed
│ └── Use path filters to skip unrelated jobs (e.g., skip e2e for docs-only PRs)
├── Use matrix builds
│ └── Shard test suites across multiple runners
├── Optimize the test suite
│ └── Remove slow tests from the critical path, run them on a schedule instead
└── Use larger runners
└── GitHub-hosted larger runners or self-hosted for CPU-heavy builds
范例:缓存与平行化
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: '22', cache: 'npm' }
- run: npm ci
- run: npm run lint
typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: '22', cache: 'npm' }
- run: npm ci
- run: npx tsc --noEmit
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with: { node-version: '22', cache: 'npm' }
- run: npm ci
- run: npm test -- --coverage
常见的自我合理化
| 自我合理化 | 现实 |
|---|---|
| “CI 太慢了” | 去优化 pipeline(见下方“CI 优化”),不要跳过它。一条 5 分钟的 pipeline 可以省掉好几小时的调试。 |
| “这变更很小,跳过 CI 吧” | 再小的变更也会把 build 弄坏。而且对小变更来说 CI 本来就跑得快。 |
| “这测试很 flaky,重跑一次就好” | Flaky 测试会掩盖真正的 bug,并浪费所有人的时间。去修好 flakiness。 |
| “CI 之后再加” | 没有 CI 的项目会累积各种坏掉的状态。第一天就把它建好。 |
| “人工测试就够了” | 人工测试无法规模化,也不可重现。能自动化的就自动化。 |
危险信号
- 项目里根本没有 CI pipeline
- CI 失败被忽略或被消音
- 为了让 pipeline 过关而在 CI 里把测试停用
- 没经过 staging 验证就部署到 production
- 没有回退机制
- 机密存在代码或 CI 配置文件里(而不是 secrets manager)
- CI 时间很长却没有任何优化的努力
验证
创建或修改 CI 之后:
- 所有质量关卡都到位(lint、类型、测试、build、audit)
- 每个 PR 与推向 main 的 push 都会跑 pipeline
- 失败会挡住 merge(branch protection 已设置)
- CI 结果会回馈进开发循环
- 机密存放在 secrets manager,而不是代码里
- 部署具备回退机制
- 测试包的 pipeline 可在 10 分钟内跑完