GCP-How to deploy your infrastructure with CI/CD pipeline using terraform?

Sumit K
Google Cloud - Community
6 min readFeb 9, 2023
Jenkins workflow

Jenkins is an open-source automation tool for Continuous Integration (CI) and Continuous Deployment (CD). It is a server-based system that runs in servlet containers like Apache Tomcat. Jenkins is one of the most used DevOps tools used along with other cloud-native tools and allows developers to build, test and deploy software seamlessly.

Terraform is an open-source infrastructure as a code software tool that enables you to safely and predictably create, change, and improve infrastructure.

Git is a free and open-source version control system used to handle small to very large projects efficiently. Git is used to track changes in the source code, enabling multiple developers to work together on non-linear development

In this tutorial, We will deploy our infrastructure (VPC and Subnet) with terraform but with a CI/CD approach. we will basically integrate terraform into a generic deployment pipeline. In simple words, We are asking Jenkins to perform terraform-related operations on behalf of humans and bring automation to make your life easier. So, you will write a code in your local machine and push these codes to the source repository GitHub. The Jenkins server (which is running in Google cloud) will fetch this repo, perform automation and deploy your infrastructure in GCP. Refer to the below diagram and Let’s get straight on to the demo.

CI/CD Pipeline Architecture with Jenkins

Pre-requisite:

  • A GCP Account with one Project.
  • Service Account with computing and network admin role attached. you can make it owner. Please keep in mind, you need to attach this service account to authorize the Jenkins machine to deploy the resources programmatically.

How to install Jenkins:

  1. Create a Virtual Machine in Google Cloud- with Service Account attached as mentioned earlier in the pre-requisite.
Jenkins Server
Service Account Attached with full access to Cloud APIs

2. Make sure you must allow port 8080 in your firewall rule.

HTTP/HTTPS Firewall on

3. Once installed, connect to your machine and install Jenkins.

4 . To install Jenkins, Run the following commands:

sudo apt update
sudo apt install default-jdk
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
sudo sh -c 'echo deb https://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
sudo systemctl status jenkins


● jenkins.service - Jenkins Continuous Integration Server
Loaded: loaded (/lib/systemd/system/jenkins.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2023-02-09 09:07:36 UTC; 1min 20s ago
Main PID: 483 (java)
Tasks: 44 (limit: 4691)
Memory: 1.2G
CPU: 53.606s
CGroup: /system.slice/jenkins.service
└─483 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/java/jenkins.war --webroot=/var/cach>

5. Jenkins is installed and ready to use. Get the public IP and access it in any browser http://<EXTERNAL_IP>:8080

Jenkins Administrator Password Location

You need to enter the administrator password to access Jenkins, available at the highlighted location, as shown in the above image. To fetch the pass, you need to go back to your Jenkins server and use the above log path with the highlighted location. Copy the pass that will appear on your screen after running the above code and paste it on the password field. Follow the instruction, install suggested plugins, and create your first admin user. You will be able to log in and see the dashboard.

6. Install Terraform. Click here to follow the instructions.

7. Install Git.

8. Install the gcloud CLI.

Write Terraform Code and push it to the GitHub Repo:

Since I have already created my repo in a GitHub and pushed my code into it. what you can do, you need to fork this repo and use it in your pipeline or copy the code, create your new repo, and push it with git commands. The choice is yours :)

Note: I assume you know the basic knowledge of Git commands. Git is a DevOps tool used for source code management.

Code has been pushed to my GitHub repo.

How to create CI/CD Pipeline with Groovy script:

Now that we have checked our code into the source repository, it’s time to build a pipeline that fetches this code from the repo builds it, and deploys the infrastructure.

Step1. Login to Jenkins with your user and password. you will see the home page.

Jenkins Login Page

Step2. Install Terraform and Git plugins. Go to Manage Jenkins →Plugin Manager →Available Plugin and search for Terraform plugin, Git Plugins and install it.

Once done, Go to Manage Jenkins →Global Tool Configuration, you will see your terraform and Git plugin configuration here. you need to set the name and default directory.

Terraform Plugin
Git Plugin

Step3. Click on New Item, Select Pipeline Enter the Name of your pipeline, and create.

Step3a. Go to the Advance Project Option, select Pipeline Script, and paste the following groovy script. This is a very simple script that contains 4 stages (git checkout, terraform init, terraform plan, and terraform apply). You just need to copy and paste this script as it is but you have to Replace your <PROJECT_ID> and <GIT CREDENTIALS> and save your pipeline. you can take a reference of my pipeline screenshot as depicted below.

To Generate GIT CREDENTIALS, Follow the steps below:

  • Click on “Pipeline syntax” (open in a new tab preferably)under the Pipeline section.
  • In “Snippet Generator” page, dropdown the “Sample Step” and select “git:Git
  • Paste your Git URL and other details. You can add a credential if you have created a git credential in your Jenkins.
  • To create a credential go to Dashboard>Credential>System>Global credentials and click on “Add credentials”.
  • Click on “Generate Pipeline script
  • You end up getting a git checkout credential for the git repo you mentioned.
  • Now copy the code snippet and paste it replacing the <GIT CREDENTIALS> in Pipeline script.
pipeline{
agent any
environment {
CLOUDSDK_CORE_PROJECT='<PROJECT_ID>'

}
stages{

stage('Git checkout'){
steps{
<GIT CREDENTIALS>
}
}
stage('Initialize'){
steps{

sh 'terraform init'

}
}
stage('Plan'){
steps{

sh 'terraform plan'

}
}
stage('apply'){
steps{

sh 'terraform apply -auto-approve'

}
}
}
}
My pipeline script is for reference.

Step4. you can see your pipeline on the dashboard. now Let’s build it. click on your pipeline and click on “build now”.

Build stage View

As you can see the pipeline has been successfully built. This Pipeline Stage View plugin includes an extended visualization of Pipeline build history on the index page of a flow project, under Stage View. When you run some builds, the stage view will appear with Checkout, Init, Plan, Apply, and one row per build like we have defined 4 stages in our Groovy Script. That's why it appears to us with 4 stages view.

Step5. Let’s Verify the resources in the Google Console.

VPC and Subnets are created

You can see that your infrastructure resources have been successfully deployed with the CI/CD pipeline.

Step6. To clean up/destroy the resources, you just need to tweak the groovy script in the existing pipeline. Once done, apply and save and then build it. See below and try to perform on your own.

Groovy script to perform Terraform Destroy
Terraform destroys stage view.

Congratulations! you have successfully implemented your CI/CD pipeline with Jenkins. In this blog, we have created a VPC/Subnet in GCP and followed all the steps to create CI CD pipeline using Jenkins. That’s it for now. feel free to like, share and comment.

Thank you for Reading!!

--

--

Sumit K
Google Cloud - Community

Humanity is the quality that we lack so much in real life, An Abide leaner, Cloud Architect⛅️, Love DevOps, AWS Community Builder 2023, Proud Hindu 🕉️