You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS多阶段流水线改造:添加手动审批并基于构建阶段动态配置S3目标存储桶

Solution for Multi-Environment Deployment with Manual Approval

Here's the modified buildspec.yaml that implements your requirements:

version: 0.2
env:
  variables:
    # Configure these values in your CodeBuild project or Pipeline stage variables (use secure variables for prod if needed)
    S3_BUCKET_DEV: "s3-frontend-dev"
    S3_BUCKET_PROD: "s3-frontend-prod"
phases:
  install:
    runtime-versions:
      nodejs: 10
  pre_build:
    commands:
      - echo Installing source NPM dependencies
      - cd project
      - npm install
      - npm install -g @angular/cli@"~9.1.1"
      # Dynamically set target bucket based on the current pipeline stage
      - echo "Identifying target environment for stage: $CODEBUILD_BUILD_STAGE"
      - if [ "$CODEBUILD_BUILD_STAGE" = "prod" ]; then 
          export TARGET_BUCKET="$S3_BUCKET_PROD"; 
        else 
          export TARGET_BUCKET="$S3_BUCKET_DEV"; 
        fi
      - echo "Target deployment bucket: $TARGET_BUCKET"
  build:
    commands:
      - echo Build started on `date`
      - export NODE_OPTIONS=--max_old_space_size=16384
      - ng build --aot --configuration=production --build-optimizer --stats-json --output-path=dist/en
  post_build:
    commands:
      - bash -c "if [ \"$CODEBUILD_BUILD_SUCCEEDING\" == \"0\" ]; then exit 1; fi"
      - echo Clearing existing content in target bucket
      - aws s3 rm s3://$TARGET_BUCKET --recursive
      - echo Deploying build artifacts to target bucket
      - aws s3 cp dist/en s3://$TARGET_BUCKET --recursive
artifacts:
  files:
    - '**/*'
cache:
  paths:
    - '/root/.m2/**/*'
    - '/root/.npm/**/*'
    - 'build/**/*'
    - 'node_modules/**/*'

Key Changes Explained:

  • Environment Bucket Configuration: Added S3_BUCKET_DEV and S3_BUCKET_PROD to explicitly define your two deployment environments. For better security, you can move these values to your CodeBuild project's secure environment variables instead of hardcoding them here.
  • Dynamic Bucket Selection: In the pre_build phase, we check the CODEBUILD_BUILD_STAGE variable to set the TARGET_BUCKET dynamically. This ensures your deployment goes to the correct environment based on which pipeline stage is executing.
  • Updated Deployment Logic: Replaced all references to the old static S3_BUCKET variable with the dynamic TARGET_BUCKET in the post_build phase, so artifacts are pushed to the right bucket every time.

Quick Pipeline Setup Reminder:

Ensure your CodePipeline is structured to support this flow:

  1. A dev stage that runs this buildspec automatically on code commits (no approval needed).
  2. A manual approval stage immediately after the dev deployment (stops the pipeline until someone approves the prod push).
  3. A prod stage that triggers this buildspec again — CodePipeline will automatically pass the stage name to CODEBUILD_BUILD_STAGE as "prod" if your stage is named correctly, but double-check your pipeline configuration to confirm the variable is being passed.

内容的提问来源于stack exchange,提问作者bhamichi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 09:12:36