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 - 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

name: Build and publish the container image

      - '*'
      - 'main'

    runs-on: ubuntu-latest
      contents: read
      packages: write
        name: Checkout repository
        uses: actions/checkout@v3
          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
          username: ${{ }}
          password: ${{ secrets.GITHUB_TOKEN }}

        name: Build and push
        uses: docker/build-push-action@v3
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags:${{ 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 \
$ git add .
$ git commit -m 'added push to workflow'

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

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

Checkout the resulting Github action workflow!


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 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.

Explore related posts