Step-by-Step Guide to Automate Google Cloud Infrastructure Deployment with Jenkins, Terraform, and Private GitHub Repository
Overview
In today’s fast-paced DevOps environment, automating infrastructure deployment is crucial for efficient and reliable operations. This blog post will guide you through the process of automating Google Cloud Platform (GCP) infrastructure deployment using Jenkins, Terraform, and a private GitHub repository. By the end of this guide, you’ll have a fully automated pipeline that deploys infrastructure changes with ease.
In this blog I will guide you through all the steps from Configuring your Jenkin instance till creating build trigger to automatically trigger pipeline on code push.
Lets get started with Jenkins first.
Setting up Jenkins on GCP using Click to Deploy
The first step in our journey is setting up Jenkins on Google Cloud Platform. GCP offers a convenient “Click to Deploy” solution for deploying Jenkins on a virtual machine instance. Search “Jenkins” in Google Cloud console search bar and go on below options.
Launch Deployment.
You will get below details after deployment.
Now , log in to Jenkins and install required Plugin. Refer YouTube video for more details on required Plugin.
Creating a Private GitHub Repository
Next, we’ll create a private GitHub repository to store our Terraform configuration files and Jenkins pipeline scripts. Keeping our repository private ensures that sensitive infrastructure details are not exposed to the public. We’ll cover the steps to create a new repository, add collaborators, and set up access permissions.
Adding Terraform Configuration Files
With our GitHub repository set up, it’s time to add our Terraform configuration files. We are adding simple code to create storage bucket.
resource "google_storage_bucket" "my-bucket" {
name = "tt-githubdemo-bucket-001"
project = "tt-dev-001"
location = "US"
force_destroy = true
public_access_prevention = "enforced"
}
Creating a Jenkinsfile
Now, let’s create a Jenkinsfile to define our Jenkins pipeline. The Jenkinsfile contains the steps for our pipeline, including checking out the Terraform code from our GitHub repository, initialising Terraform, planning and applying changes, and testing the infrastructure.
Jenkinsfile
pipeline {
agent any
environment {
GOOGLE_APPLICATION_CREDENTIALS = credentials('gcp-key')
GIT_TOKEN = credentials('git-token')
}
stages {
stage('Git Checkout') {
steps {
git "https://${GIT_TOKEN}@github.com/vishal-bulbule/gcp-tf-jenkin.git"
}
}
stage('Terraform Init') {
steps {
script {
sh 'terraform init'
}
}
}
stage('Terraform Plan') {
steps {
script {
sh 'terraform plan -out=tfplan'
}
}
}
stage('Manual Approval') {
steps {
input "Approve?"
}
}
stage('Terraform Apply') {
steps {
script {
sh 'terraform apply tfplan'
}
}
}
}
}
Creating a Credentials
Create credentials for GitHub Access Token and GCP Service Account keys.
Creating a Pipeline
Using the Jenkins web interface, we’ll create a new pipeline job and link it to our GitHub repository. We’ll configure the pipeline to use our Jenkinsfile from the repository and set up web hook triggers for automatic builds whenever changes are pushed to the repository.
Pipeline Configurations
Testing Manually
Before setting up the web hook trigger, we’ll manually run our pipeline to ensure everything is set up correctly. We’ll monitor the pipeline’s progress in the Jenkins interface and verify that our infrastructure changes are applied successfully.
Creating GitHub Build Trigger using Webhook
Finally, we’ll configure a webhook trigger in our GitHub repository to automatically trigger Jenkins builds whenever changes are pushed. This integration ensures that our pipeline runs automatically in response to code changes, streamlining the deployment process.
In Jenkins Job
Now Go to GitHub Repository > Settings > Webhook
Payload URL should be-
Jenkins url/github-webhook/
Testing Trigger
To wrap up, we’ll make a test change to our Terraform code, commit it to our GitHub repository, and observe Jenkins automatically triggering a build in response to the webhook. This final step confirms that our automation setup is working as expected.
We can see logs that pipeline is triggered by GitHub Push.
Conclusion
In conclusion, automating Google Cloud infrastructure deployment with Jenkins, Terraform, and a private GitHub repository offers numerous benefits, including faster deployments, improved consistency, and enhanced collaboration. By following this step-by-step guide, you’ll be well-equipped to implement automation in your own projects and streamline your DevOps workflow.
GitHub Repo — https://github.com/vishal-bulbule/gcp-tf-jenkin
Refer Video for complete demo
About Me
As an experienced Fully certified (11x certified) Google Cloud Architect, Google Cloud champion Innovator, with over 7+ years of expertise in Google Cloud Networking,Data ,DevOps, Security and ML, I am passionate about technology and innovation. Being a Champion Innovator and Google Cloud Architect, I am always exploring new ways to leverage cloud technologies to deliver innovative solutions that make a difference.
If you have any queries or would like to get in touch, you can reach me at my email address vishal.bulbule@techtrapture.com or connect with me on LinkedIn at https://www.linkedin.com/in/vishal-bulbule/. For a more personal connection, you can also find me on Instagram at https://www.instagram.com/vishal_bulbule/?hl=en.
Additionally, please check out my YouTube Channel at https://www.youtube.com/@techtrapture for tutorials and demos on Google Cloud.