Auto-publish Docker Images with Github Actions

Auto-publish Docker Images with Github Actions

October 22, 2021 – Tech – Written by @simoelalj

image

Since September 2021, Docker changed its business model, and No longer allows to auto-build Dockerfiles for free.

You can easily use to Github Action as a Continuous Deployment strategy for Docker images.

This script will also build both arm64 (works natively with Apple silicon macs) and amd64 images!

First, head to the "Action" tab on your Github project

image

And create the following workflow:

name: Docker Image CI

on:
  release:
    types: [published]

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Get the version
        id: vars
        run: echo ::set-output name=tag::$(echo ${GITHUB_REF:10})
      -
        name: Checkout
        uses: actions/[email protected]
      -
        name: Set up QEMU
        uses: docker/setup-qemu-[email protected]
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-[email protected]
      -
        name: Login to DockerHub
        uses: docker/login-[email protected] 
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}
      -
        name: Build and push
        uses: docker/build-push-[email protected]
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ github.repository }}:latest,${{ github.repository }}:${{steps.vars.outputs.tag}}

On your settings, set the secrets for DOCKERHUB_USERNAME and DOCKERHUB_PASSWORD

image

You will need to create a Docker hub token that will be used as a password:

image

And now, as soon as you tag a new release on Github, it will auto-build the image, tag it and publish it :)

This article is brought you by tonoïd – we are a micro-startup studio building small businesses that are profitable and solve a specific problem without any external funding nor billion-dollar market-size. Most notably, RefurbMe, a comparison site for refurbished products – and Notion Automations.

If you have any feedback, do not hesitate to reach out at [email protected]