mcpField
C
ci-cd-pipelines

ci-cd-pipelines

github.com/rohitg00云与 DevOps2.8k203 次安装24 周前更新
代码仓库

关于 ci-cd-pipelines

ci-cd-pipelines 是用于配置和检查 CI/CD 流水线的参考技能,提供 GitHub Actions 与 GitLab CI 示例及清单,涉及复用操作、矩阵构建、缓存和密钥管理。

主要功能

  1. 01GitHub Actions 工作流指南
  2. 02GitLab CI 流水线示例
  3. 03可复用的 GitHub Actions
  4. 04跨版本和系统的矩阵构建
  5. 05流水线缓存与并发实践
  6. 06密钥管理与部署防护

适用场景

  1. 01创建 GitHub Actions 工作流
  2. 02组织 GitLab CI 阶段
  3. 03在多个工作流中复用通用设置
  4. 04检查流水线缓存和密钥管理

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/rohitg00/awesome-claude-code-toolkit --skill ci-cd-pipelines

安装后在 Claude Code 中输入/ci-cd-pipelines确认已加载。

详细介绍

CI/CD 流水线

GitHub Actions 工作流

name: CI
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

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
      - run: npm run typecheck

  test:
    runs-on: ubuntu-latest
    needs: lint
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_DB: test
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
        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
      - run: npm test -- --coverage
        env:
          DATABASE_URL: postgres://test:test@localhost:5432/test
      - uses: codecov/codecov-action@v4

  deploy:
    runs-on: ubuntu-latest
    needs: test
    if: github.ref == 'refs/heads/main'
    environment: production
    steps:
      - uses: actions/checkout@v4
      - run: ./scripts/deploy.sh

使用 concurrency 取消过时的运行。使用 needs 定义作业依赖关系。

GitLab CI 流水线

stages:
  - validate
  - test
  - build
  - deploy

variables:
  NODE_IMAGE: node:22-alpine

lint:
  stage: validate
  image: $NODE_IMAGE
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths: [node_modules/]
  script:
    - npm ci
    - npm run lint
    - npm run typecheck

test:
  stage: test
  image: $NODE_IMAGE
  services:
    - postgres:16
  variables:
    POSTGRES_DB: test
    DATABASE_URL: postgres://runner:@postgres:5432/test
  script:
    - npm ci
    - npm test -- --coverage
  coverage: '/Statements\s*:\s*(\d+\.?\d*)%/'
  artifacts:
    reports:
      junit: coverage/junit.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura.xml

build:
  stage: build
  image: docker:24
  services: [docker:24-dind]
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

deploy:
  stage: deploy
  environment:
    name: production
    url: https://app.example.com
  script:
    - ./deploy.sh $CI_COMMIT_SHA
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: manual

可复用的 GitHub Action

# .github/actions/setup/action.yml
name: Setup
description: Install dependencies and cache
inputs:
  node-version:
    default: "22"
runs:
  using: composite
  steps:
    - uses: actions/setup-node@v4
      with:
        node-version: ${{ inputs.node-version }}
        cache: npm
    - run: npm ci
      shell: bash
# Usage in workflow
steps:
  - uses: actions/checkout@v4
  - uses: ./.github/actions/setup
  - run: npm test

矩阵策略

test:
  strategy:
    fail-fast: false
    matrix:
      node: [20, 22]
      os: [ubuntu-latest, macos-latest]
  runs-on: ${{ matrix.os }}
  steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node }}
    - run: npm ci && npm test

反模式

  • 未在多次运行之间缓存依赖项(npm、pip、cargo)
  • lint 和测试可以并行执行,却将所有作业按顺序运行
  • 将机密存储在工作流文件中,而不是仓库或环境机密中
  • 缺少 concurrency 组,导致快速推送时产生冗余的 CI 运行
  • 矩阵构建未使用 fail-fast: false(一次失败会取消其他任务)
  • 未设置审批关卡或环境保护规则就进行部署

检查清单

  • 在多次 CI 运行之间缓存依赖项
  • 并发组会取消过时的流水线运行
  • lint、类型检查和测试作为独立且可并行的作业运行
  • 数据库服务在测试开始前执行健康检查
  • 上传并跟踪覆盖率报告
  • 生产环境部署作业需要审批
  • 使用可复用的 action/模板提取通用设置步骤
  • 将机密存储在 CI 平台中,绝不存入代码

评论

支持 Markdown