AWS CodeBuild Buildspec
External
Internal
Overview
Example
version: 0.2
env:
variables:
DEPLOYMENT_STACK_CONFIG_FILE: "deployment-stack-config.json"
MY_LOCAL_VAR: "some value"
phases:
install:
commands:
- echo "'install' phase started on $(date)"
#
# check pre-conditions, like environment variable availability. The environment 'MY_VAR' tested
# below is supposed to be set by the invoking layer (CodeBuild driven by CodePipeline):
#
- if [ -z "${MY_VAR}" ]; then echo "required MY_VAR variable not set" 1>&2; exit 1; fi
build:
commands:
- echo "'build' phase started on $(date)"
post_build:
commands:
- echo "'post_build' phase started on $(date)"
- echo "{\"Parameters\":{\"MyConfigurationParameterA\":\"blue\", \"MyConfigurationParameterB\":\"red\"}}" > ./${DEPLOYMENT_STACK_CONFIG_FILE}
artifacts:
files:
- ${DEPLOYMENT_STACK_CONFIG_FILE}
Structure
version
env
variables
This section sets environment variables that will propagate to the container that performs the build:
env:
variables:
MY_VARIABLE: 'some value'
Note that if this build project is part of a CodePipeline pipeline, which is set up with a CloudFormation stack template, additional environment variables can be set at CodeBuild project/CloudFormation stack template level and passed to the build container, as shown here: CodePipeline-Driven CodeBuild Builds. The availability of such environment variable can be tested in the install phase of the build, as shown in the example, above.
Unfortunately, variables that refer other variables in the same block are not supported. The following will not work:
env:
variables:
A: "some value"
B: "not ${A}"
phases
install
commands
build
commands
post_build
commands
artifacts
If this build is driven by CodePipeline, the artifacts declared here are placed, as S3 objects, into the CodePipeline Build action's Output Artifact, which is an S3 "folder". For more details about CodePipeline output artifacts, see:
files
Examples
Verifying that an Environment Variable is Set
... phases: install: commands: - if [ -z "${AWS_REGION}" ]; then echo "AWS_REGION variable not set" 1>&2; exit 1; fi
Multi-Line Commands
Multiple Lines
TODO: test
... phases: ...: commands: - | if [ -z "${AWS_REGION}" ]; then echo "AWS_REGION variable not set" 1>&2; exit 1; fi else ... fi
Folded Line
TODO: test
... phases: ...: commands: - > my-command --flag1=blah --flag2=blah
Organizatorium
- An environment variable exported with the following command in an early phase (install) is visible to subsequent phases:
- export PIPELINE_RUN_ID=1
- I have encountered situations when scripts (gradlew) that come part as the source code tree and are set to be executable in GitHub lose their execution flag needed when they are to be run in the build image. The solution is to:
- chmod a+x ./gradlew - ./gradlew clean build