Community编程与开发github.com

CI/CD与自动化

自动化CI/CD流水线搭建。在需要设置或修改构建和部署流水线时使用。在需要自动化质量门禁、配置CI中的测试运行器或制定部署策略时使用。

CD与自动化 是什么?

此技能全面指导在AI智能体驱动的开发工作流中建立强大的CI/CD流水线。它强调自动化质量门禁(如lint、类型检查、测试和构建)以确保变更在到达生产环境前经过严格验证。该技能支持GitHub Actions配置,涵盖基本的CI流水线、数据库集成测试、端到端测试及部署策略(如预览部署、功能标记和分阶段发布)。还包含CI失败的反馈循环机制、优化建议(缓存、并行化、按路径过滤)以及与环境管理、依赖更新和安全加固相关的常见模式。对于希望实现可靠、高效且具备AI反馈能力的开发的工程师来说,这是一份全面的参考。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/alfathafp/portofolio-web-design/tree/HEAD/.agents/skills/ci-cd-and-automation

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

CD与自动化 是做什么的?

概述

自动化质量门禁,确保没有变更能在未通过测试、lint、类型检查和构建的情况下进入生产环境。CI/CD是其他所有技能的强制执行机制——它能捕捉人类和智能体遗漏的问题,并且在每次变更时一致地执行。

左移: 尽早发现问题。在lint中捕获的bug只需几分钟;同样的bug在生产环境中捕获则需要数小时。将检查向上游移动——静态分析在测试之前,测试在预发布环境之前,预发布环境在生产环境之前。

更快更安全: 较小的批次和更频繁的发布能降低风险,而不是增加风险。包含3个变更的部署比包含30个变更的部署更容易调试。频繁发布能增强对发布过程本身的信心。

何时使用

  • 设置新项目的CI流水线
  • 添加或修改自动化检查
  • 配置部署流水线
  • 当某个变更应触发自动验证时
  • 调试CI失败

质量门禁流水线

每次变更在合并前必须通过以下门禁:

拉取请求已打开
    │
    ▼
┌─────────────────┐
│   LINT 检查      │  eslint, prettier
│   ↓ 通过          │
│   类型检查        │  tsc --noEmit
│   ↓ 通过          │
│   单元测试        │  jest/vitest
│   ↓ 通过          │
│   构建            │  npm run build
│   ↓ 通过          │
│   集成测试        │  API/DB 测试
│   ↓ 通过          │
│   E2E(可选)     │  Playwright/Cypress
│   ↓ 通过          │
│   安全审计        │  npm audit
│   ↓ 通过          │
│   包大小检查      │  bundlesize check
└─────────────────┘
    │
    ▼
  准备就绪,等待审核

任何门禁都不能跳过。 如果 lint 失败,就修复 lint——不要禁用规则。如果测试失败,就修复代码——不要跳过测试。

GitHub Actions 配置

基础 CI 流水线

# .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: 安装依赖
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: 类型检查
        run: npx tsc --noEmit

      - name: 测试
        run: npm test -- --coverage

      - name: 构建
        run: npm run build

      - name: 安全审计
        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: npx prisma migrate deploy
        env:
          DATABASE_URL: postgresql://ci_user:${{ secrets.CI_DB_PASSWORD }}@localhost:5432/testdb
      - name: 集成测试
        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: 安装 Playwright
        run: npx playwright install --with-deps chromium
      - name: 构建
        run: npm run build
      - name: 运行 E2E 测试
        run: npx playwright test
      - uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: playwright-report
          path: playwright-report/

将CI失败反馈给智能体

AI 智能体与CI结合的力量在于反馈循环。当CI失败时:

CI 失败
    │
    ▼
复制失败输出
    │
    ▼
将其提供给智能体:
“CI 流水线因以下错误失败:
[粘贴具体错误]
修复此问题并先在本地验证,然后再次推送。”
    │
    ▼
智能体修复 → 推送 → CI 再次运行

关键模式:

Lint 失败 → 智能体运行 `npm run lint --fix` 并提交
类型错误  → 智能体读取错误位置并修复类型
测试失败 → 智能体遵循调试和错误恢复技能
构建错误 → 智能体检查配置和依赖项

部署策略

预览部署

每个PR都应获得一个预览部署,用于手动测试:

# 在PR上部署预览 (Vercel/Netlify等)
deploy-preview:
  runs-on: ubuntu-latest
  if: github.event_name == 'pull_request'
  steps:
    - uses: actions/checkout@v4
    - name: 部署预览
      run: npx vercel --token=${{ secrets.VERCEL_TOKEN }}

功能标记

