cicd-pipeline-generator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

CI/CD Pipeline Generator

CI/CD 流水线生成器

Overview

概述

Generate production-ready CI/CD pipeline configuration files for various platforms (GitHub Actions, GitLab CI, CircleCI, Jenkins). This skill provides templates and guidance for setting up automated workflows that handle linting, testing, building, and deployment for modern web applications, particularly Node.js/Next.js projects.
为各类平台(GitHub Actions、GitLab CI、CircleCI、Jenkins)生成可用于生产环境的CI/CD流水线配置文件。本技能提供模板和指导,帮助为现代Web应用(尤其是Node.js/Next.js项目)搭建处理代码检查、测试、构建和部署的自动化工作流。

Core Capabilities

核心功能

1. Platform Selection

1. 平台选择

Choose the appropriate CI/CD platform based on project requirements:
  • GitHub Actions: Best for GitHub-hosted projects with native integration
  • GitLab CI/CD: Ideal for GitLab repositories with complex pipeline needs
  • CircleCI: Optimized for Docker workflows and fast build times
  • Jenkins: Suitable for self-hosted, highly customizable environments
Refer to
references/platform-comparison.md
for detailed platform comparisons, pros/cons, and use case recommendations.
根据项目需求选择合适的CI/CD平台:
  • GitHub Actions:最适合托管在GitHub上的项目,具备原生集成能力
  • GitLab CI/CD:适用于GitLab仓库,可满足复杂流水线需求
  • CircleCI:针对Docker工作流和快速构建时间优化
  • Jenkins:适合自托管、高度可定制的环境
如需详细的平台对比、优缺点分析及适用场景建议,请参考
references/platform-comparison.md

2. Pipeline Configuration Generation

2. 流水线配置生成

Generate pipeline configs following these principles:
遵循以下原则生成流水线配置:

Pipeline Stages

流水线阶段

Structure pipelines with these standard stages:
  1. Install Dependencies
    • Checkout code from repository
    • Setup runtime environment (Node.js version)
    • Restore cached dependencies
    • Install dependencies with
      npm ci
    • Cache dependencies for future runs
  2. Lint
    • Run ESLint for code quality
    • Run TypeScript type checking
    • Fail fast on linting errors
  3. Test
    • Execute unit tests
    • Execute integration tests
    • Generate code coverage reports
    • Upload coverage to reporting services (Codecov, Coveralls)
  4. Build
    • Create production build
    • Verify build succeeds
    • Store build artifacts
  5. Deploy
    • Deploy to staging (develop branch)
    • Deploy to production (main branch)
    • Run post-deployment smoke tests
按以下标准阶段构建流水线:
  1. 安装依赖
    • 从仓库拉取代码
    • 设置运行时环境(Node.js版本)
    • 恢复缓存的依赖
    • 使用
      npm ci
      安装依赖
    • 缓存依赖以供后续构建使用
  2. 代码检查
    • 运行ESLint检查代码质量
    • 执行TypeScript类型检查
    • 代码检查出错时立即终止流程
  3. 测试
    • 执行单元测试
    • 执行集成测试
    • 生成代码覆盖率报告
    • 将覆盖率数据上传至报告服务(Codecov、Coveralls)
  4. 构建
    • 创建生产环境构建产物
    • 验证构建是否成功
    • 存储构建产物
  5. 部署
    • 部署到预发布环境(develop分支)
    • 部署到生产环境(main分支)
    • 运行部署后的冒烟测试

Caching Strategy

缓存策略

Implement effective caching to speed up builds:
yaml
undefined
实现高效缓存以加快构建速度:
yaml
undefined

Cache node_modules based on package-lock.json

根据package-lock.json缓存node_modules

cache: key: ${{ hashFiles('package-lock.json') }} paths: - node_modules/ - .npm/
undefined
cache: key: ${{ hashFiles('package-lock.json') }} paths: - node_modules/ - .npm/
undefined

Environment Variables

环境变量

Configure necessary environment variables:
  • NODE_ENV
    : Set to
    production
    for builds
  • Platform-specific tokens: Store as secrets
  • Build-time variables: Pass to build process
配置必要的环境变量:
  • NODE_ENV
    :构建时设置为
    production
  • 平台专属令牌:存储为保密变量
  • 构建时变量:传递给构建流程

3. Template Usage

