A developer is configuring a continuous integration pipeline using AWS CodeBuild to compile a Node.js application, run unit tests, and push the resulting container image to an Amazon Elastic Container Registry (ECR) repository. The developer needs to configure the build process to meet the following requirements:
* The unit tests must run during the build process. If they fail, the build must stop immediately and mark the build run as failed.
* A cleanup script must execute to remove temporary files, regardless of whether the unit tests succeed or fail.
* The Docker image must only be built and pushed to Amazon ECR if all unit tests pass.
Which configuration should the developer use to meet these requirements?
- Configure the buildspec file to run the unit tests in the build phase. Place the cleanup script in the post_build phase to run unconditionally. In the post_build phase, check the value of the CODEBUILD_BUILD_SUCCEEDING environment variable, and only build and push the Docker image if its value is 1.Answer
- BConfigure the buildspec file to run the unit tests in the pre_build phase. Use the on-failure: CONTINUE attribute for the test command so the build proceeds. In the build phase, build and push the Docker image, and run the cleanup script in a custom phase named cleanup at the end of the build.
- CConfigure the buildspec file to run unit tests in the build phase and the cleanup script in the post_build phase. To authenticate with Amazon ECR, store the ECR authorization token in Systems Manager Parameter Store with automatic rotation enabled, and reference it under the env: parameter-store block of the buildspec file.
- DConfigure the buildspec file to run the unit tests in the build phase, and place the ECR push commands in the post_build phase. To allow CodeBuild to authenticate, update the trust policy of the CodeBuild service role to allow the ecr.amazonaws.com service principal to assume the role, and attach a policy allowing ecr:GetAuthorizationToken to the role.
Answer
Configure the buildspec file to run the unit tests in the build phase, place the cleanup script in the post_build phase to run unconditionally, and check the CODEBUILD_BUILD_SUCCEEDING environment variable in the post_build phase before building and pushing the Docker image.
The correct configuration uses the build phase to run the tests and the post_build phase to run the cleanup script unconditionally. By checking the value of the CODEBUILD_BUILD_SUCCEEDING environment variable in the post_build phase, the developer can conditionally build and push the Docker image only if all previous phases succeeded.
Step-by-Step Solution
Key Concept
AWS CodeBuild buildspec phases, environment variables, and execution behavior.