构建CI/CD管道Skill building-ci-pipelines

此技能专注于构建安全高效的CI/CD管道,涵盖供应链安全(SLSA)、单仓库优化、缓存策略和并行化模式,适用于GitHub Actions、GitLab CI、Argo Workflows等平台。用于自动化测试、构建和部署工作流,提升开发效率和安全性,关键词包括CI/CD、DevOps、自动化、安全、效率、管道优化。

CI/CD 0 次安装 0 次浏览 更新于 3/23/2026

名称: 构建CI/CD管道 描述: 构建安全、高效的CI/CD管道,包括供应链安全(SLSA)、单仓库优化、缓存策略和并行化模式,适用于GitHub Actions、GitLab CI和Argo Workflows。用于设置自动化测试、构建或部署工作流时使用。

构建CI管道

目的

CI/CD管道自动化测试、构建和部署软件。此技能提供在GitHub Actions、GitLab CI、Argo Workflows和Jenkins中构建稳健、安全和高效管道的模式。重点领域:供应链安全(SLSA)、单仓库优化、缓存和并行化。

何时使用此技能

在以下情况下调用:

  • 为新项目设置持续集成
  • 实现自动化测试工作流
  • 构建具有安全来源的容器镜像
  • 优化缓慢的CI管道(尤其是单仓库)
  • 实现SLSA供应链安全
  • 配置多平台构建
  • 设置GitOps自动化
  • 从传统CI系统迁移

平台选择

GitHub托管 → GitHub Actions(SLSA原生,10K+动作,OIDC) GitLab托管 → GitLab CI(父-子管道,内置安全) Kubernetes → Argo Workflows(基于DAG,事件驱动) 传统 → Jenkins(可能时迁移)

平台比较

功能 GitHub Actions GitLab CI Argo Jenkins
易用性 ⭐⭐⭐⭐⭐ ⭐⭐⭐⭐ ⭐⭐⭐ ⭐⭐
SLSA 原生 手动 良好 手动
单仓库 良好 优秀 手动 插件

快速启动模式

模式1: 基本CI(Lint → 测试 → 构建)

# GitHub Actions
名称: CI
触发: [push, pull_request]

任务:
  lint:
    运行在: ubuntu-latest
    步骤:
      - 使用: actions/checkout@v4
      - 运行: npm run lint

  test:
    需要: lint
    运行在: ubuntu-latest
    步骤:
      - 使用: actions/checkout@v4
      - 运行: npm test

  build:
    需要: test
    运行在: ubuntu-latest
    步骤:
      - 使用: actions/checkout@v4
      - 运行: npm run build

模式2: 矩阵策略(多平台)

test:
  运行在: ${{ matrix.os }}
  策略:
    矩阵:
      os: [ubuntu-latest, windows-latest, macos-latest]
      node-version: [18, 20, 22]
  步骤:
    - 使用: actions/checkout@v4
    - 使用: actions/setup-node@v4
      参数:
        node-version: ${{ matrix.node-version }}
    - 运行: npm test

9个任务并行(3个操作系统 × 3个版本):5分钟对比45分钟顺序执行。

模式3: 单仓库受影响(Turborepo)

build:
  运行在: ubuntu-latest
  步骤:
    - 使用: actions/checkout@v4
      参数:
        fetch-depth: 0  # 受影响检测所需

    - 使用: actions/setup-node@v4
      参数:
        node-version: 20

    - 名称: 构建受影响
      运行: npx turbo run build --filter='...[origin/main]'
      环境:
        TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
        TURBO_TEAM: ${{ vars.TURBO_TEAM }}

对单仓库减少60-80%的CI时间。

模式4: SLSA级别3来源证明

名称: SLSA构建
触发:
  push:
    标签: ['v*']

权限:
  id-token: write
  contents: read
  packages: write

任务:
  build:
    运行在: ubuntu-latest
    输出:
      digest: ${{ steps.build.outputs.digest }}
    步骤:
      - 使用: actions/checkout@v4
      - 名称: 构建容器
        id: build
        使用: docker/build-push-action@v5
        参数:
          push: true
          标签: ghcr.io/${{ github.repository }}:${{ github.sha }}

  provenance:
    需要: build
    权限:
      id-token: write
      actions: read
      packages: write
    使用: slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@v1.10.0
    参数:
      image: ghcr.io/${{ github.repository }}
      digest: ${{ needs.build.outputs.digest }}
      registry-username: ${{ github.actor }}
    秘密:
      registry-password: ${{ secrets.GITHUB_TOKEN }}

验证:

cosign verify-attestation --type slsaprovenance \
  --certificate-identity-regexp "^https://github.com/slsa-framework" \
  --certificate-oidc-issuer https://token.actions.githubusercontent.com \
  ghcr.io/myorg/myapp@sha256:abcd...

模式5: OIDC联邦(无凭证)

deploy:
  运行在: ubuntu-latest
  权限:
    id-token: write
    contents: read
  步骤:
    - 使用: actions/checkout@v4

    - 名称: 配置AWS凭证
      使用: aws-actions/configure-aws-credentials@v4
      参数:
        role-to-assume: arn:aws:iam::123456789012:role/GitHubActionsRole
        aws-region: us-east-1

    - 名称: 部署
      运行: aws s3 sync ./dist s3://my-bucket

优点:无存储凭证,1小时生命周期,完整审计追踪。

模式6: 安全扫描

