Github Actions

GitHub Actions is a service offered by GitHub for automating CI/CD tasks and jobs. With Github's actions, we can define the CI/CD tasks to be executed when a Github event is triggered. The list of the supported events can be found here.

To integrate the Docker image release task as a Github action we need to write a workflow for our job. This workflow is simply a YAML file stored under the following path in ( .github/workflows ), the root path of the GitHub repository. The workflow file contains the needed information for executing the workflow and a list of actions that need to be performed. GitHub provides a marketplace for the actions that can be executed on the workflows. On the other hand, you can develop your own action and publish it to the market place so others can also use it.

To automate and integrate the process of building docker images using GitHub actions, I’ll introduce two workflows.

Workflow one

The first workflow is executed once we create a pull request against the master branch or if a new commit is pushed to the master branch. This workflow will only execute the make build command to make sure our images Docker build is not broken (these actions are for demonstration purposes and can be changed according to needs).

The workflow starts by defining when the actions need to be triggered and then lists the steps that need to be executed. The last step is to send a Slack notification based on job status.

Note that the secret SLACK_WEBHOOK_URL should be created and added to the respective GitHub repository. You can do that by performing these steps.

Second workflow

The second workflow is executed only if a new release is published on GitHub. It will take care of building docker images for that release and push them to the Docker registry.

The first difference between this workflow and the first one is the trigger event. In the first one, it’s triggered on each push to master or each PR against the master branch, but this workflow is only triggered if a new release on GitHub is published. As mentioned, push events are not the only event provided by GitHub, In fact, there’s a long list of events that can be utilized by the workflows.

The second difference is that this workflow will build, tag and push the Docker images to the Docker registry. That means we need to add an extra step to authenticate the Docker registry so it can push images to the registry (secrets can be added as described here).

The Docker image’s tag is also extracted from the GitHub action payload and passed as an Environment variable to the make command as follows: IMAGE_TAG=${{ github.ref}} make release .

Here’s a full implementation of the second workflow. Adding these workflow files to the Github workflow path will automatically enable GitHub actions on the respective repository: