How to build your Dockerhub Images with Github Actions
Dockerhub recently removed the autobuild feature for all free users. This means that every docker image that relies on docker hub to auto build the images on new pushes or new releases will not be updated anymore. Users now need to build those images manually and push them to Docker Hub.
Thankfully you can use Github Actions to build the images for you and push them to the docker hub registry.
To get started you need to create a new folder in your project root called .github/workflows
. Inside this folder place the following file named dockerhub.yml
.
name: Build Docker Images
on:
push:
branches:
- master
workflow_dispatch:
schedule:
- cron: '0 0 * * *'
jobs:
dockerhub:
runs-on: ubuntu-latest
steps:
- name: checkout sources
uses: actions/[email protected]
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to Docker Hub
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v2
with:
push: true
tags: USERNAME/REPO:TAG
Please adjust the USERNAME
, REPO
and TAG
to your needs. This is also just a basic example that only pushes one tag but the script can be extended to your needs. This script is configured to run on every push to the master
branch (change this if you renamed your branch), daily at 00:00 and also manually via workflow_dispatch
.
To keep the dependencies in this script up to date we create the file .github/dependabot.yml
with the following content:
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions every weekday
interval: "daily"
This tells github to check the used actions daily for new versions and create pull requests for you if there is a new version available.
Lastly we need to create an access token on Docker Hub for Github Actions to authenticate with. Login to Docker Hub, go to Account Settings –> Security (https://hub.docker.com/settings/security) and create a new Access Token. As of today it’s only possible to create one single Token with full super admin rights to your account for free users (which I think is a big security mistake) so create this token and note the displayed token. As you can also only create one token I suggest you put it in your password manager if you want to use it in other projects too as you can’t display the value of the token afterwards.
Now head over to your github repositories settings and click on Secrets
on the left. Create one secret called DOCKERHUB_USERNAME
with your Dockerhub username and one named DOCKERHUB_TOKEN
with your copied token.
Now push the two yml files to your repository and watch the build on the Actions
tab of your repository.