security:
  运行在: ubuntu-latest
  步骤:
    - 使用: actions/checkout@v4
      参数:
        fetch-depth: 0

    - 名称: Gitleaks(秘密检测)
      使用: gitleaks/gitleaks-action@v2

    - 名称: Snyk(漏洞扫描)
      使用: snyk/actions/node@master
      环境:
        SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}

    - 名称: SBOM生成
      使用: anchore/sbom-action@v0
      参数:
        format: spdx-json
        output-file: sbom.spdx.json

缓存

自动依赖缓存

- 使用: actions/setup-node@v4
  参数:
    node-version: 20
    cache: 'npm'  # 自动缓存 ~/.npm
- 运行: npm ci

支持:npm, yarn, pnpm, pip, poetry, cargo, go

手动缓存控制

- 使用: actions/cache@v4
  参数:
    path: |
      ~/.cargo/bin
      ~/.cargo/registry
      target/
    key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
    restore-keys: |
      ${{ runner.os }}-cargo-

多层缓存(Nx)

- 名称: Nx Cloud(构建输出)
  运行: npx nx affected -t build
  环境:
    NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}

- 名称: Vite缓存
  使用: actions/cache@v4
  参数:
    path: '**/node_modules/.vite'
    key: vite-${{ hashFiles('package-lock.json') }}

- 名称: TypeScript缓存
  使用: actions/cache@v4
  参数:
    path: '**/tsconfig.tsbuildinfo'
    key: tsc-${{ hashFiles('tsconfig.json') }}

结果:70-90%构建时间减少。

并行化

任务级并行化

任务:
  unit-tests:
    步骤:
      - 运行: npm run test:unit

  integration-tests:
    步骤:
      - 运行: npm run test:integration

  e2e-tests:
    步骤:
      - 运行: npm run test:e2e

所有三个同时运行。

测试分片

test:
  策略:
    矩阵:
      shard: [1, 2, 3, 4]
  步骤:
    - 运行: npm test -- --shard=${{ matrix.shard }}/4

20分钟测试套件 → 5分钟(4倍加速)。

语言示例

Python

test:
  策略:
    矩阵:
      python-version: ['3.10', '3.11', '3.12']
  步骤:
    - 使用: actions/setup-python@v5
      参数:
        python-version: ${{ matrix.python-version }}
    - 运行: pipx install poetry
    - 运行: poetry install
    - 运行: poetry run ruff check .
    - 运行: poetry run mypy .
    - 运行: poetry run pytest --cov

Rust

test:
  策略:
    矩阵:
      os: [ubuntu-latest, windows-latest, macos-latest]
      rust: [stable, nightly]
  步骤:
    - 使用: dtolnay/rust-toolchain@master
      参数:
        toolchain: ${{ matrix.rust }}
        components: rustfmt, clippy
    - 使用: Swatinem/rust-cache@v2
    - 运行: cargo fmt -- --check
    - 运行: cargo clippy -- -D warnings
    - 运行: cargo test

Go

test:
  步骤:
    - 使用: actions/setup-go@v5
      参数:
        go-version: '1.23'
        cache: true
    - 运行: go mod verify
    - 使用: golangci/golangci-lint-action@v4
    - 运行: go test -v -race -coverprofile=coverage.txt ./...

TypeScript

test:
  策略:
    矩阵:
      node-version: [18, 20, 22]
  步骤:
    - 使用: pnpm/action-setup@v3
      参数:
        version: 8
    - 使用: actions/setup-node@v4
      参数:
        node-version: ${{ matrix.node-version }}
        cache: 'pnpm'
    - 运行: pnpm install --frozen-lockfile
    - 运行: pnpm run lint
    - 运行: pnpm run type-check
    - 运行: pnpm test

最佳实践

安全

做:

  • 使用OIDC代替长期凭证
  • 固定动作到提交SHA:actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
  • 限制权限:permissions: { contents: read }
  • 每次提交扫描秘密(Gitleaks)
  • 为发布生成SLSA来源证明

不做:

  • 在日志中暴露秘密
  • 使用pull_request_target而无需验证
  • 信任未验证的第三方动作

性能

做:

  • 对单仓库使用受影响检测
  • 缓存依赖和构建输出
  • 并行化独立任务
  • 快速失败:strategy.fail-fast: true
  • 使用远程缓存(Turborepo/Nx Cloud)

不做:

  • 每次提交都重建所有内容
  • 在PR检查中运行长测试
  • 使用通用缓存键

调试

# 启用调试日志
环境:
  ACTIONS_STEP_DEBUG: true
  ACTIONS_RUNNER_DEBUG: true

# SSH到运行器
- 使用: mxschmitt/action-tmate@v3

高级模式

详细指南,参考:

示例

完整可运行工作流:

  • examples/github-actions-basic/ - 启动模板(lint/test/build)
  • examples/github-actions-monorepo/ - Turborepo远程缓存
  • examples/github-actions-slsa/ - SLSA级别3来源证明
  • examples/gitlab-ci-monorepo/ - 父-子动态管道
  • examples/argo-workflows-dag/ - 钻石DAG并行化
  • examples/multi-language-matrix/ - 跨平台测试

实用脚本

无令牌执行:

  • scripts/validate_workflow.py - 验证YAML语法和最佳实践
  • scripts/generate_github_workflow.py - 从模板生成工作流
  • scripts/analyze_ci_performance.py - CI指标分析
  • scripts/setup_oidc_aws.py - 自动化AWS OIDC设置

相关技能

testing-strategies - 测试执行策略(单元、集成、E2E) deploying-applications - 部署自动化和GitOps auth-security - 秘密管理和认证 observability - 管道监控和警报