3. 模板使用

Use provided templates from
assets/
directory:
GitHub Actions Template (
assets/github-actions-nodejs.yml
):
  • Multi-job workflow with lint, test, build, deploy
  • Matrix builds for multiple Node.js versions (optional)
  • Vercel deployment integration
  • Artifact uploading
  • Code coverage reporting
GitLab CI Template (
assets/gitlab-ci-nodejs.yml
):
  • Multi-stage pipeline
  • Dependency caching
  • Manual production deployment
  • Automatic staging deployment
  • Coverage reporting
To use a template:
  1. Copy the appropriate template file
  2. Place in the correct location:
    • GitHub Actions:
      .github/workflows/ci.yml
    • GitLab CI:
      .gitlab-ci.yml
  3. Customize deployment targets, environment variables, and branch names
  4. Add required secrets to platform settings
使用
assets/
目录下提供的模板:
GitHub Actions模板
assets/github-actions-nodejs.yml
):
  • 包含代码检查、测试、构建、部署的多任务工作流
  • 支持多Node.js版本的矩阵构建(可选)
  • 集成Vercel部署
  • 产物上传
  • 代码覆盖率报告
GitLab CI模板
assets/gitlab-ci-nodejs.yml
):
  • 多阶段流水线
  • 依赖缓存
  • 手动触发生产环境部署
  • 自动触发预发布环境部署
  • 覆盖率报告
模板使用步骤:
  1. 复制对应模板文件
  2. 放置到正确路径:
    • GitHub Actions:
      .github/workflows/ci.yml
    • GitLab CI:
      .gitlab-ci.yml
  3. 自定义部署目标、环境变量和分支名称
  4. 在平台设置中添加所需的保密变量

4. Deployment Configuration

4. 部署配置

Vercel Deployment

Vercel部署

For GitHub Actions:
yaml
- uses: amondnet/vercel-action@v25
  with:
    vercel-token: ${{ secrets.VERCEL_TOKEN }}
    vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
    vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}
    vercel-args: '--prod'
Required Secrets:
  • VERCEL_TOKEN
    : Get from Vercel account settings
  • VERCEL_ORG_ID
    : From Vercel project settings
  • VERCEL_PROJECT_ID
    : From Vercel project settings
GitHub Actions配置示例:
yaml
- uses: amondnet/vercel-action@v25
  with:
    vercel-token: ${{ secrets.VERCEL_TOKEN }}
    vercel-org-id: ${{ secrets.VERCEL_ORG_ID }}
    vercel-project-id: ${{ secrets.VERCEL_PROJECT_ID }}
    vercel-args: '--prod'
所需保密变量
  • VERCEL_TOKEN
    :从Vercel账户设置获取
  • VERCEL_ORG_ID
    :从Vercel项目设置获取
  • VERCEL_PROJECT_ID
    :从Vercel项目设置获取

Netlify Deployment

Netlify部署

yaml
- run: |
    npm install -g netlify-cli
    netlify deploy --prod --dir=.next
  env:
    NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
    NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
yaml
- run: |
    npm install -g netlify-cli
    netlify deploy --prod --dir=.next
  env:
    NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
    NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}

AWS S3 + CloudFront

AWS S3 + CloudFront

yaml
- uses: aws-actions/configure-aws-credentials@v4
  with:
    aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
    aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    aws-region: us-east-1

- run: |
    aws s3 sync .next/static s3://${{ secrets.S3_BUCKET }}/static
    aws cloudfront create-invalidation --distribution-id ${{ secrets.CF_DIST_ID }} --paths "/*"
yaml
- uses: aws-actions/configure-aws-credentials@v4
  with:
    aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
    aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    aws-region: us-east-1

- run: |
    aws s3 sync .next/static s3://${{ secrets.S3_BUCKET }}/static
    aws cloudfront create-invalidation --distribution-id ${{ secrets.CF_DIST_ID }} --paths "/*"

5. Testing Integration

5. 测试集成

Configure test execution with proper reporting:
Jest Configuration:
yaml
- name: Run tests with coverage
  run: npm test -- --coverage --coverageReporters=text --coverageReporters=lcov

- name: Upload coverage
  uses: codecov/codecov-action@v4
  with:
    files: ./coverage/lcov.info
    flags: unittests
