Orchestrate Infra Pipeline with Jenkins and Google Cloud SDK
In the earlier posts, we looked at how to use Google Cloud SDK in PowerShell as well as also looked at some examples. Now lets spend sometime to look at how we can orchestrate the “gcloud” commands through Jenkins and of course leveraging PowerShell scripting features.
Scenario: We want to build a job in Jenkins that accepts and reads a CSV (comma-separated) file that comprises of a list of VM names that are to be created in Google.
Pre-requisite: The following should be installed and configured for building and running this scenario-
* Jenkins and Jenkins plugins – GCloudSDK Plugin, Google OAuthCredentials, PowerShell Plugin
Step 1 -> Log into Jenkins (post installing the above said plugins) and create a new item/project called “GCP-Setup-Infra” as shown below and save this project-
Step 2 -> Let us configure this new project now. First, we will give a brief description to this project and then select the option “This project is parametrized” and mention the file format as shown below that accepts names as “vm_name_repo.csv“-
Note- when we run this job; it will prompt us to browse for a CSV file. Once we mention the file, the Jenkins job will upload the file in the project workspaces folder under Jenkins.
Step 3 -> We now need to code the steps to be executed. In the project configuration page, click on “BUILD” tab and insert a PowerShell script for adding below code (do replace the lines 5 and 6 with your GCP credential and project ID under which the VMs are to be created). And then save the project settings.
Click here to copy script content
Step 4 -> We are ready with the Jenkins project, but before running this project; we need to build a CSV in our local system called “vm_name_repo.csv” as shown below that comprises of VM names to be setup in GCP-
Step 5 -> Let us now run this Jenkins project. For the first time, Jenkins will prompt for Google authorization but in successive runs it will not prompt for the same. We will click on the “Build with Parameters” option on the left panel in Jenkins to trigger the build.
Note- as soon as the job gets triggered, the job will prompt user to specify the CSV file, we will browse and mention the file name from local.
Click on the “Build” button to start the job. Now, click on the #build number on left panel, in our case its #1, click on this link as shown below-
Click on the “Console Output” option to view details on the run as shown below-
View the job status.. if the build was successful; you will see the Finished status as SUCCESS as shown below-
Step 6 -> Confirm the new Compute Engine/ VMs on GCP platform as shown below. The new Compute Engine / VMs get created in “us-central1-a” zone with default settings-
So the command the helped us spin up GCP Compute Engine/VMs was –
gcloud compute instances create “vm name” –zone=us-central1-a
The ‘zone‘ attribute can also be parameterized as done for VM names.
Wasn’t this fun and interesting 🙂