Set up a CI/CD pipeline using GitHub Actions to a GKE cluster
Tags: devops, kubernetes, programming, beginners
Published: 2023-01-31 11:17:53

Setting up a CI/CD pipeline using GitHub Actions to a Google Kubernetes Engine (GKE) cluster

In this article, we'll show you how to set up a Continuous Integration/Continuous Deployment (CI/CD) pipeline using GitHub Actions to a GKE cluster. A CI/CD pipeline automates the deployment of code changes to a production environment. With this setup, code changes will be automatically deployed to the GKE cluster whenever code is pushed to the GitHub repository.

Step 1: Create a GitHub repository

Create a GitHub repository for your project and push the code to the repository. click [here](https://docs.github.com/en/get-started/quickstart/create-a-repo) on how to create a github repository and proceed to step 2

Step 2: Create a GKE cluster

Create a GKE cluster using the Google Cloud Console or the gcloud CLI.

![Create a GKE cluster using the Google Cloud Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rmw1fscz3u35pv39svmf.png)

Replace `<CLUSTER_NAME>` and `<ZONE>` with the appropriate values for your project.

Step 3: Create a Kubernetes deployment

Create a file named `deployment.yml` or `.yaml` .Create a Kubernetes deployment that defines the desired state of your application. The deployment should specify the number of replicas, the container image to use, and any environment variables or secrets required.


![create depolyment.yml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kmyjlfpplcuddptwncl2.png)

Replace `<DEPLOYMENT_NAME>`, `<REPLICAS>`, `<APP_LABEL>`, `<CONTAINER_NAME>`, `<IMAGE_NAME>`, `<ENV_VAR_NAME>`, and `<ENV_VAR_VALUE>` with the appropriate values for your project

Step 4: Create a GitHub Actions Workflow

Create a GitHub Actions workflow to automate the deployment of the Kubernetes deployment to the GKE cluster. The workflow should perform the following steps:

 1. Check out the code from the GitHub repository.
 2. Build the container image.
 3. Push the container image to a container registry, such as Google Container Registry (GCR).
 4. Deploy the container image to the GKE cluster using kubectl.

A workflow is an automated procedure that can be configured to execute one or more jobs. Workflows are defined by a YAML file that is checked into your repository and run when triggered by an event there, manually, or according to a set schedule.

A repository can have multiple workflows, each of which can carry out a unique set of tasks. Workflows are defined in the .github/workflows directory in a repository. One workflow could be used to create and test pull requests, another to deploy your application each time a release is made, and yet another to add a label each time a new issue is opened. 

Create a file named `.github/workflows/deploy.yml` with the following content:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhcpclzuyo7t3iemws45.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mm45znbzawrtlesgaz8a.png)
Remeber that this .yml should be written on a single yaml file, i splitted the screenshot into two in order for the contents to be visible

Replace `<PROJECT_ID>`, `<CLUSTER_NAME>`, `<ZONE>`, `<IMAGE_NAME>`, and `<VERSION>` with the appropriate values for your project.

Step 5: Add Secrets

In the workflow, the `GCLOUD_AUTH` secret is used to authenticate with the `GKE cluster`. Create the `GCLOUD_AUTH` secret in the GitHub repository and add the content of a service account key that has sufficient permissions to deploy to the GKE cluster.

To create the secret, navigate to the GitHub repository, go to the "Settings" tab, and click on "Secrets." Then, click on the "New repository secret" button and give the secret a name (e.g. "GCLOUD_AUTH") and paste in the content of the service account key.

Step 6: Push code changes to the GitHub repository

Push code changes to the GitHub repository and observe the GitHub Actions workflow being triggered. If the workflow is successful, the changes should be automatically deployed to the GKE cluster.

In conclusion, with the setup of a CI/CD pipeline using GitHub Actions, you can automate the deployment process and save time and effort in manual deployments. Additionally, the pipeline ensures that the latest code changes are deployed to the production environment, improving the overall quality of the application.