mcpField
P
pipeline-blueprint

pipeline-blueprint

github.com/zebbern云与 DevOps4.7k40 次安装1 天前更新
代码仓库

关于 pipeline-blueprint

pipeline-blueprint 是面向团队的 CI/CD 技能,针对项目类型提供 GitHub Actions 和 GitLab CI 模板及配置建议,涵盖流水线设计与安全实践。

主要功能

  1. 01按项目类型推荐流水线模板
  2. 02提供 GitHub Actions 与 GitLab CI 示例
  3. 03涵盖缓存、并行和制品策略
  4. 04包含 CI/CD 安全检查清单
  5. 05介绍可复用工作流与环境保护

适用场景

  1. 01为前端或后端项目搭建 CI/CD
  2. 02为库、全栈或移动项目选择流水线
  3. 03为多包仓库规划受影响任务
  4. 04规范部署审批与凭据管理

安装

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

$npx skills add https://github.com/zebbern/claude-code-guide --skill pipeline-blueprint

安装后在 Claude Code 中输入/pipeline-blueprint确认已加载。

附带文件LICENSE

详细介绍

CI/CD 配置最佳实践

此技能提供适用于 GitHub Actions 和 GitLab CI 的 CI/CD 流水线模板与最佳实践。它会根据项目类型推荐配置,帮助团队快速搭建可靠、安全且高效的流水线。

使用方法

当用户描述其项目(语言、框架、部署目标)时,推荐下方最合适的流水线模板。根据其技术栈调整阶段、缓存策略和部署步骤。


通用最佳实践

流水线设计原则
  1. 快速失败:在耗时的集成测试或端到端测试之前运行代码检查和单元测试。
  2. 积极使用缓存:缓存依赖目录(node_modules、.pip_cache、.m2、.gradle),以加快构建。
  3. 固定版本:将 CI 运行器映像、工具版本和操作版本固定为 SHA 或确切标签,切勿使用 latest。
  4. 最小权限:为令牌和凭据使用最低限度的权限。在支持的情况下,优先使用 OIDC,而不是长期有效的密钥。
  5. 并行执行:将测试套件拆分到并行作业中。使用矩阵构建进行多版本测试。
  6. 不可变制品:只构建一次,并将同一制品逐步部署到预发布环境 → 生产环境。
  7. 分支保护:要求 CI 通过后才能合并。在默认分支上使用状态检查。
安全检查清单
  • 切勿在流水线文件中硬编码密钥;请使用平台的密钥管理功能(GitHub Secrets / GitLab CI Variables)。
  • 使用前审查第三方操作/映像。优先选择官方或已验证的来源。
  • 尽可能启用依赖项扫描(Dependabot、GitLab Dependency Scanning)和 SAST。
  • 限制可触发生产部署的人员。
  • 定期轮换密钥。

项目类型模板

1. 前端(React / Vue / Angular / 静态网站)

关键阶段:安装 → 代码检查 → 测试 → 构建 → 部署

GitHub Actions
name: Frontend CI/CD

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

permissions:
  contents: read

jobs:
  ci:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm

      - run: npm ci

      - run: npm run lint

      - run: npm test -- --coverage

      - run: npm run build

      - uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist/

  deploy:
    needs: ci
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    runs-on: ubuntu-22.04
    environment: production
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist/

      # Replace with your deployment step (e.g., S3 sync, Cloudflare Pages, Vercel)
      - name: Deploy
        run: echo "Add your deployment command here"
GitLab CI
stages:
  - install
  - lint
  - test
  - build
  - deploy

default:
  image: node:20-slim
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/

install:
  stage: install
  script:
    - npm ci

lint:
  stage: lint
  script:
    - npm run lint

test:
  stage: test
  script:
    - npm test -- --coverage
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura-coverage.xml

build:
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - dist/

deploy:
  stage: deploy
  script:
    - echo "Add your deployment command here"
  environment:
    name: production
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: on_success

2. 后端(Node.js / Python / Go / Java)

关键阶段:安装 → 代码检查 → 测试 → 构建 → Docker 构建 → 部署

GitHub Actions(Python 示例)
name: Backend CI/CD

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

permissions:
  contents: read
  packages: write

jobs:
  ci:
    runs-on: ubuntu-22.04
    services:
      postgres:
        image: postgres:16
        env:
          POSTGRES_USER: test
          POSTGRES_PASSWORD: test
          POSTGRES_DB: testdb
        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-python@v5
        with:
          python-version: "3.12"
          cache: pip

      - run: pip install -r requirements.txt

      - run: ruff check .

      - run: pytest --cov --cov-report=xml
        env:
          DATABASE_URL: postgresql://test:test@localhost:5432/testdb

  docker:
    needs: ci
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - uses: docker/setup-buildx-action@v3

      - uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
GitLab CI(Python 示例)
stages:
  - test
  - build
  - deploy

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.pip_cache"

default:
  image: python:3.12-slim

test:
  stage: test
  services:
    - postgres:16
  variables:
    POSTGRES_USER: test
    POSTGRES_PASSWORD: test
    POSTGRES_DB: testdb
    DATABASE_URL: postgresql://test:test@postgres:5432/testdb
  cache:
    key: pip
    paths:
      - .pip_cache/
  script:
    - pip install -r requirements.txt
    - ruff check .
    - pytest --cov --cov-report=xml
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml

build-image:
  stage: build
  image: docker:24
  services:
    - docker:24-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - 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
  script:
    - echo "Add your deployment command here"
  environment:
    name: production
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: manual

3. 库 / 软件包(npm / PyPI / Maven)

