🎉 Announcing new lower pricing — up to 40% lower costs for Cloud Servers and Cloud SQL! Read more →

Building Docker images with GitHub Actions

Here’s how to automatically build docker images and push them to the Brightbox Container Registry using a GitHub Action.

Setup the container repository

First you’ll need to create a container repository, which you can do with our Control Panel.

Then click Add Access Rule and create a new API Client and grant it Push + Pull privileges. You’ll be given the API Client credentials which we’ll have GitHub use to log in.

You never want to commit any sensitive data such as credentials to a GitHub repository, so we’ll set these as GitHub repository secrets. In the settings on your GitHub code repository, click Secrets and variables and then Actions. Create three secrets, one called CR_ACCOUNT with your account identifier (acc-xxxxx) and then CR_USERNAME and CR_SECRET with the API Client credentials.

Setup the action

In your code repository, create a file named .github/workflows/build.yaml with the following content:

name: 'Build and Push'
on:
  push:
    branches:
      - '*'
    tags:
      - '*'
  pull_request:
    branches:
      - '*'
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      - name: Cache Docker layers
        uses: actions/cache@v3
        with:
          path: /tmp/.buildx-cache
          key: buildx
      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            cr.brightbox.com/${{ secrets.CR_ACCOUNT }}/myapp/myapp
          tags: |
            type=ref,event=branch
            type=ref,event=tag
            type=ref,event=pr
      - name: Login to Brightbox Container Registry
        uses: docker/login-action@v2
        with:
          registry: cr.brightbox.com
          username: ${{ secrets.CR_USERNAME }}
          password: ${{ secrets.CR_SECRET }}
      - name: Docker build and push
        uses: docker/build-push-action@v3
        with:
          context: .
          file: ./Dockerfile
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          push: true
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache

Edit the images attribute to specify the container registry that you created and the name of your image (but don’t add an image tag here!).

This will build the container image on every kind of git push (branches, tags, pull requests) and push the resulting image(s) to the Brightbox registry. The images will be tagged according to the branch and tag name, or the pull request number.

So if you push to branch develop the image will be tagged myapp:develop. Push to tag v1.5.0 you’ll get myapp:v1.5.0. Create pull request number 8 and you’ll get myapp:pr-8. This can be fully customized and it’s easy to use a date stamp instead, enforce semver versioning or even extract out substrings with regular expressions. See the docker/metadata-action for more details.

This action also uses GitHub action caching, so successive builds will take less time if any of the Docker layers have previously been built, even though the build environment is discarded when the action finishes.

More to come

This is obviously completely customisable. For example, we usually run a test suite, only push the image when a GitHub release is created and automatically trigger deployments (usually with ArgoCD running on Kubernetes). More on that in a future post but if you want help in the mean time, contact us.

Try Brightbox for free!

If you want to try out the Brightbox container registry, you can sign up in just a couple of minutes and get a £50 free credit to try us out.

Get started with Brightbox Sign up takes just two minutes...