How to build & Publish on Dockerhub using Github Action?

Reading Time: 4 minutes

In this Blog, We will look into how to Configure Github Actions to build a docker file and push the image to Dockerhub. If you are new to GitHub actions and you can check out this Blog where I have shown you how you can build and test a Java-Maven Project using Github Actions.

Prerequisite

  1. A project in any language with Dockerfile
  2. You must have knowledge of secrets in Github Actions.
  3. You should be familiar with Yaml for Github Actions

Here in My Case, I have taken a Java Project and I have already written a Dockerfile for this.

Now next thing we need is your docker hub Credentials that we will pass to repository Secrets. So that we can use those secrets while running our Github Actions.

To add the credentials as Secret

  1. Navigate to your GitHub repository and click Settings > Secrets > New secret.
  2. Create a new secret with the name DOCKER_USERNAME and your Docker ID as value
  3. Create a new Personal Access Token (PAT). To create a new token, go to Docker Hub Settings and then click New Access Token.
  4. Note that a personal access token is similar to a password except you can have many tokens and revoke access to each one at any time.
  5. Now you can add this token in secrets as DOCKER_ACCESS_TOKEN

The above Screenshot showed how you can generate an access token. You can give any name and then check the permissions and then click on Generate. Let’s Finish adding secrets then we can move to the next Part. If you don’t Know about Secrets you can visit this blog.

We are done adding secrets in our Repository as you can see in the screenshot above. Now let us configure Github Actions.

Set-up Workflow

To set up the workflow:

  1. Go to your repository in GitHub and then click Actions > New workflow.
  2. Click set up a workflow yourself and add the following content:

First, we will name this workflow

name: Build-Publish

Then, we will choose when we run this workflow. In our example, we are going to do it for every push against the master branch.

on:
  push:
    branches:
      - 'master'

Then we will specify the jobs and add where we want the build to take place. In this example, we will build on the Ubuntu machine.

jobs:
  build:
    runs-on: ubuntu-latest

After this, we can add other steps such as Checkout and login to docker hub using Personal Access token and lastly setup a Build to create the Builder Instance using a Buildkit Container

 steps:
      -
        name: Checkout 
        uses: actions/checkout@v2
      -
        name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ secrets.DOCKER_USERNAME }}/hellodocker:latest

This Workflow, as you can see will push the image to the docker hub with the latest tag.

Now let us commit the workflow file and you will notice the build has started in the actions tab.

Now if you logged into your Dockerhub, You will see the image has been pushed.

So This was all about Building and pushing a docker image to the docker hub with the Help of Github Actions.

Now suppose you don’t want to add the latest tag, Instead, you want to use the tag method where the build will take place whenever you add a tag. To do this we can follow these steps.

tags:
      - 'v*.*.*'
name: Docker Image CI

on:
  push:

  tags:
      - 'v*.*.*'
jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      -
        name: Checkout 
        uses: actions/checkout@v2
      -
        name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ secrets.DOCKER_USERNAME }}/hellodocker:${{github.ref_name }}

You can notice instead of the latest we replaced this with ${{ github.ref_name }}. Now to make this what we will do, we tag our commit with something like v1.0.0

This ensures that the CI will trigger your workflow on push events (branch and tags). If we tag our commit with something like v1.0.0:

git tag -a v1.0.0
 git push origin v1.0.0
Whenever you will add a tag and push the tag, it will trigger the action and the image will push with the tag you added. This was all about tagging the image where the build is triggered whenever there is a tag commit. There are many other methods of tagging also that you can explore on your own.

Conclusion

In this blog, we looked into Github Actions to build and push a Docker image to Dockerhub. If you liked this blog please comment and share. You can explore more about this like how we can add tagging on every push to Dockerhub. Thank you for sticking to the end.

Refererences

Link1 Link2

knoldus

Written by 

Passionate about Technology and always Interested to Upskill myself in new technology, Working in the field of DevOps