Kevin W. McConnell

Using recent Go versions on CodeBuild

CodeBuild is a simple and reliable platform for building CI/CD pipelines. I use it a lot with Go projects, and one snag I’ve run into is that the supplied base images often take a long time to be updated to include new Go releases. In order to use features added in later Go versions, the build environment needs to be updated.

There are a couple of ways to do this. One is to build a custom Docker image to use in place of the provided ones. This works fine, and has the advantage that you can install anything else you want in the image. However it also means you now have to maintain that image, applying security patches and updates when necessary.

The other way is to stick with one of the provided, managed images, but update the Go version that it contains. Handily enough, the codebuild/standard images already contain goenv, which makes adding new Go versions easy.

To install a specific Go version, you’ll want to add a couple of lines to your buildspec.yml that will do two things:

This is how I’ve been doing it in my projects:

yaml
phases:
install:
commands:
# Make sure goenv is up to date
- cd $HOME/.goenv && git pull --ff-only && cd -
# Install Go 1.17
- goenv install 1.17 && goenv global 1.17
...

I’ve found this usually only adds a few seconds to the build, which seems a small price to pay for the convenience of not having to manage a custom image.

Posted August 18, 2021.