CI/CD Pipeline with Docker using GitHub Actions

Uvindu Dharmawardana
Nerd For Tech
Published in
6 min readFeb 19, 2021

A few years ago Github announced Github Actions which is an incredibly useful tool for developers. Now the big question is will Github Actions reduce the complexity of modern DevOps operations? the answer is inside this article.

In this article, we will be focussing on…

  1. What are GitHub Actions?
  2. What are the Developer workflows?
  3. GitHub Events and Actions.
  4. CI/CD Pipeline with GitHub Actions.

What are GitHub Actions?

Nowadays the developers have to consider more than just writing the code. Devs have to write their code, build the artifacts, test those artifacts, and also deploy those artifacts to the test environments. You can think that “that's a lot of work..!” yes it is, and this is where GitHub action really starts to shine. In simple words, GitHub actions used to automate the software development workflows. Basically, GitHub actions are CI/CD platform that will help developers to automate their workflows. So it's better we take a look at what are those developer workflows?

What are the Developer workflows?

Like I said before developers spend so much of their time code management, code testing, deploying, etc. When it comes to GitHub there are lots of organizational tasks going on. There both contributors and users for a project. Users create new issues and developers or contributors need to check on that issue and fix it. After fixing the issue new version of the code is available and pull requests are creating for the new version of the code. In that way, the new version of code can be used for the next release of the software. With all that, there are major tasks that can be identified with these tasks.

The above figure shows the most common workflow that needs to be done after merging a new version of the software. Developers need to test the new version of code, build and deploy the software to the production environment. For a small project, you can say that there is no lot of work that needs to be done, but problems come when projects started to grow, More users will associate with the project, and more contributors will join so this makes the whole process difficult to manage. So you can understand that when the project gets bigger organizational tasks related to that project also become complex. So as a developer you don’t need to spend your time managing the organizational tasks related to the project, you need to automate these things as much as possible. It will allow developers to more concentrate on their code.

GitHub Events and Actions

So far we have discussed that what is GitHub Actions and what are the developer workflows. Now we can take a look at how GitHub Events and actions work together. When something happens in your repository you can set automatic actions as a response to those events. These events specially named GitHub Events, creating a new issue, new pull request, contributor joining all these things can be identified as GitHub events. The process of triggering action is very simple. First, you listen for a particular event to get triggered then depend on the event certain action is getting started automatically.

CI/CD Pipeline with GitHub Actions

The first question that comes to everyone's mind is that what we need GitHub Actions?, we already have many CI/CD tools like Jenkins. The answer is if you have your code in GitHub you don’t need any other third-party tools to manage your CI/CD process. GitHub Actions is a built-in tool for GitHub users. And also setting up the CI/CD pipeline using GitHub actions is very easy. This makes it easy to Integration with other technologies as well.

Now let’s see how to set up CI/CD pipeline with docker for your application. In this example, we are using a java based application. Before we start the demo you need to have a GitHub repository to set up your CI/CD pipeline. I have created a repository called Java-CI-CD-pipeline.

You can see that there is a tab called “Actions” in your Git repository. Click on that tab and you can see you can get started with automating your own workflow. Now I’m going to publish my java application to the Git repository I just created. Using the “git push” command you can publish your changes.

To create a GitHub Action click on the “Actions” tab, then you will see a list of workflow templates. In this example, I’m going to show you how to create a simple continuous integration workflow for your application. So you can use one of those matching workflow templates to create the GitHub Actions. In this example, I’m using a java Gradle application, So according to the application type I have to select Java with Gradle workflow for my project.

Once you select the workflow type, .yml type file will be created in your GitHub repository under the ./workflow directory.

This is the file that created in my repo. in this file push and pull refers to the general git push command and pull actions, so if any kind of these actions triggered it will start the workflow defined in the .yml file just created. in this file, jobs refer to a group of actions that will be executed in the workflow.

In the above code sample, the “name” tag is defined to add a workflow name. and under the steps tag, “uses” attribute is used to set predefined actions such as checkout. So like that using the “uses” attribute you can set any such actions. And the “run” attribute refers to running a command-line command. So let’s check out how our workflow is going to execute.

After setting up this file, commit the file to your code. And next add some changes to your local code and push it to the git repository that you have just enabled the GitHub actions. After completing that go and check the GitHub actions section, you will able to see the executed workflow under the git commit message. In my case, I added the git commit as “Update gradle.yml”

Click on the workflow and you will be able to see the execution of the jobs you have defined in the workflow.

Ok cool..! now we have the basic setup of our GitHub actions, now let’s see how to integrate Docker to this.

Integrate Docker to the Workflow

In this step, we are going to build and publish a docker image for our application using GitHub actions. First, you need to have a docker repository in your docker hub account. After creating the repository add the following code sample to your workflow yaml file.

steps:
- uses: actions/checkout@v2
name: Check out code

- uses: mr-smithers-excellent/docker-build-push@v5
name: Build & push Docker image
with:
image: repo/image
tags: v1, latest
registry: registry-url.io
dockerfile: Dockerfile.ci
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

Add the required details for the docker configuration. For the username and password in this code it is referring to a secret, so what you can do is, you can add a secret using the GitHub secrets. In your repository go to the settings sections and there at the bottom you can find the secrets tab from there you can add two secrets for username and password. Note that before running the workflow you need to add your dockerfile into the working directory. After that push the changes to the repository. Now you will be able to see that your docker workflow is also running.

--

--