Migrating Docker Autobuild to Github Actions

nu12
4 min readJun 10, 2021

--

Since we won’t be able to use the Autobuild feature on free accounts starting on June 18th, we need to find alternatives to automate the Docker image creation based on the linked Github repository activity. To achieve this we are going to explore Github actions to trigger image creation, and push new images to Docker Hub using a token for authentication.

Configuring Docker Hub

First things first, let’s delete existing automated build configurations. Login into your Docker Hub account, go to the project’s repository, under Builds tab select “Configure Automated Builds”.

Delete all existing rules using the “trash” icon and save.

Go into the Account Settings, in the Security panel select “New Access Token”.

In the popup window, give the token a name and select “Create”. You’ll see the message below.

From this we can see that we are going to login into Docker Hub using our generated token as password. We’ll do it programmatically in our Github actions. Take note of your token.

Github Actions

Our Docker access is already configured, let’s create the workflow. Clone your git repository and open it in the code editor or IDE of your choice. In the root folder of the application, create a folder called “.github” and another one called “workflows” inside it. This folder structure is needed to make Github actions work. Inside the newly created folder, create a file called “build-latest.yml”.

Inside “build-latest.yml” we will write the code that will run after every push into the master branch, including the conditions to trigger its execution. Github actions follows a structured and well documented approach and you can find more about it here.

We will write a basic recipe to build and push our image based on the repository’s content.

This was made to be generic and reusable. As you can see, configuring some secrets it will be possible to use the above code in multiple projects. In your Github repository, go to Settings, Secrets tab and select “New repository secret”.

Create three secrets: DOCKER_REPO, DOCKER_USER and DOCKER_TOKEN.

Do the same for the other two secrets. This is what you need to have in the end of the process:

Note: enter the appropriate information for each secret.

We have everything in place, let’s commit and push our code.

Under the Actions tab we can follow the script execution. When completed, we should have the following result:

In your Docker Hub account, verify that the new latest build was successfully pushed.

Next, let’s do the same to build Github repository tags (this was one of the Autobuild rules that we deleted before).

The yml file is practically the same, we only need to include one step to extract the tag name from the environment and use this value instead of “latest” in the build and push process. Let’s call this one “build-tag.yml”.

Commit and push this new file. This push will also trigger the “latest” build just like before. Now, create a new tag in the repository to trigger the new workflow. Note: in our example the git tag must start with “v” just like what was defined in the Docker Autobuild configuration, but the final Docker image will not contain the letter. Change this rule to suit specific tagging strategies.

Once the tag is created, the build process shall start. The process will be the same. In the end verify the new tag was successfully pushed to the Docker Hub repository.

That’s it! Now we can automatically build and push Docker images without using Docker Autobuild.

--

--

nu12
0 Followers

Programmer interested in data analytics, Ruby, Python, C++, Docker & Kubernetes.