Skip to content
返回部署

使用 AWS CodeBuild 部署

AWS 全托管 CI 服务,按用量付费,支持 VPC。

CI/CD进阶

为什么

CodeBuild 原生集成 CodePipeline、CodeCommit 和 IAM,无需管理基础设施。DORA 2024 DevOps 状态报告显示精英团队部署频率比低绩效团队高 973 倍。

带自动化测试的 CI/CD 管道将变更失败率降至 5-15%,而手动部署为 40-60%。

何时使用

以 AWS 为中心的团队,希望 CI 无需管理 Jenkins 或 GitHub Actions runner。贡献者少于 5 人的仓库避免复杂管道编排 — pre-commit 钩子 + GitHub Actions 足够。

除非需要 GPU 或 VPN 访问,否则跳过自托管 runner。

如何做

# 构建spec.YML version: 0.2 phases: install: runtime-versions: nodejs: 20 commands: - npm ci build: commands: - npm run build - npm test post_build: commands: - aws ecr get-login-password | docker login --username AWS --password-stdin $ECR_URI - docker build -t myapp . - docker tag myapp $ECR_URI/myapp:$CODEBUILD_BUILD_NUMBER - docker push $ECR_URI/myapp:$CODEBUILD_BUILD_NUMBER artifacts: files: - '**/*' base-directory: dist 4. 验证部署 — curl -I https://your-app.example.com 5. 监控日志 — 前 5 分钟检查错误 6. 需要时回滚 — 回退到上一版本

示例

# Code构建 with VPC for 私有 re源s { "vpcConfig": { "vpcId": "vpc-xxx", "subnets": ["subnet-xxx"], "securityGroupIds": ["sg-xxx"] }, "environment": { "type": "LINUX_CONTAINER", "image": "aws/codebuild/standard:7.0" } }

常见问题

  • ⚠VPC 构建无法拉镜像 — 检查 NAT 网关或 VPC endpoint
  • ⚠IAM 拒绝 — 给 service role 加 codebuild:BatchGetBuilds
  • ⚠环境间配置漂移 — 使用基础设施即代码(Terraform/Pulumi)保持 dev/staging/prod 一致

最佳实践

  • ✓用构建缓存 — S3 或 LOCAL 复用依赖
  • ✓用 CodeBuild Batch — 并行化独立测试套件
  • ✓设置健康检查和优雅关闭 — 处理 SIGTERM 以在退出前排空进行中的请求

常见问题解答