如何在CodePipeline中自动将CodeBuild产物上传至S3存储桶?
Great questions! Let's break this down into two clear solutions that avoid the hassle of extra CodeBuild projects, plus address your requirement to upload test results regardless of prior step failures.
1. Automate Uploading CodeBuild Artifacts to S3 (No Extra CodeBuild Needed)
You have two clean, native AWS ways to do this without writing custom S3 CLI scripts in a new project:
Option 1: Use CodePipeline's Built-In S3 Deploy Stage
CodePipeline integrates directly with S3 to deploy artifacts, so you can add this as a dedicated stage in your pipeline:
- Open your CodePipeline in the AWS Console and click Edit to enter pipeline configuration mode.
- Add a new stage (name it something like
UploadTestResultsToS3) after your CodeBuild stage. - Inside this stage, add a Deploy action with the provider set to Amazon S3.
- Configure the action:
- Select your target S3 bucket.
- Set an object key prefix (e.g.,
test-results/${CODEBUILD_BUILD_ID}/) to organize results by build ID. - Check Overwrite existing objects if you want to replace old results for the same build.
- Choose the output artifact from your CodeBuild stage (your
test-resultbundle) as the input for this action.
- Save the pipeline. From now on, every time your CodeBuild runs, the pipeline will automatically sync the artifacts to your S3 bucket.
Option 2: Upload Directly from CodeBuild (Keep Pipeline Artifacts Too)
If you prefer to handle the upload during the CodeBuild process (while still passing artifacts to CodePipeline), modify your buildspec.yml to add an S3 sync in the post_build phase:
version: 0.2 phases: build: commands: # Your existing build/test commands go here (e.g., mvn test, npm test) post_build: alwaysRun: true # Critical: Ensures this phase runs even if build/test fails commands: # Sync your test results directory to S3 - replace with your bucket and path - aws s3 sync ./path-to-test-results s3://your-test-bucket/test-results/${CODEBUILD_BUILD_ID}/ artifacts: name: test-result files: - '**/*' - '*'
Just make sure your CodeBuild service role has the s3:PutObject permission for your target S3 bucket.
2. Upload Test Results Even If Prior Steps Fail
This depends on which upload method you choose:
If Using CodePipeline's S3 Deploy Stage
By default, CodePipeline stops execution if a prior stage fails. To bypass this:
- In pipeline edit mode, find your CodeBuild action (the one that generates test results).
- Click Edit on the action, then expand the Advanced section.
- Uncheck the box for Fail the stage if this action fails.
- Save the pipeline. Now, even if your CodeBuild stage fails (e.g., tests don't pass), the pipeline will still run the S3 upload stage to preserve your test results.
If Uploading Directly from CodeBuild
The alwaysRun: true flag in the post_build phase we added earlier ensures this phase executes regardless of whether the build phase succeeds or fails. This guarantees your test results get uploaded to S3 every time, no matter what happens during the build/test steps.
Bonus tip: If your test results are in a structured format (like JUnit XML), you can also use CodeBuild's reports feature to send results to the AWS Console for easy viewing, while still syncing raw files to S3.
内容的提问来源于stack exchange,提问作者Joey Yi Zhao