关键阶段:代码检查 → 测试(矩阵)→ 构建 → 发布

GitHub Actions(npm 库示例)
name: Library CI/CD

on:
  push:
    branches: [main]
    tags: ["v*"]
  pull_request:
    branches: [main]

permissions:
  contents: read
  id-token: write

jobs:
  test:
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        node-version: [18, 20, 22]
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: npm

      - run: npm ci
      - run: npm run lint
      - run: npm test

  publish:
    needs: test
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          registry-url: https://registry.npmjs.org
          cache: npm

      - run: npm ci
      - run: npm run build
      - run: npm publish --provenance --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
GitLab CI(PyPI 库示例)
stages:
  - test
  - publish

test:
  stage: test
  image: python:${PYTHON_VERSION}-slim
  parallel:
    matrix:
      - PYTHON_VERSION: ["3.10", "3.11", "3.12"]
  script:
    - pip install -e ".[dev]"
    - ruff check .
    - pytest

publish:
  stage: publish
  image: python:3.12-slim
  script:
    - pip install build twine
    - python -m build
    - twine upload dist/*
  variables:
    TWINE_USERNAME: __token__
    TWINE_PASSWORD: $PYPI_TOKEN
  rules:
    - if: $CI_COMMIT_TAG =~ /^v/

4. 全栈(前端 + 后端单体仓库)

关键阶段:检测变更 → 运行受影响的流水线 → 集成测试 → 部署

GitHub Actions
name: Fullstack CI/CD

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

permissions:
  contents: read

jobs:
  changes:
    runs-on: ubuntu-22.04
    outputs:
      frontend: ${{ steps.filter.outputs.frontend }}
      backend: ${{ steps.filter.outputs.backend }}
    steps:
      - uses: actions/checkout@v4
      - uses: dorny/paths-filter@v3
        id: filter
        with:
          filters: |
            frontend:
              - 'frontend/**'
            backend:
              - 'backend/**'

  frontend:
    needs: changes
    if: needs.changes.outputs.frontend == 'true'
    runs-on: ubuntu-22.04
    defaults:
      run:
        working-directory: frontend
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm
          cache-dependency-path: frontend/package-lock.json
      - run: npm ci
      - run: npm run lint
      - run: npm test
      - run: npm run build

  backend:
    needs: changes
    if: needs.changes.outputs.backend == 'true'
    runs-on: ubuntu-22.04
    defaults:
      run:
        working-directory: backend
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
          cache: pip
          cache-dependency-path: backend/requirements.txt
      - run: pip install -r requirements.txt
      - run: ruff check .
      - run: pytest

  e2e:
    needs: [frontend, backend]
    if: always() && !cancelled() && !contains(needs.*.result, 'failure')
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      - name: Run E2E tests
        run: echo "Add E2E test command (e.g., Playwright, Cypress)"

5. 单体仓库(Turborepo / Nx / Lerna)
GitHub Actions(Turborepo 示例)
name: Monorepo CI

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

permissions:
  contents: read

jobs:
  ci:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm

      - run: npm ci

      - run: npx turbo run lint test build --filter='...[HEAD^]'
        env:
          TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
          TURBO_TEAM: ${{ vars.TURBO_TEAM }}

6. 移动应用(React Native / Flutter)
GitHub Actions(React Native / Android 示例)
name: Mobile CI

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

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm

      - run: npm ci
      - run: npm run lint
      - run: npm test

  android-build:
    needs: test
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 17
          cache: gradle

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: npm

      - run: npm ci

      - name: Build Android
        working-directory: android
        run: ./gradlew assembleRelease

      - uses: actions/upload-artifact@v4
        with:
          name: android-apk
          path: android/app/build/outputs/apk/release/*.apk

高级模式

可复用工作流(GitHub Actions)

将通用 CI 逻辑提取到可复用工作流中,以减少不同仓库间的重复:

# .github/workflows/reusable-node-ci.yml
name: Reusable Node CI

on:
  workflow_call:
    inputs:
      node-version:
        type: string
        default: "20"
      working-directory:
        type: string
        default: "."

jobs:
  ci:
    runs-on: ubuntu-22.04
    defaults:
      run:
        working-directory: ${{ inputs.working-directory }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ inputs.node-version }}
          cache: npm
      - run: npm ci
      - run: npm run lint
      - run: npm test
      - run: npm run build
GitLab CI 包含模板
# Use shared templates to standardize pipelines across projects
include:
  - template: Security/SAST.gitlab-ci.yml
  - template: Security/Dependency-Scanning.gitlab-ci.yml
  - project: 'my-org/ci-templates'
    ref: main
    file: '/templates/node-ci.yml'
环境保护规则
  • GitHub:为生产部署使用环境保护规则,并要求审核者批准。
  • GitLab:对受控部署使用 when: manual 和 allow_failure: false。
缓存策略摘要
生态系统缓存键缓存路径
Node(npm)package-lock.json~/.npm 或 node_modules/
Pythonrequirements.txt~/.cache/pip
Gogo.sum~/go/pkg/mod
Javabuild.gradle / pom.xml~/.gradle/caches / ~/.m2
RustCargo.lock~/.cargo 和 target/

决策指南:使用哪个模板

您的项目类似于……推荐模板
单页应用、静态网站或 SSR 前端前端
REST API、微服务或服务端应用后端
供他人安装的 npm/PyPI/Maven 包库
在同一仓库中包含前端和后端全栈
使用 Turborepo/Nx/Lerna 管理的多个包Monorepo
React Native 或 Flutter 应用移动端

评论

支持 Markdown