CodeBuild buildspec phases, finally blocks and env variables
A buildspec has four phases: install, pre_build, build and post_build. The CodeBuild User Guide says a finally block in a phase runs even when a command in that phase fails. For an environment variable defined in several places, the start-build value wins, then the project definition, then the buildspec.
What is each buildspec phase for?
AWS recommends install only for installing packages in the build environment, such as a test framework. pre_build holds commands that run before the build, for example signing in to Amazon ECR or installing npm dependencies. build runs the build or test tools. post_build runs after the build, for example packaging artifacts, pushing a Docker image to Amazon ECR or sending a notification through Amazon SNS.
By default the file is named buildspec.yml and sits in the root of the source directory. A project can name a different file with its buildspec setting, and a single build can do so with buildspecOverride on start-build. Both paths are relative to CODEBUILD_SRC_DIR. The file can also be in an S3 bucket in the same Region as the project. A build project has only one buildspec.
How do finally and on-failure change a failed phase?
When a command in a phase's commands list fails, CodeBuild skips the rest of that list and then runs the phase's finally block. A phase succeeds only when every command in both commands and finally succeeds. A cleanup command in finally therefore always runs, and the phase is still reported as failed.
on-failure sets what happens after a phase fails: ABORT stops the build, CONTINUE moves to the next phase, and the RETRY forms retry the build, up to 3 times by default or a count from 0 to 100, optionally only when the error matches a regular expression. on-failure is not supported on Lambda compute or reserved capacity.
Which value does an environment variable get?
A variable with the same name can be set in three places. The value passed in the start build call has the highest precedence. The value in the build project definition comes next. The value declared in the buildspec has the lowest precedence. Any variable you set also replaces one of the same name that the Docker image already defines.
Plain-text variables can be displayed in the console and the AWS CLI, so AWS discourages using them for sensitive values. The alternatives are the PARAMETER_STORE and SECRETS_MANAGER variable types on the project, or the env/parameter-store and env/secrets-manager mappings in the buildspec. For these types the variable's value is the name of the parameter or secret, and CodeBuild substitutes the stored value at run time.
Reading from Parameter Store requires the ssm:GetParameters action on the CodeBuild service role. Names that start with CODEBUILD_ are reserved.
Where the exam catches people
Believing a buildspec value overrides the project's value: the buildspec has the lowest precedence. Start build beats the project, and the project beats the buildspec.
Believing a finally block rescues a failed phase: it runs after a failure, but the phase still fails if any command failed.
Believing the remaining commands in a phase still run after one fails: CodeBuild skips them and goes to finally.
Believing a PARAMETER_STORE variable works without extra IAM: the service role needs ssm:GetParameters.
Believing one project can hold several buildspecs: it holds one. Use buildspecOverride on the build to choose a different file.