Fail Fast Strategy:
yaml
undefined
配置测试执行并生成正确报告:
Jest配置
yaml
- name: 运行带覆盖率的测试
  run: npm test -- --coverage --coverageReporters=text --coverageReporters=lcov

- name: 上传覆盖率数据
  uses: codecov/codecov-action@v4
  with:
    files: ./coverage/lcov.info
    flags: unittests
快速失败策略
yaml
undefined

Run quick tests first

先运行快速测试

jobs: lint: # Fails in ~30 seconds test: # Fails in ~2 minutes build: # Fails in ~5 minutes needs: [lint, test] deploy: needs: [build]
undefined
jobs: lint: # 约30秒内失败 test: # 约2分钟内失败 build: # 约5分钟内失败 needs: [lint, test] deploy: needs: [build]
undefined

6. Branch-Based Workflows

6. 基于分支的工作流

Implement different behaviors per branch:
Feature Branches / PRs:
  • Run lint + test only
  • No deployment
  • Add PR comments with test results
Develop Branch:
  • Run lint + test + build
  • Deploy to staging environment
  • Automatic deployment
Main Branch:
  • Run lint + test + build
  • Deploy to production
  • Manual approval (optional)
  • Create release tags
Example:
yaml
deploy_staging:
  if: github.ref == 'refs/heads/develop'
  # Deploy to staging

deploy_production:
  if: github.ref == 'refs/heads/main'
  environment: production  # Requires manual approval
  # Deploy to production
为不同分支实现不同的行为:
功能分支 / 拉取请求
  • 仅运行代码检查 + 测试
  • 不执行部署
  • 在拉取请求中添加测试结果评论
Develop分支
  • 运行代码检查 + 测试 + 构建
  • 部署到预发布环境
  • 自动部署
Main分支
  • 运行代码检查 + 测试 + 构建
  • 部署到生产环境
  • 手动审批(可选)
  • 创建发布标签
示例
yaml
deploy_staging:
  if: github.ref == 'refs/heads/develop'
  # 部署到预发布环境

deploy_production:
  if: github.ref == 'refs/heads/main'
  environment: production  # 需要手动审批
  # 部署到生产环境

Workflow Decision Tree

工作流决策树

Follow this decision tree to generate the appropriate pipeline:
  1. Which platform?
    • GitHub → Use
      assets/github-actions-nodejs.yml
    • GitLab → Use
      assets/gitlab-ci-nodejs.yml
    • CircleCI/Jenkins → Adapt GitHub Actions template
    • Unsure → Consult
      references/platform-comparison.md
  2. What stages are needed?
    • Always include: Lint, Test, Build
    • Optional: Security scanning, E2E tests, performance tests
    • Add deployment stage if deploying from CI
  3. Which deployment platform?
    • Vercel → Use Vercel deployment examples
    • Netlify → Use Netlify CLI approach
    • AWS → Use AWS Actions/CLI
    • Custom → Implement custom deployment script
  4. What triggers?
    • On push to main/develop
    • On pull request
    • On tag creation
    • Manual workflow dispatch
  5. What environment variables needed?
    • Platform tokens (Vercel, Netlify, AWS)
    • API keys for external services
    • Build-time environment variables
    • Feature flags
遵循以下决策树生成合适的流水线:
  1. 选择哪个平台?
    • GitHub → 使用
      assets/github-actions-nodejs.yml
    • GitLab → 使用
      assets/gitlab-ci-nodejs.yml
    • CircleCI/Jenkins → 适配GitHub Actions模板
    • 不确定 → 参考
      references/platform-comparison.md
  2. 需要哪些阶段?
    • 必须包含:代码检查、测试、构建
    • 可选:安全扫描、端到端测试、性能测试
    • 如果从CI执行部署,添加部署阶段
  3. 选择哪个部署平台?
    • Vercel → 使用Vercel部署示例
    • Netlify → 使用Netlify CLI方式
    • AWS → 使用AWS Actions/CLI
    • 自定义 → 实现自定义部署脚本
  4. 触发条件是什么?
    • 推送到main/develop分支时
    • 拉取请求创建时
    • 创建标签时
    • 手动触发工作流
  5. 需要哪些环境变量?
    • 平台令牌(Vercel、Netlify、AWS)
    • 外部服务的API密钥
    • 构建时环境变量
    • 功能开关

Best Practices

