Set up a CI/CD pipeline using GitHub Actions to a GKE cluster
Adesoji1
Posted on January 31, 2023
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 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.
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.
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:
- Check out the code from the GitHub repository.
- Build the container image.
- Push the container image to a container registry, such as Google Container Registry (GCR).
- 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:
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.
Posted on January 31, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.