This article provides a comprehensive guide to creating a CI/CD pipeline that deploys microservices on Google Cloud Platform (GCP) using Jenkins, Terraform, and Cloud Run. Leveraging Jenkins for continuous integration, Terraform for Infrastructure as Code (IaC), and Cloud Run for containerized service hosting, we’ll explore a robust solution to automate deployments to the production environment efficiently and reliably.
Table of Contents
- Continuous Integration Tool — Jenkins
- IaC with Terraform
- Container Services on Google Cloud Platform
- Cloud Run
- Infrastructure Setup
- Code
1. Continuous Integration Tool — Jenkins
Jenkins is a popular, open-source CI/CD tool used for automating software development workflows, from testing to deployment. This article focuses on using Jenkins to deploy the latest Docker images to Cloud Run whenever a new code commit is pushed to GitHub.
- Setup: Jenkins can run on any system with Java installed and can be set up using Docker, native system packages, or standalone.
- Deployment: We’ll configure Jenkins to automate image deployment to Cloud Run by monitoring code changes in GitHub.
2. Infrastructure as Code — Terraform
Terraform is an IaC tool that allows for managing cloud resources in a safe, repeatable way. Here, Terraform will handle the state of the Cloud Run service, updating it with the latest Docker image.
3. Container Services on Google Cloud Platform
GCP offers several containerized service options, including Google Kubernetes Engine (GKE), Cloud Run, and App Engine. In this article, we’ll focus on Cloud Run for its ease of use and managed environment for container deployment.
4. Cloud Run
Cloud Run is a managed GCP service for deploying containerized applications without requiring server infrastructure management. It supports source-based deployment of containers, allowing for applications in various languages, including Go, Node.js, Python, and Java.
Key Features:
- Dedicated HTTPS endpoints for services.
- Request-based auto-scaling.
- Integrated traffic management.
- Public and private service options.
5. Infrastructure Setup
In this setup:
- Jenkins, Docker, Git, and Terraform are installed on a Compute Engine virtual machine.
- GitHub repository acts as the source for code, with automatic Jenkins jobs triggered by commits.
Pipeline Workflow:
- Developer commits code to GitHub.
- Jenkins job triggers, building a new Docker image and pushing it to Google Container Registry (GCR).
- Terraform updates the Cloud Run service with the new Docker image.
Configuration Steps
Install Jenkins
# Install Jenkins on Ubuntu
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt install openjdk-11-jdk -y
sudo apt-get install jenkins -y
Configure Firewall Rules
Allow Jenkins on port 8080 and set up a firewall rule for VM access.
Jenkins Setup
- Log in to Jenkins using the VM’s IP and port 8080.
- Install the GitHub and Extended Email plugins.
- Configure webhooks in GitHub to trigger Jenkins jobs on code commits.
- Set up email notifications using SMTP for deployment status updates.
Service Account Setup
Create a GCP service account with editor permissions for managing Cloud Run and GCR, and attach it to the VM instance.
Install Terraform
# Install Terraform on Ubuntu
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update
sudo apt-get install terraform
6. Code
Jenkins Pipeline Script
In Jenkins, create a pipeline script (Jenkinsfile
) with stages for building, testing, and deploying the Docker container to Cloud Run.
Example Pipeline Stages
- Git Clone: Downloads the latest code.
- Docker Build & Push: Builds a new Docker image and pushes it to GCR.
- Email Notification: Sends deployment updates.
- QA Approval: Manual approval step for quality checks.
- Cloud Run Update: Updates the Cloud Run service with the latest image.
Summary
This article demonstrates setting up a CI/CD pipeline with Jenkins, Terraform, and Cloud Run on GCP. Jenkins monitors code commits in GitHub, triggering Terraform to deploy updates to Cloud Run automatically. This CI/CD setup helps streamline deployments, reduce manual intervention, and speed up the release cycle.
Ref: https://gitlab.com/leonardompdutra/rga_cloud_assessment