AWS Certified Developer – Associate
CodeBuild buildspec.yml phases, env and artifacts (DVA-C03)
The CodeBuild User Guide says a buildspec is a YAML file of build commands and settings. phases holds the commands for install, pre_build, build and post_build. env defines variables, including values read from Parameter Store and Secrets Manager. artifacts names the output files to upload, cache the paths to reuse, and reports the test result files.
What goes in each buildspec phase?
By default the file is named buildspec.yml and sits in the root of the source directory. A project uses one buildspec, but the name and location can be overridden, for example with buildspecOverride on start-build. version is required and AWS recommends 0.2. In version 0.1 each command runs in its own shell, so a cd or an exported variable does not carry to the next command.
phases is required. The allowed names are install, pre_build, build and post_build. AWS recommends install only for installing packages in the build environment, and it is where runtime-versions goes. pre_build is for steps such as signing in to Amazon ECR or installing npm dependencies. post_build is for steps such as packaging a JAR or pushing a Docker image to Amazon ECR.
Commands in a phase run one at a time in the order listed. If any command fails, the phase fails. A finally block runs after commands even when a command failed. on-failure sets what happens next: ABORT, CONTINUE or a RETRY form. on-failure is not supported on Lambda compute or reserved capacity.
How does env read secrets?
env/variables holds plain-text key and value pairs. AWS discourages putting sensitive values there, because environment variables can be displayed in plain text in the CodeBuild console and the AWS CLI.
env/parameter-store maps a local variable name to the name of a Systems Manager Parameter Store parameter. The CodeBuild service role must have the ssm:GetParameters action, or the build cannot read the value. env/secrets-manager maps a local variable name to secret-id:json-key:version-stage:version-id. Only the secret ID is required. Without a JSON key CodeBuild retrieves the whole secret text, and without a version it retrieves AWSCURRENT.
CodeBuild hides strings that come from Parameter Store and Secrets Manager in the build logs. The masking matches the exact stored value, so a value that the build Base64-encodes or changes in another way can still appear in the logs.
When the same variable is set in several places, the value on the start build call wins, then the build project definition, then the buildspec. env/exported-variables lists variables that CodePipeline passes to later stages. Parameter Store and Secrets Manager values and names that start with AWS_ cannot be exported.
Artifacts, cache and reports
artifacts tells CodeBuild where the build output is and how to prepare it for upload to the S3 output bucket. artifacts/files is required when the block is present. Paths are relative to the original build location, and '**/*' means all files recursively. The block can be left out when the build only pushes a Docker image to Amazon ECR or only runs unit tests.
base-directory names a top-level directory to collect from. The directory itself is not included in the artifact, only its files and subdirectories. discard-paths: yes flattens every file into one output directory. Example: with base-directory: dist and files: '**/*', the artifact root holds index.html and assets/, not a dist folder.
cache/paths lists the locations CodeBuild saves and restores between builds. A project can use Amazon S3 caching or local caching, and local caching has source, Docker layer and custom modes. cache/key is matched exactly, and up to five fallback-keys are matched by prefix.
reports maps a report group name or ARN to the test result files. The default file-format is JUNITXML. A project can have at most five report groups.
Where the exam catches people
- Believing a successful build always hands an artifact to the next pipeline stage: CodeBuild uploads only what the
artifactsblock lists underfiles. Acacheorreportsblock does not create a build output artifact. - Believing
env/parameter-storeworks with no extra permission: the CodeBuild service role needsssm:GetParameterson the parameter. - Believing a Base64-encoded token in
env/variablesis protected: plain-text variables can be displayed in the console and the AWS CLI. Useenv/secrets-managerorenv/parameter-store. - Believing
files: 'dist/**/*'puts the contents ofdistat the artifact root: thedistfolder stays in the paths unlessbase-directory: distis set. - Believing the buildspec value overrides the project setting: for a variable with the same name, the start build call wins, then the project, and the buildspec is last.