Build a CICD Pipeline with Git Jenkins Terraform

jaffar shaik
4 min readMay 18, 2021

--

In this article we will Demonstrate How to build a CICD pipeline with Git, Jenkins and Terraform.

step1

Lets implement the below Architecture for CICD builds using jenkins.In our case we are configuring Git web hooks for implementing continuous Deployments. Whenever there is change in code to GitHub repository build will happens and a slack notification will be triggered to slack channel.

Figure 0 :CICD setup for Jenkins with Terraform

This script supports ubuntu machine .So Launch a aws EC2 instance with ubuntu machine and make sure port 8080 opened there and then Login to the server using Moboxterm or putty.

once logged in Lets Install Jenkins on Docker through a bash script.the script can be found at the following repository.

under this repositoty we have a file called Jenkins by default runs on port 8080.Lets clone this Repository .

Figure1 Cloning the git repository

Once the repository is cloned we can see a file with name “jenkinscript.sh”

As its a Bash script Lets execute this code to install jenkins.

Figure 2 executing script on ubuntu machine to install jenkins

Figure 3 Jenkins installed Successfully

From image 3 , it can be seen that jenkins can be now accessed through public Ip of ubuntu server:8080 in our case :34.229.194.231::8080

when we first Login to jenkins It would seem like this below screen

Figure 4 Fresh jenkins looks like this on first time Login

Now to get the password we need to Cat the above Directory.

Figure 5 password for Jenkins
Figure 6 after jenkins installation

once jenkins is configured we need to Terraform plugin.

Figure 7 Now our Jenkins server is ready.

after installation of Terraform plugin Lets configure it under Global tool configuration.

figure 8 Installing Terraform plugin

once plugin is installed .we can now install Terraform and Git as shown in below screen shot.

To install Terraform please find the below Screen shot.

Figure9 Terraform Installation

whenever we make changes to code in Git the jenkins job should Trigger. Lets Design a pipeline for this Goal.

Lets Assign Aws access to Terraform by creating a role in IAM.

Figure 10 creating IAM role to acess EC2 for Jenkins.

once the Role is created attach that role to a Ec2 instance where we have installed jenkins.

Figure 11 Attach role to EC2 jenkins server.

Once the Role is created , we need to point the github repository code under the pipeline of jenkins as shown in below image

Figure12 :point Github Code to jenkins pipeline job

the source code for jenkins job is present in the Following repository .you can use this below repository code to build CICD pipelines.

Jaffarterraform786/jenkinsfile (github.com)

webhooks configuration:

webhooks allow you to trigger a percent-encoded URL if, for example, new code is pushed or a new issue is created. You can configure webhooks to listen for specific events like pushes, issues or merge requests.

To configure webhooks go to the repository of the jenkins file and under seetings just click on add webook as shown in below image.

Figure 13 :Webhook for push Events.

Here in our case we have configured web hooks for push events. When developers pushed the code the job will be triggered automatically.

Configuring slack Alerts:

Slack is a channel-based messaging platform.Slack can be classified as a tool in the “Group Chat & Notifications” category.

To configure slack Notifications we need to install “slack notification” Plugin.

Figure 14 Slack Notification Plugin Installation

Lets now go to slack.com and create a channel. In our case we have created a channel channel called #jenkins-deployments which is a public channel. Lets add an application it.

Figure 15:Adding Jenkins CI to Slack.

Lets configure slack in Jenkins server.

Figure 16:Slack configuration in Jenkins server.

Now we have configured alerts for Jenkins build to the slack channel.an alert will be triggered to slack channel on successful job builds.

Figure16 :Sample successful build results.

Conclusion:

This CICD setup has been useful for many Devops engineers you can implement the same in your organization .

--

--