Loading Posts...

A Simple GitHub Action To Build The Docker Images

I have been busy with few of my personal projects and thought to write an article about using GitHub Action to build the Docker images easily without worrying about the manual build on the local computer. I personally use this in all of my labs and also, I believe this will find it useful for someone who got the similar requirement with Docker image builds.

I use GitLab pipelines to perform CI operations in my projects and I use a custom made Docker image to run the jobs. The reason behind using this Docker image is to have the relevant version of applications such as Terraform, Ansible, etc and I found it really hard without this image to manage my pipelines. After start using this GitHub actions workflow it was even more easier to manage my Docker image. I only need to worry about the packages I wanted to run my pipelines and after updating the image, new images will be updated and available in my Docker hub in minutes while I focus and work on the other stuff.

Read More:

This is the repo I use to maintain my Docker image, and below is the simple build code for the image. Again, it’s quite simple and this is not the intention to guide you to make advanced Docker images. I only wanted to show you the way to use GitHub Actions for this.

To start your Docker build GitHub action workflow created a folder with the name “.github” inside my main repo and another folder inside named “workflow”. So, my GitHub Actions workflow folder path would like this Dockerfile-Terraform-Ansible-AWS/.github/workflows . Inside that created a yaml file named “image_build.yaml” and created the workflow as below. This name can be anything.

name: GitLab Deployment Image

on:
  pull_request:
    branches:
      - master
      
jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Build & Push Image
      run: |
        docker login --username=arunalakmal --password=${{ secrets.TCDockerCred }}
        docker image build -t arunalakmal/tc-terraform-ansible-aws:v1.0.0 .
        docker push arunalakmal/tc-terraform-ansible-aws:v1.0.0

I usually work on a branch and merge to the master branch after the confirmation of the functionality. So, my workflow is running at the pull request to the master branch.

As soon as merging to the master branch Docker build will start under the actions. Then it will push to the Docker Hub.

GitHub Action To Build The Docker image

In order to push the created Docker image to the Docker Hub your account need to be authenticated and credentials are needed. It is obvious that storing the credentials in a repo is crucial and GitHub secrets should be used.

Under the “Settings” and “Secrets” you can add the name of the secret and the values in it with a given name. My secret name is “TCDockerCred“.

GitHub Action To Build The Docker and secrets

You can use this secret in the anywhere in the code as “${{ secrets.TCDockerCred }}”. This is the similarity to the GitLab variables. So, with this you can handle the sensitive information in your code, quite easy.

I hope this is useful for anyone who needs the similar requirement and for more information about configuring the GitHub workflows refer this GitHub Document.

Click to rate this post!
[Total: 2 Average: 5]

Aruna Lakmal

Associate Technical Specialist at Pearson, Sri Lanka. Technology junky, enthusiast, a VMware vExpert and a blogger with more than 7 years of Experience in Information Technology more focusing on VMware Virtualization, Microsoft and Datacenter Technologies.

Get Updates Directly To Your Inbox!

   

Leave a Comment

Loading Posts...