Automate Docker Image Building, Tagging, and Publishing With GitHub Actions

Hirusha Fernando
Stackademic
Published in
3 min readMar 27, 2024

--

Hi all, Let’s talk about how to build docker images, tagging them with semantic versioning and publish to Docker Hub using Github actions. After creating this workflow, simply push your tag to Github and docker image will be build and push to the Docker Hub repository with the version of your Github tag. You don’t need to build, tag and push docker image manually. Github action will do that for you.

Let’s Start

For this I use these Github actions

  1. Docker Metadata Action
  2. Build And Push Docker Images

My folder and file structure is like this. Dockerfile is in the project root.

MyApp
|__ app/
|__ Dockerfile

Now let’s create the workflow file

  1. Create a folder named .github in the project root
  2. Inside the .github folder, create another folder named workflows.
  3. Inside workflows folder, create a file named docker_workflow.yaml (this file can be named as you want. File extension should be .yaml)
  4. Go to your Github repo settings and create these repository secrets (Settings > Secrets And Variables > Actions > New repository secret. DOCKERHUB_USERNAME — Your Docker hub username. DOCKERHUB_TOKEN — Your Dockerhub token DH_REPO_NAME — Dockerhub repository name
  5. Copy and paste this code into tha .yaml file
#NAME
name: Build And Push Images to Dockerhub
#EVENT
on:
push:
tags:
- 'v*'
#JOBS
jobs:
build_docker_images:
name: Build Docker Image To Dockerhub
runs-on: [ubuntu-latest]
steps:
- name: Code Checkout
uses: actions/checkout@v3

- name: Extract Metadata
uses: docker/metadata-action@v5
id: meta
with:
images: |
${{secrets.DOCKERHUB_USERNAME}}/${{secrets.DH_REPO_NAME}}
tags: |
type=semver,pattern={{version}}

- name: Docker Login
uses: docker/login-action@v2
with:
username: ${{secrets.DOCKERHUB_USERNAME}}
password: ${{secrets.DOCKERHUB_TOKEN}}
logout: true

- name: Build And Push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}

6. Push this code into master branch.

How This Works?

When you want to build and push the Docker image to Docker Hub, simply create a tag with the version you need. This should be follow semantic versionning. For example, create a tag named v1.0.0 and push it.

git tag v1.0.0
git push origin --tags

Now the docker image will be build with the tags 1.0.0 , latest and push it into Docker Hub.

Workflow Breakdown

on:
push:
tags:
- 'v*'

This section tells when to run the workflow. According to this, when we push a tag with name starting from v, this workflow will run. If you want to use branches instead of tags, replace tags with branches.

- name: Extract Metadata
uses: docker/metadata-action@v5
id: meta
with:
images: |
${{secrets.DOCKERHUB_USERNAME}}/${{secrets.DH_REPO_NAME}}
tags: |
type=semver,pattern={{version}}

Here I use a push event to trigger this workflow. Docker Metadata Action can extract metadata from push events. From the push event it will get the tag version. Under images, we give a list of docker image names to use. Also I have given an id for this. It will be used in the next section. In the Tags section, we specify how the docker image tagging should happen. Here I used semantic versioning. You can use, branch names, commit hash and etc. You can find more details on their documentation page.

- name: Build And Push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}

In this section, Docker images will be pushed into the Docker Hub repository with the tag. The context is where the Dockerfile is located. Docker images tag is mentioned in the tags section. We get the tag from an earlier meta section. That’s why I gave an id, meta. Here, I used that id to get the tag.

That’s all for now. I hope this article will help you. See you from another article. Happy Coding! If you found this article useful, please tap the “👏” button.

Stackademic 🎓

Thank you for reading until the end. Before you go:

--

--