功能标记将部署与发布解耦。在不完整或有风险的功能后面部署它们,以便:

  • 在不启用功能的情况下交付代码。 尽早合并到main,准备就绪时再启用。
  • 无需重新部署即可回滚。 禁用标记,而不是还原代码。
  • 灰度发布新功能。 先对1%的用户启用,然后10%,最后100%。
  • 运行 A/B 测试。 比较有和没有该功能时的行为。
// 简单的功能标记模式
if (featureFlags.isEnabled('new-checkout-flow', { userId })) {
  return renderNewCheckout();
}
return renderLegacyCheckout();

标记生命周期: 创建 → 启用进行测试 → 灰度发布 → 全面发布 → 移除标记和死代码。长期存在的标记会变成技术债务——在创建标记时设定一个清理日期。

分阶段发布

PR 合并到 main
    │
    ▼
  预发布环境部署(自动)
    │ 手动验证
    ▼
  生产环境部署(手动触发或在预发布环境成功后自动触发)
    │
    ▼
  监控错误(15分钟窗口)
    │
    ├── 检测到错误 → 回滚
    └── 正常 → 完成

回滚计划

每次部署都应该是可逆的:

# 手动回滚工作流
name: 回滚
on:
  workflow_dispatch:
    inputs:
      version:
        description: '要回滚到的版本'
        required: true

jobs:
  rollback:
    runs-on: ubuntu-latest
    steps:
      - name: 回滚部署
        run: |
          # 部署指定的先前版本
          npx vercel rollback ${{ inputs.version }}

环境管理

.env.example       → 提交(开发人员模板)
.env                → 不提交(本地开发)
.env.test           → 提交(测试环境,无真实密钥)
CI 密钥             → 存储在 GitHub Secrets / 保险库中
生产环境密钥        → 存储在部署平台 / 保险库中

CI 绝不应持有生产环境密钥。为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 Cop 的工作是修复或回滚——而不是导致变更的那个人。这可以防止在每个人都在等待别人修复时,失败的构建不断累积。

PR 检查

  • 必需审核: 合并前至少1人批准
  • 必需状态检查: CI 必须通过才能合并
  • 分支保护: 禁止强制推送到 main
  • 自动合并: 如果所有检查通过且获得批准,则自动合并

CI 优化

当流水线耗时超过10分钟时,按影响顺序应用以下策略:

CI 流水线慢?
├── 缓存依赖项
│   └── 使用 actions/cache 或 setup-node 的 cache 选项来缓存 node_modules
├── 并行运行作业
│   └── 将 lint、类型检查、测试、构建拆分为单独的并行作业
├── 仅运行变更的部分
│   └── 使用路径过滤器跳过不相关的作业(例如,对于仅文档的PR跳过E2E)
├── 使用矩阵构建
│   └── 将测试套件分片到多个运行器上
├── 优化测试套件
│   └── 从关键路径中移除缓慢的测试,改为按计划运行
└── 使用更大的运行器
    └── GitHub托管的更大运行器或自托管运行器用于CPU密集型构建

示例:缓存和并行化

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太慢了”优化流水线(见下文 CI 优化),不要跳过它。一个5分钟的流水线可以防止数小时的调试。
“这个变更微不足道,跳过CI”微不足道的变更也会破坏构建。对于微不足道的变更,CI本身也很快。
“测试不稳定,只需重新运行”不稳定的测试掩盖真正的错误并浪费每个人的时间。修复不稳定问题。
“我们以后再加CI”没有CI的项目会累积损坏状态。在第一天就设置好。
“手动测试就足够了”手动测试无法扩展且不可重复。自动化你能自动化的一切。

危险信号

  • 项目中没有CI流水线
  • CI失败被忽略或静默处理
  • 为让流水线通过而在CI中禁用测试
  • 未经预发布环境验证就进行生产环境部署
  • 没有回滚机制
  • 密钥存储在代码或CI配置文件中(而非 secret manager)
  • CI 时间长且未做优化

验证

设置或修改CI后:

  • 所有质量门禁均已就位(lint、类型、测试、构建、审计)
  • 流水线在每次PR和推送到main时运行
  • 失败会阻止合并(已配置分支保护)
  • CI结果反馈回开发循环
  • 密钥存储在密钥管理器中,而非代码中
  • 部署具有回滚机制
  • 流水线在10分钟内完成测试套件

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

alfathafp/portofolio-web-design

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).

alfathafp/portofolio-web-design

Tests in real browsers via Chrome DevTools MCP. Use when building or debugging anything that runs in a browser. Use when you need to inspect the DOM, capture console errors, analyze network requests, profile performance, or verify visual output with real runtime data. Requires the chrome-devtools MCP server to be configured.

