cicd-pipeline-generator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCI/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 for detailed platform comparisons, pros/cons, and use case recommendations.
references/platform-comparison.md根据项目需求选择合适的CI/CD平台:
- GitHub Actions:最适合托管在GitHub上的项目,具备原生集成能力
- GitLab CI/CD:适用于GitLab仓库,可满足复杂流水线需求
- CircleCI:针对Docker工作流和快速构建时间优化
- Jenkins:适合自托管、高度可定制的环境
如需详细的平台对比、优缺点分析及适用场景建议,请参考。
references/platform-comparison.md2. Pipeline Configuration Generation
2. 流水线配置生成
Generate pipeline configs following these principles:
遵循以下原则生成流水线配置:
Pipeline Stages
流水线阶段
Structure pipelines with these standard stages:
-
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
-
Lint
- Run ESLint for code quality
- Run TypeScript type checking
- Fail fast on linting errors
-
Test
- Execute unit tests
- Execute integration tests
- Generate code coverage reports
- Upload coverage to reporting services (Codecov, Coveralls)
-
Build
- Create production build
- Verify build succeeds
- Store build artifacts
-
Deploy
- Deploy to staging (develop branch)
- Deploy to production (main branch)
- Run post-deployment smoke tests
按以下标准阶段构建流水线:
-
安装依赖
- 从仓库拉取代码
- 设置运行时环境(Node.js版本)
- 恢复缓存的依赖
- 使用安装依赖
npm ci - 缓存依赖以供后续构建使用
-
代码检查
- 运行ESLint检查代码质量
- 执行TypeScript类型检查
- 代码检查出错时立即终止流程
-
测试
- 执行单元测试
- 执行集成测试
- 生成代码覆盖率报告
- 将覆盖率数据上传至报告服务(Codecov、Coveralls)
-
构建
- 创建生产环境构建产物
- 验证构建是否成功
- 存储构建产物
-
部署
- 部署到预发布环境(develop分支)
- 部署到生产环境(main分支)
- 运行部署后的冒烟测试
Caching Strategy
缓存策略
Implement effective caching to speed up builds:
yaml
undefined实现高效缓存以加快构建速度:
yaml
undefinedCache node_modules based on package-lock.json
根据package-lock.json缓存node_modules
cache:
key: ${{ hashFiles('package-lock.json') }}
paths:
- node_modules/
- .npm/
undefinedcache:
key: ${{ hashFiles('package-lock.json') }}
paths:
- node_modules/
- .npm/
undefinedEnvironment Variables
环境变量
Configure necessary environment variables:
- : Set to
NODE_ENVfor buildsproduction - Platform-specific tokens: Store as secrets
- Build-time variables: Pass to build process
配置必要的环境变量:
- :构建时设置为
NODE_ENVproduction - 平台专属令牌:存储为保密变量
- 构建时变量:传递给构建流程
3. Template Usage
3. 模板使用
Use provided templates from directory:
assets/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:
- Copy the appropriate template file
- Place in the correct location:
- GitHub Actions:
.github/workflows/ci.yml - GitLab CI:
.gitlab-ci.yml
- GitHub Actions:
- Customize deployment targets, environment variables, and branch names
- Add required secrets to platform settings
使用目录下提供的模板:
assets/GitHub Actions模板():
assets/github-actions-nodejs.yml- 包含代码检查、测试、构建、部署的多任务工作流
- 支持多Node.js版本的矩阵构建(可选)
- 集成Vercel部署
- 产物上传
- 代码覆盖率报告
GitLab CI模板():
assets/gitlab-ci-nodejs.yml- 多阶段流水线
- 依赖缓存
- 手动触发生产环境部署
- 自动触发预发布环境部署
- 覆盖率报告
模板使用步骤:
- 复制对应模板文件
- 放置到正确路径:
- GitHub Actions:
.github/workflows/ci.yml - GitLab CI:
.gitlab-ci.yml
- GitHub Actions:
- 自定义部署目标、环境变量和分支名称
- 在平台设置中添加所需的保密变量
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:
- : Get from Vercel account settings
VERCEL_TOKEN - : From Vercel project settings
VERCEL_ORG_ID - : From Vercel project settings
VERCEL_PROJECT_ID
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账户设置获取
VERCEL_TOKEN - :从Vercel项目设置获取
VERCEL_ORG_ID - :从Vercel项目设置获取
VERCEL_PROJECT_ID
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: unittestsFail 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
undefinedRun 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]
undefinedjobs:
lint: # 约30秒内失败
test: # 约2分钟内失败
build: # 约5分钟内失败
needs: [lint, test]
deploy:
needs: [build]
undefined6. 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:
-
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
- GitHub → Use
-
What stages are needed?
- Always include: Lint, Test, Build
- Optional: Security scanning, E2E tests, performance tests
- Add deployment stage if deploying from CI
-
Which deployment platform?
- Vercel → Use Vercel deployment examples
- Netlify → Use Netlify CLI approach
- AWS → Use AWS Actions/CLI
- Custom → Implement custom deployment script
-
What triggers?
- On push to main/develop
- On pull request
- On tag creation
- Manual workflow dispatch
-
What environment variables needed?
- Platform tokens (Vercel, Netlify, AWS)
- API keys for external services
- Build-time environment variables
- Feature flags
遵循以下决策树生成合适的流水线:
-
选择哪个平台?
- GitHub → 使用
assets/github-actions-nodejs.yml - GitLab → 使用
assets/gitlab-ci-nodejs.yml - CircleCI/Jenkins → 适配GitHub Actions模板
- 不确定 → 参考
references/platform-comparison.md
- GitHub → 使用
-
需要哪些阶段?
- 必须包含:代码检查、测试、构建
- 可选:安全扫描、端到端测试、性能测试
- 如果从CI执行部署,添加部署阶段
-
选择哪个部署平台?
- Vercel → 使用Vercel部署示例
- Netlify → 使用Netlify CLI方式
- AWS → 使用AWS Actions/CLI
- 自定义 → 实现自定义部署脚本
-
触发条件是什么?
- 推送到main/develop分支时
- 拉取请求创建时
- 创建标签时
- 手动触发工作流
-
需要哪些环境变量?
- 平台令牌(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 (not just
18.x)18 - Commit lockfiles ()
package-lock.json - Add retry logic for flaky external services
- Set reasonable timeouts (10-15 minutes max)
- Use for non-critical steps
continue-on-error
- 固定精确的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 deployyaml
- name: 部署
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: npm run deployArtifact 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-outputyaml
- name: 上传构建产物
uses: actions/upload-artifact@v4
with:
name: build-output
path: .next/
retention-days: 7
- name: 下载构建产物
uses: actions/download-artifact@v4
with:
name: build-outputTroubleshooting
故障排查
Pipeline Failures
流水线失败
- Check action/job logs for error messages
- Verify environment variables and secrets are set
- Test commands locally before adding to pipeline
- Check for platform-specific issues in documentation
- 查看任务/工作流日志中的错误信息
- 验证环境变量和保密变量是否正确设置
- 在本地测试命令后再添加到流水线
- 查看平台文档中的特定问题
Slow Builds
构建缓慢
- Verify cache is working (check cache hit/miss logs)
- Parallelize independent jobs
- Use faster runners if available
- Optimize dependency installation
- 验证缓存是否正常工作(查看缓存命中/未命中日志)
- 并行执行独立任务
- 如果可用,使用更快的运行器
- 优化依赖安装过程
Deployment Failures
部署失败
- Verify deployment tokens are valid
- Check platform status pages
- Review deployment logs
- Test deployment commands locally
- 验证部署令牌是否有效
- 查看平台状态页面
- 检查部署日志
- 在本地测试部署命令
Resources
资源
Templates (assets/
)
assets/模板(assets/
)
assets/- : Complete GitHub Actions workflow
github-actions-nodejs.yml - : Complete GitLab CI pipeline
gitlab-ci-nodejs.yml
- : 完整的GitHub Actions工作流
github-actions-nodejs.yml - : 完整的GitLab CI流水线
gitlab-ci-nodejs.yml
Reference Documentation (references/
)
references/参考文档(references/
)
references/- : Detailed comparison of CI/CD platforms, deployment targets, best practices, and common patterns
platform-comparison.md
- : CI/CD平台、部署目标、最佳实践和常见模式的详细对比
platform-comparison.md
Example Usage
使用示例
User Request: "Create a GitHub Actions workflow that runs tests and deploys to Vercel"
Steps:
- Copy template
assets/github-actions-nodejs.yml - Create directory if it doesn't exist
.github/workflows/ - Save as
.github/workflows/ci.yml - Update deployment section with Vercel credentials
- Add secrets to GitHub repository settings:
VERCEL_TOKENVERCEL_ORG_IDVERCEL_PROJECT_ID
- Commit and push to trigger workflow
User Request: "Set up GitLab CI with staging and production environments"
Steps:
- Copy template
assets/gitlab-ci-nodejs.yml - Save as in repository root
.gitlab-ci.yml - Configure GitLab CI/CD variables:
VERCEL_TOKEN- Other deployment credentials
- Review manual approval settings for production
- Commit to trigger pipeline
用户请求:"创建一个运行测试并部署到Vercel的GitHub Actions工作流"
步骤:
- 复制模板
assets/github-actions-nodejs.yml - 如果不存在,创建目录
.github/workflows/ - 保存为
.github/workflows/ci.yml - 更新部署部分的Vercel凭证
- 在GitHub仓库设置中添加保密变量:
VERCEL_TOKENVERCEL_ORG_IDVERCEL_PROJECT_ID
- 提交并推送代码以触发工作流
用户请求:"搭建带有预发布和生产环境的GitLab CI"
步骤:
- 复制模板
assets/gitlab-ci-nodejs.yml - 保存到仓库根目录的
.gitlab-ci.yml - 配置GitLab CI/CD变量:
VERCEL_TOKEN- 其他部署凭证
- 检查生产环境的手动审批设置
- 提交代码以触发流水线
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 AMyaml
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 }}