最佳实践

Security

安全

  • Store all secrets in platform secret management (never in code)
  • Use least-privilege tokens (read-only when possible)
  • Rotate secrets regularly
  • Audit secret access permissions
  • Never log secrets (use
    ***
    masking)
  • 所有保密变量存储在平台的保密管理系统中(绝不要存入代码)
  • 使用最小权限令牌(尽可能使用只读权限)
  • 定期轮换保密变量
  • 审计保密变量的访问权限
  • 绝不要记录保密变量(使用
    ***
    掩码)

Performance

性能

  • Cache dependencies aggressively
  • Parallelize independent jobs
  • Use matrix builds for multi-version testing
  • Fail fast: Run quick checks before slow ones
  • Optimize Docker layer caching
  • 积极缓存依赖
  • 并行执行独立任务
  • 使用矩阵构建进行多版本测试
  • 快速失败:先运行快速检查,再运行慢速检查
  • 优化Docker层缓存

Reliability

可靠性

  • Pin exact Node.js versions (
    18.x
    not just
    18
    )
  • Commit lockfiles (
    package-lock.json
    )
  • Add retry logic for flaky external services
  • Set reasonable timeouts (10-15 minutes max)
  • Use
    continue-on-error
    for non-critical steps
  • 固定精确的Node.js版本(如
    18.x
    而非仅
    18
  • 提交锁文件(
    package-lock.json
  • 为不稳定的外部服务添加重试逻辑
  • 设置合理的超时时间(最多10-15分钟)
  • 对非关键步骤使用
    continue-on-error

Maintainability

可维护性

  • Add comments explaining complex logic
  • Use reusable workflows/templates
  • Keep configs DRY (Don't Repeat Yourself)
  • Version control all pipeline changes
  • Document required secrets in README
  • 添加注释解释复杂逻辑
  • 使用可复用的工作流/模板
  • 保持配置文件DRY(不要重复自己)
  • 对所有流水线变更进行版本控制
  • 在README中记录所需的保密变量

Common Patterns

常见模式

Multi-Environment Deployment

多环境部署

yaml
deploy_staging:
  environment: staging
  if: github.ref == 'refs/heads/develop'

deploy_production:
  environment: production
  if: github.ref == 'refs/heads/main'
  needs: [deploy_staging]
yaml
deploy_staging:
  environment: staging
  if: github.ref == 'refs/heads/develop'

deploy_production:
  environment: production
  if: github.ref == 'refs/heads/main'
  needs: [deploy_staging]

Matrix Testing

矩阵测试

yaml
strategy:
  matrix:
    node-version: [16.x, 18.x, 20.x]
    os: [ubuntu-latest, windows-latest]
yaml
strategy:
  matrix:
    node-version: [16.x, 18.x, 20.x]
    os: [ubuntu-latest, windows-latest]

Conditional Steps

条件步骤

yaml
- name: Deploy
  if: github.event_name == 'push' && github.ref == 'refs/heads/main'
  run: npm run deploy
yaml
- name: 部署
  if: github.event_name == 'push' && github.ref == 'refs/heads/main'
  run: npm run deploy

Artifact Management

产物管理

yaml
- name: Upload build
  uses: actions/upload-artifact@v4
  with:
    name: build-output
    path: .next/
    retention-days: 7

- name: Download build
  uses: actions/download-artifact@v4
  with:
    name: build-output
yaml
- name: 上传构建产物
  uses: actions/upload-artifact@v4
  with:
    name: build-output
    path: .next/
    retention-days: 7

- name: 下载构建产物
  uses: actions/download-artifact@v4
  with:
    name: build-output

Troubleshooting

故障排查

Pipeline Failures

流水线失败

  1. Check action/job logs for error messages
  2. Verify environment variables and secrets are set
  3. Test commands locally before adding to pipeline
  4. Check for platform-specific issues in documentation
  1. 查看任务/工作流日志中的错误信息
  2. 验证环境变量和保密变量是否正确设置
  3. 在本地测试命令后再添加到流水线
  4. 查看平台文档中的特定问题

Slow Builds

构建缓慢

  1. Verify cache is working (check cache hit/miss logs)
  2. Parallelize independent jobs
  3. Use faster runners if available
  4. Optimize dependency installation
  1. 验证缓存是否正常工作(查看缓存命中/未命中日志)
  2. 并行执行独立任务
  3. 如果可用,使用更快的运行器
  4. 优化依赖安装过程

Deployment Failures

部署失败

  1. Verify deployment tokens are valid
  2. Check platform status pages
  3. Review deployment logs
  4. Test deployment commands locally
  1. 验证部署令牌是否有效
  2. 查看平台状态页面
  3. 检查部署日志
  4. 在本地测试部署命令

Resources

资源

Templates (
assets/
)

模板(
assets/

  • github-actions-nodejs.yml
    : Complete GitHub Actions workflow
  • gitlab-ci-nodejs.yml
    : Complete GitLab CI pipeline
  • github-actions-nodejs.yml
    : 完整的GitHub Actions工作流
  • gitlab-ci-nodejs.yml
    : 完整的GitLab CI流水线

Reference Documentation (
references/
)

参考文档(
references/

  • platform-comparison.md
    : Detailed comparison of CI/CD platforms, deployment targets, best practices, and common patterns
  • platform-comparison.md
    : CI/CD平台、部署目标、最佳实践和常见模式的详细对比

Example Usage

使用示例

User Request: "Create a GitHub Actions workflow that runs tests and deploys to Vercel"
Steps:
  1. Copy
    assets/github-actions-nodejs.yml
    template
  2. Create
    .github/workflows/
    directory if it doesn't exist
  3. Save as
    .github/workflows/ci.yml
  4. Update deployment section with Vercel credentials
  5. Add secrets to GitHub repository settings:
    • VERCEL_TOKEN
    • VERCEL_ORG_ID
    • VERCEL_PROJECT_ID
  6. Commit and push to trigger workflow
User Request: "Set up GitLab CI with staging and production environments"
Steps:
  1. Copy
    assets/gitlab-ci-nodejs.yml
    template
  2. Save as
    .gitlab-ci.yml
    in repository root
  3. Configure GitLab CI/CD variables:
    • VERCEL_TOKEN
    • Other deployment credentials
  4. Review manual approval settings for production
  5. Commit to trigger pipeline
用户请求:"创建一个运行测试并部署到Vercel的GitHub Actions工作流"
步骤
  1. 复制
    assets/github-actions-nodejs.yml
    模板
  2. 如果不存在,创建
    .github/workflows/
    目录
  3. 保存为
    .github/workflows/ci.yml
  4. 更新部署部分的Vercel凭证
  5. 在GitHub仓库设置中添加保密变量:
    • VERCEL_TOKEN
    • VERCEL_ORG_ID
    • VERCEL_PROJECT_ID
  6. 提交并推送代码以触发工作流
用户请求:"搭建带有预发布和生产环境的GitLab CI"
步骤
  1. 复制
    assets/gitlab-ci-nodejs.yml
    模板
  2. 保存到仓库根目录的
    .gitlab-ci.yml
  3. 配置GitLab CI/CD变量:
    • VERCEL_TOKEN
    • 其他部署凭证
  4. 检查生产环境的手动审批设置
  5. 提交代码以触发流水线

Advanced Configuration

高级配置

Monorepo Support

单仓库多项目支持

yaml
paths:
  - 'apps/frontend/**'
  - 'packages/**'
yaml
paths:
  - 'apps/frontend/**'
  - 'packages/**'

Scheduled Runs

定时运行

yaml
on:
  schedule:
    - cron: '0 2 * * *'  # Daily at 2 AM
yaml
on:
  schedule:
    - cron: '0 2 * * *'  # 每天凌晨2点运行

External Service Integration

外部服务集成

yaml
- name: Notify Slack
  uses: 8398a7/action-slack@v3
  with:
    status: ${{ job.status }}
    webhook_url: ${{ secrets.SLACK_WEBHOOK }}
yaml
- name: 通知Slack
  uses: 8398a7/action-slack@v3
  with:
    status: ${{ job.status }}
    webhook_url: ${{ secrets.SLACK_WEBHOOK }}

Security Scanning

安全扫描

yaml
- name: Run security audit
  run: npm audit --audit-level=moderate

- name: Check for vulnerabilities
  uses: snyk/actions/node@master
  env:
    SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
yaml
- name: 运行安全审计
  run: npm audit --audit-level=moderate

- name: 检查漏洞
  uses: snyk/actions/node@master
  env:
    SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}