Auto-publish Docker Images with Github Actions

Auto-publish Docker Images with Github Actions

September 6, 2021

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.

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:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Login to DockerHub Registry
        run: echo ${{ secrets.DOCKERHUB_PASSWORD }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
      - name: Get the version
        id: vars
        run: echo ::set-output name=tag::$(echo ${GITHUB_REF:10})
      - name: Build the tagged Docker image
        run: docker build . --file Dockerfile --tag ${{ github.repository }}:${{steps.vars.outputs.tag}}
      - name: Push the tagged Docker image
        run: docker push ${{ github.repository }}:${{steps.vars.outputs.tag}}
      - name: Build the latest Docker image
        run: docker build . --file Dockerfile --tag ${{ github.repository }}:latest
      - name: Push the latest Docker image
        run: docker push ${{ github.repository }}:latest

On your settings, set the secrets:

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