Blog

A ready-to-use Github action workflow for pushing container images to the registry

08 Jul, 2022
Xebia Background Header Wave

In the blog I will present you a ready-to-use Github action workflow to push container images to the image registry. In modern software development, we depend on container images a lot. As Github now has its own container image registry – ghcr.io too, it becomes easy to build and push images directly to a registry within a single workflow.

Github Action Workflow

Below you see the ready-to-use Github action workflow to push your container image to the Github container registry ghcr.io:

name: Build and publish the container image

"on":
  push:
    tags:
      - '*'
    branches:
      - 'main'

jobs:
  build-and-push-image:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      -
        name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      -
        name: Get tag
        id: repository
        run: echo "tag=$(git describe --tags HEAD)" > $GITHUB_ENV

      -
        name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      -
        name: Log in to the Container registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      -
        name: Build and push
        uses: docker/build-push-action@v3
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ghcr.io/${{ github.repository }}:${{ env.tag }}

As you can see, it will work on any repository. If you have a git repository for a container image, just add this workflow.

  • it pushes into the container registry corresponding with the repository
  • it uses git describe --tags HEAD to generate a semver tag for the image
  • it builds a multi-architecture image for amd64 and arm64
  • it runs on every push to the main branch, or tag of a commit

Pretty neat, huh?

install the workflow

To install the workflow, type the following commands:

# add the workflow
$ mkdir -p .github/workflows/
$ curl -sS -L -o .github/workflows/push-to-registry.yaml 
     https://raw.githubusercontent.com/binxio/push-to-registry-gh-action-workflow/main/.github/workflows/push-to-registry.yaml
$ git add .
$ git commit -m 'added push to ghcr.io workflow'

To make a beautiful semantic version release of your image, type:

$ read NEW_SEMVER
$ git tag $NEW_SEMVER
$ git push --tags
$ git push

Checkout the resulting Github action workflow!

Conclusion

With the availability of the Github container image registry, this simple ready-to-use workflow make Github a one-stop-shop to build and distribute your container images!

Photo by Jakob Owens on Unsplash

Mark van Holsteijn
Mark van Holsteijn is a senior software systems architect at Xebia Cloud-native solutions. He is passionate about removing waste in the software delivery process and keeping things clear and simple.
Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts