01 logo

CI/CD Using GitHub Actions

From Code Push to Kubernetes

By Amr KhaledPublished 4 years ago 3 min read
1

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:

On push to Feature branch

Run tests

On push to Master branch

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

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.

how to
1

About the Creator

Amr Khaled

I’m a software engineer who is passionate about software architecture and design. Enjoy coding in Java, Scala, and JavaScript.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.