How Can You Build Your Docker Image on MacOs with GitHub Actions CI Pipeline?

--

In this article, I will show how to use the GitHub Actions to:

  • Lint and test the code when new commits are pushed to GitHub
  • Build and push a Docker image to the Docker Hub when a release is published

Getting Started With GitHub Actions

Prerequisites

  1. Working knowledge of Git and Docker.
  2. A basic understanding of the YAML syntax is also required. You can learn YAML from its official website, or this basic tutorial kindly provided by the Ansible people.
  3. Fork my application repository and remove all files under .github/workflows/ to practice with the code in this article.
  4. A valid user account on the Docker Hub is required.

What is GitHub Actions?

GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.

GitHub Action is just another tool?

GitHub Actions offer a complete and powerful set of functionalities, a lot of extensions are available on the Marketplace and GitHub offers a generous free tier also for private repositories (at the time of writing).

  • Use the same tool instead of third-party integration
  • Setup the pipeline is easy
  • Tool for developers
  • Integration with other technologies is important and hard to manage but you don’t need this in the GitHub action CI/CD pipeline.

How Do I Create a Workflow on GitHub Actions?

I logged into my GitHub account and created a public repository named p13-MacOS_CI_github_actions.

I pushed my java project using from my local to GitHub repository with git commands.

On my GitHub repository, I clicked on the Actions tab. On this page, you can see the workflows of additional very popular services. To use these, you can build your application without having to write a workflow from scratch.

I have chosen the workflow template named Java with Gradle here. Automatically created .github/workflows/gradle.yml file under my project.

Java with Gradle template is like this:

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
name: Java CI with Gradleon:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-lateststeps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Build with Gradle
uses: gradle/gradle-build-action@4137be6a8bf7d7133955359dbd952c0ca73b1021
with:
arguments: build

I have configured this template for my own project.

Firstly, I have changed my workflows default gradle.yml name to ci.yml.

nameis the name of the project. My project name is Java CI with Gradle On MacOS.

name: Java CI with Gradle

The on part required. Specifies how the workflow will be triggered. I have organized my workflow so that it will be triggered when a push or pull request is made to the main branch.

on: #which events trigger the pipeline   
push: #pushing the master branch
branches: [ main ]
pull_request: #pull request from the master branch
branches: [ main ]

The jobs part shows the events that will run when the workflow is triggered. I also named my job as build-java. I set the operating system that my job will run on as macOS-latest.

jobs: 
build-java: #job name
runs-on: macOS-latest #OS

I have checked out my repository with this syntax. You can see these action details on this link.

    steps:    
- uses: actions/checkout@v2 #pull repository

This action performs the installation of the java environment by looking at the version I have specified here.

    - name: Set up JDK 1.8
uses: actions/setup-java@v1 #install java env
with:
java-version: 1.8 #java version

Unlike the others, this is a Linux command. With this command, I have granted execute permission for my gradlew file.

    - name: Grant execute permission for gradlew
run: chmod +x gradlew

I have built my application with ./gradlew build command.

    - name: Build with Gradle
run: ./gradlew build

I performed the installation of the Docker environment with the following actions. If you want to access the details of the commands running behind these actions, you can access the link.

You can also share your own Actions with the community on the Marketplace.

    - uses: docker-practice/actions-setup-docker@master
- run: set -x

I have built my java application with my Dockerfile which I wrote before, and I create the docker image of my java application. After that, I made my actions authenticate with docker hub and then sent my image to my Docker Hub. I performed all of these operations with the following actions.

    - name: Build and Push Docker Image      
uses: mr-smithers-excellent/docker-build-push@v4
with:
image: drerdemsezgin/flask-app
registry: docker.io
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

I have written whole my ci.yaml. You can see this below and also in my repository.

# This workflow will build a Java project with Gradle
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
name: Java CI with Gradle On MacOS
on: # which events trigger the pipeline
push: # pushing the master branch
branches: [ main ]
pull_request: # pull request from the master branch
branches: [ main ]
jobs:
build-java: # job name
runs-on: macOS-latest # OS
steps:
- uses: actions/checkout@v2 # pull repository

- name: Set up JDK 1.8
uses: actions/setup-java@v1 # install java env
with:
java-version: 1.8
- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Build with Gradle
run: ./gradlew build
- uses: docker-practice/actions-setup-docker@master
- run: set -x
- name: Build and Push Docker Image
uses: mr-smithers-excellent/docker-build-push@v4
with:
image: drerdemsezgin/flask-app
registry: docker.io
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

Before I have triggered GitHub Actions, I defined the credentials to Git Hub Actions so that I can authenticate with Docker Hub. Thus, I increased the security of my workflow by not writing my secrets as hardcode.

For adding secrets to my repository Repository > Settings > Secrets > New repository

  • Name=DOCKER_USERNAME, Value=drerdemsezgin
  • Name=DOCKER_PASSWORD, value=<MyPassword>

I have committed to the master branch of my repo and this commit triggered GitHub actions workflow. Thus, my continuous integration has begun to run.

I clicked on the Actions tab and when I clicked on my ongoing workflow, I can see its details. A list of all the steps will be presented, named as in the name elements in the Action code below. Each “step” line can be expanded to get its output. Also, the command lines can be furtherly expanded to get some more useful information:

If the workflow execution was successfully completed, the image should also be listed in the Docker Hub.

Congratulations! You have just created your Action.

Resources:

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

--

--

DevOps Engineer || 2 x Certified AWS SAA || Certified Kubernetes Administrator || Certified HashiCorp Terraform