github-actions-ci-workflow
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGitHub Actions CI Workflow
GitHub Actions CI工作流
Set up complete GitHub Actions workflows for continuous integration and deployment of worldbuilding applications.
为Web应用搭建完整的GitHub Actions持续集成与部署工作流。
Overview
概述
To configure comprehensive CI/CD pipelines with GitHub Actions:
- Analyze the project structure and dependencies
- Generate workflow files for lint, test, build, and deploy stages
- Configure caching strategies for node_modules, Next.js cache, and build artifacts
- Add preview deployment with automatic URL comments on pull requests
- Set up environment-specific deployment targets
要使用GitHub Actions配置全面的CI/CD流水线,请遵循以下步骤:
- 分析项目结构与依赖
- 生成用于代码检查、测试、构建与部署阶段的工作流文件
- 配置node_modules、Next.js缓存及构建产物的缓存策略
- 添加预览部署功能,并在拉取请求中自动添加URL评论
- 设置环境专属的部署目标
Workflow Structure
工作流结构
Create the following workflow files in :
.github/workflows/在目录下创建以下工作流文件:
.github/workflows/CI Workflow (ci.yml
)
ci.ymlCI工作流(ci.yml
)
ci.ymlTo set up continuous integration:
- Configure triggers for push to main/master and pull requests
- Set up job matrix for multiple Node.js versions if needed
- Add checkout, dependency installation with caching
- Run linting (ESLint, Prettier, type checking)
- Execute test suites with coverage reporting
- Build the application to verify no build errors
- Upload build artifacts for deployment jobs
要搭建持续集成:
- 配置推送至main/master分支及拉取请求的触发规则
- 若需要,为多版本Node.js设置任务矩阵
- 添加代码拉取、带缓存的依赖安装步骤
- 运行代码检查(ESLint、Prettier、类型检查)
- 执行测试套件并生成覆盖率报告
- 构建应用以验证无构建错误
- 上传构建产物供部署任务使用
Deploy Workflow (deploy.yml
)
deploy.yml部署工作流(deploy.yml
)
deploy.ymlTo set up continuous deployment:
- Trigger on push to main/master branch
- Download build artifacts from CI workflow
- Deploy to target environment (Vercel, Netlify, AWS, etc.)
- Set environment variables and secrets
- Notify on deployment success/failure
要搭建持续部署:
- 设置触发规则为推送至main/master分支
- 从CI工作流中下载构建产物
- 部署至目标环境(Vercel、Netlify、AWS等)
- 配置环境变量与密钥
- 发送部署成功/失败通知
Preview Deployment (preview.yml
)
preview.yml预览部署(preview.yml
)
preview.ymlTo set up preview deployments for pull requests:
- Trigger on pull_request events
- Build and deploy to preview environment
- Generate unique preview URL
- Comment preview URL on pull request using GitHub API
- Clean up preview deployments when PR is closed
要为拉取请求设置预览部署:
- 设置触发规则为pull_request事件
- 构建并部署至预览环境
- 生成唯一的预览URL
- 使用GitHub API在拉取请求中评论预览URL
- 当PR关闭时清理预览部署资源
Caching Strategy
缓存策略
To optimize workflow performance:
- Cache with dependency lock file as cache key
node_modules - Cache Next.js build cache () for faster builds
.next/cache - Cache testing artifacts and coverage reports
- Use with appropriate cache keys
actions/cache@v3 - Implement cache restoration fallbacks for partial matches
要优化工作流性能:
- 以依赖锁文件为缓存键,缓存
node_modules - 缓存Next.js构建缓存()以加快构建速度
.next/cache - 缓存测试产物与覆盖率报告
- 使用并配置合适的缓存键
actions/cache@v3 - 为部分匹配的缓存实现回退恢复逻辑
Resources
资源
Consult for workflow optimization patterns and security best practices.
references/github-actions-best-practices.mdUse to scaffold workflow files based on detected project configuration.
scripts/generate_ci_workflow.pyReference for starter templates for common frameworks (Next.js, React, Node.js).
assets/workflow-templates/参考获取工作流优化模式与安全最佳实践。
references/github-actions-best-practices.md使用根据检测到的项目配置生成工作流文件模板。
scripts/generate_ci_workflow.py参考获取常见框架(Next.js、React、Node.js)的入门模板。
assets/workflow-templates/Implementation Steps
实施步骤
To implement complete CI/CD with GitHub Actions:
-
Detect Project Configuration
- Identify framework (Next.js, Vite, CRA, etc.)
- Detect package manager (npm, yarn, pnpm)
- Find test runner (Jest, Vitest, Playwright)
- Check for linting configuration
-
Generate Workflow Files
- Use with detected configuration
scripts/generate_ci_workflow.py - Customize jobs based on project requirements
- Add matrix builds if testing multiple environments
- Use
-
Configure Secrets and Variables
- Document required secrets in README or workflow comments
- Set up environment-specific variables
- Configure deployment credentials
-
Add Preview Deployments
- Generate preview workflow with URL commenting
- Configure preview environment provider
- Set up cleanup automation
-
Optimize Caching
- Implement multi-level caching strategy
- Use cache keys based on lock files
- Add cache restoration logic
-
Test and Validate
- Push workflows to repository
- Create test PR to verify preview deployments
- Check workflow execution times and optimize
要使用GitHub Actions搭建完整的CI/CD:
-
检测项目配置
- 识别框架(Next.js、Vite、CRA等)
- 检测包管理器(npm、yarn、pnpm)
- 确定测试运行器(Jest、Vitest、Playwright)
- 检查代码检查配置
-
生成工作流文件
- 结合检测到的配置,使用
scripts/generate_ci_workflow.py - 根据项目需求自定义任务
- 若需测试多环境,添加矩阵构建
- 结合检测到的配置,使用
-
配置密钥与变量
- 在README或工作流注释中记录所需密钥
- 设置环境专属变量
- 配置部署凭证
-
添加预览部署
- 生成带URL评论功能的预览工作流
- 配置预览环境提供商
- 设置清理自动化
-
优化缓存
- 实现多级缓存策略
- 使用基于锁文件的缓存键
- 添加缓存恢复逻辑
-
测试与验证
- 将工作流推送至仓库
- 创建测试PR以验证预览部署
- 检查工作流执行时间并优化
Advanced Features
高级功能
To add advanced CI/CD capabilities:
- Parallel Jobs: Split test suites across multiple jobs for faster execution
- Conditional Deployments: Deploy only specific paths or when conditions are met
- Status Checks: Require CI passing before merge
- Deployment Gates: Add manual approval steps for production deployments
- Performance Monitoring: Integrate Lighthouse CI or bundle analysis
- Dependency Updates: Automate dependency updates with Dependabot integration
要添加高级CI/CD能力:
- 并行任务:将测试套件拆分至多个任务以加快执行速度
- 条件部署:仅在特定路径或满足条件时执行部署
- 状态检查:要求CI通过后才能合并
- 部署闸门:为生产部署添加手动审批步骤
- 性能监控:集成Lighthouse CI或包体积分析
- 依赖更新:通过Dependabot集成实现依赖自动更新
Framework-Specific Configuration
框架专属配置
Next.js Applications
Next.js应用
To optimize for Next.js:
- Cache directory for faster builds
.next/cache - Set to disable telemetry
NEXT_TELEMETRY_DISABLED=1 - Use and
next buildfor static exportsnext export - Configure ISR revalidation for preview deployments
针对Next.js优化:
- 缓存目录以加快构建速度
.next/cache - 设置以禁用遥测
NEXT_TELEMETRY_DISABLED=1 - 使用与
next build生成静态导出文件next export - 为预览部署配置ISR重新验证
Full-Stack Applications
全栈应用
To handle full-stack deployments:
- Set up separate jobs for frontend and backend
- Configure database migrations in deployment workflow
- Run integration tests against preview environment
- Coordinate deployment order (backend first, then frontend)
处理全栈部署:
- 为前端与后端设置独立任务
- 在部署工作流中配置数据库迁移
- 针对预览环境运行集成测试
- 协调部署顺序(先部署后端,再部署前端)
Deployment Providers
部署提供商
Consult for platform-specific configuration:
references/deployment-providers.md- Vercel: Use vercel-action with project configuration
- Netlify: Use netlify-cli with site ID and auth token
- AWS: Configure S3/CloudFront or ECS deployment
- Docker: Build and push container images to registry
- Self-Hosted: SSH deployment with rsync or git pull
参考获取平台专属配置:
references/deployment-providers.md- Vercel:使用vercel-action并配置项目信息
- Netlify:使用netlify-cli并提供站点ID与认证令牌
- AWS:配置S3/CloudFront或ECS部署
- Docker:构建并推送容器镜像至镜像仓库
- 自托管:通过SSH使用rsync或git pull进行部署
Monitoring and Notifications
监控与通知
To add monitoring and notifications:
- Configure Slack/Discord webhooks for deployment notifications
- Add GitHub status checks for required CI jobs
- Set up error tracking integration (Sentry, etc.)
- Monitor workflow execution times and optimize slow jobs
- Track deployment frequency and failure rates
添加监控与通知:
- 配置Slack/Discord webhook以接收部署通知
- 为必要的CI任务添加GitHub状态检查
- 集成错误追踪工具(如Sentry等)
- 监控工作流执行时间并优化慢任务
- 追踪部署频率与失败率
Troubleshooting
故障排除
Common issues and solutions:
- Cache Misses: Verify cache key includes all dependency files
- Timeout Errors: Increase timeout or split into parallel jobs
- Permission Errors: Check repository secrets and permissions
- Build Failures: Ensure environment variables are set correctly
- Preview URL Not Commented: Verify PR comment permissions and token scope
常见问题与解决方案:
- 缓存未命中:验证缓存键是否包含所有依赖文件
- 超时错误:增加超时时间或拆分为并行任务
- 权限错误:检查仓库密钥与权限
- 构建失败:确保环境变量设置正确
- 预览URL未评论:验证PR评论权限与令牌范围