Adding CodeBuild support to your application repository

Whenever you create a CodeBuild project, you must define how CodeBuild should test and build your application source code, and then publish application artifacts and/or Docker images. CodeBuild defines these tasks within a build specification, which provides the build instructions the CodeBuild agent should execute when running a build.

CodeBuild allows you to provide a build specification in several ways:

In general, I recommend using the self-defined method, as it allows the repository owner (typically, your developers) to configure and maintain the specification independently of CodeBuild; this is the approach we will take.

The following example demonstrates adding a build specification to the todobackend repository, in a file called buildspec.yml

version: 0.2

phases:
pre_build:
commands:
- nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --storage-driver=overlay&
- timeout -t 15 sh -c "until docker info; do echo .; sleep 1; done"
- export BUILD_ID=$(echo $CODEBUILD_BUILD_ID | sed 's/^[^:]*://g')
- export APP_VERSION=$CODEBUILD_RESOLVED_SOURCE_VERSION.$BUILD_ID
- make login
build:
commands:
- make test
- make release
- make publish
post_build:
commands:
- make clean
- make logout

The build specification starts by specifying a version that must be included in every build specification, the most current version being 0.2, as of the writing of this book. Next, you define the phases sequence, which is required, defining the commands that CodeBuild will run during the various phases of the build.  In the previous example, you define three phases:

You can find more information about the CodeBuild build specifications at https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html.

During the pre_build stage, you perform the following actions:

Once the pre_build stage has completed, the build stage is straightforward, and simply executes the various build steps that we have executed manually so far in this book. The final post_build stage runs the make clean task to tear down the Docker Compose environment, and then removes any local ECR credentials by running the make logout command.

One important point to note is that the post_build stage always runs, even if the build stage fails. This means you should only reserve post_build tasks for actions that you would run regardless of whether the build passes or fails. For example, you might be tempted to run the make publish task as a post_build step; however, if you do this, and the previous build stage fails, CodeBuild will still attempt to run the make publish task, given that it is defined as a post_build step. Placing the make publish task as the final action in the build stage ensures that if make test or make release fails, the build stage will immediately exit with an error, bypassing the make publish action and  proceeding to execute the cleanup tasks in the post_build step.

You can find out more about all of the CodeBuild phases, and whether they execute on success/failure, at https://docs.aws.amazon.com/codebuild/latest/userguide/view-build-details.html#view-build-details-phases.

The final step that you need to perform is to commit and push your changes to your Git repository, so that the newly created buildspec.yml file will be available when you configure CodePipeline and CodeBuild:

> git add -A
> git commit -a -m "Add build specification"
[master ab7ac16] Add build specification
1 file changed, 19 insertions(+)
create mode 100644 buildspec.yml
> git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 584 bytes | 584.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:docker-in-aws/todobackend.git
5fdbe62..ab7ac16 master -> master