CI with GitHub Actions — Publish Docker Images

Mamtha Soni Kashthuri
4 min readMay 14, 2020

--

GitHub provides an in-house feature called GitHub Actions to perform Continuous Integration and Delivery. It lets you create a workflow, define actions in the workflow to build, test, publish and deploy applications.

Actions tab in GitHub provides an easy way for defining the workflow. It gives several options to set up a workflow. example: Java with maven, Ruby with Rake etc..

Continuous Integration Workflow Suggestions

Once you click on Set up this workflow, GitHub opens editor that shows the generated workflow(.github/workflows/maven.yml) and lets you commit the changes

Java with Maven CI Workflow

Defining The Workflow

I used GitHub Actions to build a java Spring Boot Application, create a docker image out of it and published it to Docker Hub.

Here are the steps

  1. Build spring boot application using maven
  2. Build image and publish to Docker Hub

Step 1: Build spring boot application using maven

- name: Set up JDK    
uses: actions/setup-java@v1
with:
java-version: 1.11
- name: Build with Maven
run: mvn clean install
  • Set up JDK task uses the action setup-java@v1 to use the java version 11.
  • Build with Maven task runs the command mvn clean install

Step 2 : Build image & publish to Docker Hub

- name: Publish to Docker Hub
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
repository: dockerHubUser/dockerHubRepository
tags: ${{github.run_number}}

The above task uses docker/build-push-action@v1 action and builds a docker image from the docker file in git repo’s root directory and publishes it to Docker Hub.

  • Docker file’s location could be specified with dockerfile option, if it is not in repository’s root directory.
  • By default, images are pushed to Docker Hub. This setting could be overwritten by providing the registry option.
  • repository refers to the location within the registry where the image should be pushed.

User Name & Password — fetched from GitHub secrets

As always, it is not a good practice to provide user name and passwords in plain text . This is managed by adding a secret in GitHub Secrets and the values could be used in the workflows.

Secrets should be set up from repository settings. Once the secrets are setup, they could be referenced in the workflow file using ${{ secrets.SECRET_NAME }}

GitHub Secrets Page

Tags — fetched from GitHub actions environment variable

tags option sets the tag for docker image. I would like to tag images with build number to uniquely identify each image. tags option is set to ${{github.run_number}} which is one of GitHub Action’s default environment variables. You could also define custom environment variables. Refer to this link to read more about environment variables.

Running the Workflow

Once workflow file changes are saved, build runs automatically and you should be able to see the build results

Build Results Page

Use create status badge from builds page and copy the markdown to README.md file to view the build status in readme.

Build Status Badge

Workflow and Docker Configuration:

Here is the complete workflow file [.github/workflows/build.yml]

name: Build and Publish Docker Image

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 1.11
- name: Build with Maven
run: mvn clean install
- name: Publish to Docker Hub
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
repository: dockerHubUser/dockerHubRepository
tags: ${{github.run_number}}

Dockerfile

FROM openjdk:11

EXPOSE 8080

WORKDIR /applications

COPY target/sample-application-0.0.1-SNAPSHOT.jar /applications/sample-application.jar

ENTRYPOINT ["java","-jar", "sample-application.jar"]

References:

https://help.github.com/en/actions/configuring-and-managing-workflows

https://www.docker.com/blog/first-docker-github-action-is-here/

https://martinfowler.com/articles/continuousIntegration.html

--

--