Build a CICD Pipeline with Git Jenkins Terraform
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.
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 .
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
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
Now to get the password we need to Cat the above Directory.
once jenkins is configured we need to Terraform plugin.
after installation of Terraform plugin Lets configure it under Global tool configuration.
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.
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.
once the Role is created attach that role to a Ec2 instance where we have installed jenkins.
Once the Role is created , we need to point the github repository code under the pipeline of jenkins as shown in below image
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.
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.
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.
Lets configure slack 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.
Conclusion:
This CICD setup has been useful for many Devops engineers you can implement the same in your organization .