alfathafp/portofolio-web-design

Conducts multi-axis code review. Use before merging any change. Use when reviewing code written by yourself, another agent, or a human. Use when you need to assess code quality across multiple dimensions before it enters the main branch. Use when asked to review a diff or a pull request, even when the diff is pasted inline.

alfathafp/portofolio-web-design

Simplifies code for clarity. Use when refactoring code for clarity without changing behavior. Use when code works but is harder to read, maintain, or extend than it should be. Use when reviewing code that has accumulated unnecessary complexity.

alfathafp/portofolio-web-design

Establishes a project's quality bar as a written contract and stops agents quietly lowering it. Interviews the user on which dimensions matter, supplies sane default thresholds when they have no number in mind, records everything in CONSTRAINTS.md, and watches the diff for a weakened bar — new @ts-ignore or eslint-disable suppressions, skipped or deleted tests, assertions stripped out, unimplemented stubs, thresholds edited down. Use when no quality bar is written down, when the user says "set up constraints" or "define our standards", when the user wants dimensions they care about — accessibility, web performance, coverage — set up as enforced constraints, when an agent keeps silencing checks or skipping tests to get to green, when you need a coverage or performance threshold and don't know what number to pick, or when an agent writes more code than anyone will read.

alfathafp/portofolio-web-design

Optimizes agent context setup. Use when starting a new session, when agent output quality degrades, when switching between tasks, or when you need to configure rules files and context for a project.

alfathafp/portofolio-web-design

Guides systematic root-cause debugging. Use when tests fail, builds break, something that worked yesterday broke, behavior doesn't match expectations, or you encounter any unexpected error. Use when you need to figure out what broke and why — a systematic approach to finding and fixing the root cause rather than guessing.

alfathafp/portofolio-web-design

Deploy applications and websites to Vercel. Use when the user requests deployment actions like "deploy my app", "deploy and give me the link", "push this live", or "create a preview deployment".

alfathafp/portofolio-web-design

Manages deprecation and migration. Use when removing old systems, APIs, or features. Use when migrating users from one implementation to another. Use when migrating a database schema in production, such as renaming or dropping a column without downtime (expand/contract). Use when deciding whether to maintain or sunset existing code.

alfathafp/portofolio-web-design

Use when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.

alfathafp/portofolio-web-design

Use when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies

alfathafp/portofolio-web-design

Records decisions and documentation. Use when you need to document an architecture decision (ADR) or the reasoning behind a design choice, when changing public APIs, shipping features, or when you need to record context that future engineers and agents will need to understand the codebase.

alfathafp/portofolio-web-design

Subjects every non-trivial decision to a fresh-context adversarial review before it stands. Use when you want every assumption cross-examined before proceeding, when stress-testing a plan for hidden failure modes, when correctness matters more than speed, when working in unfamiliar code, when stakes are high (production auth, security-sensitive logic, a high-stakes migration, irreversible operations), or any time a confident output would be cheaper to verify now than to debug later.

alfathafp/portofolio-web-design

Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available

alfathafp/portofolio-web-design

Use when implementation is complete, all tests pass, and you need to decide how to integrate the work

alfathafp/portofolio-web-design

Builds production-quality, accessible, responsive user-facing UIs. Use when building or modifying interfaces and pages, creating components, implementing layouts, meeting WCAG accessibility requirements, managing state, or when the output needs to look and feel production-quality rather than AI-generated.

alfathafp/portofolio-web-design

Structures git workflow practices. Use when making any code change. Use when committing, branching, resolving conflicts, splitting uncommitted work in a messy working tree into clean atomic commits, opening or reviewing a pull request (PR), pushing to a remote, or when you need to organize work across multiple parallel streams. Use when cutting a release, choosing a semantic version bump, tagging, or writing a changelog.

alfathafp/portofolio-web-design

Refines raw ideas into sharp, actionable concepts through structured divergent and convergent thinking. Use when an idea is still vague, when you need to stress-test assumptions before committing to a plan, or when you want to expand options before converging on one. Triggers on "ideate", "refine this idea", or "stress-test my plan".

alfathafp/portofolio-web-design

Delivers changes incrementally in thin, verifiable slices. Use when implementing any feature or change that touches more than one file, or when picking up the next task from a plan. Use when rolling a change out behind a feature flag, when you're about to write a large amount of code at once, or when a task feels too big to land in one step.

alfathafp/portofolio-web-design

Guides stable API and interface design. Use when designing APIs, module boundaries, or any public interface. Use when creating REST or GraphQL endpoints, defining type contracts between modules, or establishing boundaries between frontend and backend.

相关技能