Pushing Cutom Images to Docker Hub using GitHub Actions
Tags: githubaction, docker, dockerhub, container
Published: 2022-09-30 22:18:59

In last [article](https://dev.to/nitin27may/pushing-custom-docker-image-to-docker-hub-5ado), we saw that how to push custom image on Docker hub using docker cli, In this article we are going to see how we can automat this using GitHub Actions. Please keep this article link handy as we will be referring it for few key infomration.

First, we need an application source code which we have already dockerized, in this case we will take a simple example, Here is the link of the [Git Repo](https://github.com/nitin27may/docker-images-example). In this project, we have just two file which have been used. One is 'index.html' and another is 'Dockerfile'. Assumtion is that we already know how to build and run it locally.

Now, We will keep our docker account details : username and access token handy (Explained in same article).
We will go to our github repository, Settings > Secrets > Actions and click on 'New repository secret'.

![New repository secret](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tks6jyd7ypi9dk71hvjh.png)
It will open a new window, where we have to add key and value for our docker account access token (in our case, we name it, DOCKERHUB_TOKEN). We also addeed docker hub user name (DOCKERHUB_USERNAME) as secret.

![Add new](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8idoqtqqv0m42s1gxfz.png)
Now let's add our first GitHub Action at root of our project like below :

![VS Code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8rfkmzyjyve55ixhp0n.png)
Below is the full script.
{% embed  https://gist.github.com/nitin27may/5f7a0ab2f4cbcba76ac25128ef8478a7.js %}

In this script we are trigeering the build if any changes are pushed to master branch for all paths. We are using an build agent in this case it is ubunt 22.04

Ther very **first step** is [actions/checkout@v2](https://github.com/actions/checkout) action, which take the latest code from the master branch.

**Second step** is [docker/setup-qemu-action](https://github.com/docker/setup-qemu-action) which installs QEMU static binaries, which are used to run builders for architectures other than the host.

**Third step** is [docker/setup-buildx-action](https://github.com/docker/setup-buildx-action) configures [buildx](https://github.com/docker/buildx), which is a Docker CLI plugin that provides enhanced build capabilities.

**Fourth step** is login ([docker/login-action@v2](https://github.com/docker/login-action)) in to docker account, it is using username and token configured in the repository's action secret section. Then next step is building and pushing it to docker hub.

**Fifth step**, if you will see, we are tagging ([docker/build-push-action@v3](https://github.com/docker/build-push-action)) the image with 2 tags, one is the latest, so every image will pushed as latest and also as a Variable which is the Github Run Number, which is the number of build (GitHub Action)run for that repo. This way every build will create, it's own tag too and we can pull that particulat image from the dokcer hub.

Now on any code push to master branch it will trigger a build, and we haven't added any filter or condition. On pushing this script to master branch it will trigger a new build. In below screen shot, the yellow dot shows on GitHub repository if any build has been triggered.

![Build in Queue](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jum9655973do65epx2x.png)
The below screenshot is showing that how tags get update in any build.

![Build Run Details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9p91qm91k9s1mjryve2.png)
Below is the screenshot from a successfull run.

![Result at Repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7jfcvjm3klfalnulsv2d.png)
After, sucessfull push, we can see that it pushed 2 tag, one is latest and another is 5, which we can see that it was the build number.
This was a pretty straight forward example to create a GitHub Action to build and push image to Dokcer Hub. Hope this was helpful and thanks for reading.