Skip to content

Leo Dutra – Tech Blog

Menu
  • Home
  • HOME LAB
  • VMWARE
  • KUBERNETES
  • ABOUT
  • PRIVACY POLICY
Menu

Deploying Microservices on Google Cloud Platform with Jenkins, Terraform, and Cloud Run

Posted on November 6, 2024November 6, 2024 by leonardompdutra

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

  1. Continuous Integration Tool — Jenkins
  2. IaC with Terraform
  3. Container Services on Google Cloud Platform
  4. Cloud Run
  5. Infrastructure Setup
  6. 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:

  1. Developer commits code to GitHub.
  2. Jenkins job triggers, building a new Docker image and pushing it to Google Container Registry (GCR).
  3. 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

  1. Log in to Jenkins using the VM’s IP and port 8080.
  2. Install the GitHub and Extended Email plugins.
  3. Configure webhooks in GitHub to trigger Jenkins jobs on code commits.
  4. 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

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Social Media

  • LinkedIn

Recent Posts

  • Deploy Azure Kubernetes - Terraform
  • Microservices on AWS - ECS
  • Microservices on Google Cloud
  • Easy IoT data - Influxdb Telegraf Grafana
  • Install AWX on Ubuntu 24
  • How to Expand the Windows Partition

ABOUT

My name is Leonardo Dutra and I’m a Devops at Energisa.

With years of experience applying DevOps practices, CI/CD pipelines, and Infrastructure as Code (IaC), I have accelerated software delivery, minimized errors, and enhanced scalability, particularly through the use of containers and Kubernetes. My expertise also spans load balancing, automation, software development, and public cloud environments.

© 2025 Leo Dutra – Tech Blog | Powered by Minimalist Blog WordPress Theme