avatarAmr Saleh

Summary

This tutorial demonstrates how to set up CI/CD using GitHub Actions, deploying a sample application on Google Cloud Platform Kubernetes using Helm3.

Abstract

The text provides a step-by-step guide on how to set up Continuous Integration and Continuous Deployment (CI/CD) using GitHub Actions. The author explains the concept of GitHub Actions as tasks to be achieved at specific events, such as Push to a master event. The tutorial focuses on deploying a sample application on Google Cloud Platform Kubernetes using Helm3. The prerequisites for this tutorial are familiarity with Kubernetes and Helm.

The tutorial outlines the use case, where a sample application is built using Spring Boot, and two workflows are defined: one for a feature branch and another for the master branch. On push to a feature branch, the workflow will run tests, while on push to the master branch, the workflow will run tests, push the Docker image to Docker Hub, and deploy to Kubernetes. The tutorial provides links to sample applications and workflow files.

The author explains how GitHub searches for YAML files under ".github/workflows" and how these files define workflow jobs. The tutorial provides examples of feature-workflow.yml and master-workflow.yml files. The feature-workflow.yml file contains a single job (test) with four steps: checkout, setup-java, maven package, and maven verify. The master-workflow.yml file contains three jobs: test, docker, and deploy. The docker job prepares the environment to run regular docker commands to build and push the image to docker hub. The deploy job uses setup-gcloud action from Google cloud platform to install gcloud and generate the kubeconfig file.

The tutorial concludes with instructions on how to verify the deployment using kubectl commands. The author emphasizes the benefits of using GitHub's actions to define the workflow and the ability to find an action for every task that needs to be achieved.

Bullet points

  • GitHub Action is a task to achieve at a certain event
  • The tutorial demonstrates how to set up CI/CD using GitHub Actions
  • The sample application is deployed on Google Cloud Platform Kubernetes using Helm3
  • The tutorial assumes familiarity with Kubernetes and Helm
  • The tutorial defines two workflows: one for a feature branch and another for the master branch
  • The feature-workflow.yml file contains a single job (test) with four steps
  • The master-workflow.yml file contains three jobs: test, docker, and deploy
  • The tutorial provides examples of YAML files for GitHub workflows
  • The tutorial concludes with instructions on how to verify the deployment using kubectl commands

Setting up CI/CD Using GitHub Actions

Photo by Richy Great on Unsplash

GitHub Action is a task you want to achieve at a certain event.

For example, on Push to a master event, you want to build, run tests, create a docker image, push to docker hub, and deploy to Kubernetes.

Build, run, create, push, and deploy are all GitHub Actions.

GitHub has a repository in which you can find an action for pretty much any task you want to achieve, check it out.

You can also write a custom action but this is out of the scope of this tutorial.

Prerequisites

We will be deploying out sample application on google cloud platform Kubernetes using Helm3.

It is highly recommended to familiarize yourself with the following topics:

  1. Kubernetes.
  2. Helm.

Defining the use case

We will write a sample application using spring boot (it doesn’t really matter) and we will run the following workflows:

  1. On push to Feature branch
Run tests

2. On push to Master branch

Run tests -> Push to docker hub -> Deploy to K8s

You can check out the sample application here.

Creating workflow files

GitHub looks for YAML files under “.github/workflows”, these YAML files describe the workflow jobs.

Feature branch workflow

We will create a file called feature-workflow.yml, and let’s have a look into it.

Lines 3–6, excludes the master branch from this workflow, and it applies to all other branches on push events.

The rest of the file will define the jobs, as you can see our feature workflow consists of only one job (test), and this job consists of 4 steps.

1st step uses checkout action from GitHub actions to check out the code.

2nd step, using setup-java action, and set the action argument “java-version” to 13.

3rd and 4th steps are normal maven package and maven verify commands.

You can see the workflows from the “Actions” tab in the GitHub repository.

Master branch workflow

We will create master-workflow.yml file under “.github/workflows”.

This workflow is applied on the master branch and consists of 3 jobs, test, docker, and deploy.

The 1st job is the same as the one above, so we will look into the 2nd and the 3rd.

docker job

Nothing complicated, it prepares the environment to run regular docker commands to build and push the image to docker hub.

Here also we used GitHub secrets to provide the docker hub user, password, and repository name.

After running this job you should see the Docker image pushed to the docker hub.

deploy job

Step 2, 3, and 4, It is using setup-gcloud action from the Google cloud platform to install gcloud and generate the kubeconfig file.

You should specify the project and create a service account for GitHub deployment.

Last 2 steps, setting up helm3 and running helm upgrade command to deploy to k8s.

Helm is a Kubernetes package and release management, you can know more about Helm charts from here.

You can find the Helm chart to deploy this application under “deployment/helm”.

Let’s look into the helm values.yml file and validate the deployment.

We want 2 replicas of github-actions-test, and NodePort service.

Push the code to master, go to Actions and, you’ll find the master workflow is running.

Verifying the deployment

When the master workflow is done, issue the below command

kubectl get pod
Kubectl get svc

You should see 2 instances of the application are running and, exposed through a NodePort service.

Conclusion

We understood what GitHub's actions are, and how to use them to define the workflow.

We also learnt about GitHub action repository and how to find an action for every task that we want to achieve.

Ci Cd Pipeline
Github Actions
Kubernetes
Helm
Infrastructure
Recommended from ReadMedium