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_DEVandS3_BUCKET_PRODto 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_buildphase, we check theCODEBUILD_BUILD_STAGEvariable to set theTARGET_BUCKETdynamically. 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_BUCKETvariable with the dynamicTARGET_BUCKETin thepost_buildphase, so artifacts are pushed to the right bucket every time.
Quick Pipeline Setup Reminder:
Ensure your CodePipeline is structured to support this flow:
- A
devstage that runs this buildspec automatically on code commits (no approval needed). - A manual approval stage immediately after the
devdeployment (stops the pipeline until someone approves the prod push). - A
prodstage that triggers this buildspec again — CodePipeline will automatically pass the stage name toCODEBUILD_BUILD_STAGEas "prod" if your stage is named correctly, but double-check your pipeline configuration to confirm the variable is being passed.
内容的提问来源于stack exchange,提问作者bhamichi
相关产品推荐
相关产品推荐

