Integration of DevOps Tools with Jenkins — Jenkins Case Study

Visal Tyagi
DevOps-Guides
Published in
20 min readMay 11, 2024

Problem Statement:

You have been hired as a DevOps Engineer in XYZ software company. They want to implement a CI/CD pipeline in their company. You have been asked to implement this lifecycle as fast as possible. As this is a product-based company, their product is available on this GitHub link. https://github.com/hshar/website.git

The following are the specifications of the continuous integration:

1. Git workflow has to be implemented

2. Code Build should automatically be triggered once a commit is made to the master branch or develop branch. If a commit is made to the master branch, build and publish a website on port 82. If a commit is made to develop branch, build the product, do not publish.

3. Create a pipeline for the above tasks

4. Create a container with Ubuntu and Apache installed in it and use that container to build the code the code should be on ‘/var/www/html’.

Before checking this Jenkins Case Study Solution, kindly check these Jenkins Assignments to understand this case-study solution:

Trigger a Pipeline Using Git When Push on Develop Branch & Also Pull Git Content on Folder — Jenkins Assignment 1

Add 2 Nodes in Jenkins & Made Push to Branches & Pull Content to Git Folder — Jenkins Assignment 2

Create a Pipeline When One Job Is Successful Another Job Should Be Triggered — Jenkins Assignment 3

Integration of DevOps Tools with Jenkins — Jenkins Case Study
Integration of DevOps Tools with Jenkins — Jenkins Case Study

Copy the Given Below Commands & Fork The Repository Using this Git Hub Repo:

1. Create Jenkins Master & Jenkins Slave Machine

A. Create Two Amazon EC2 Instances Named As Jenkins Master & Jenkins Slave

Step 1: Go to the “Services” section & search “EC2” here. Put the cursor over “EC2” & click on “Instances”.

Search EC2
Search EC2

Step 2: Click on “Launch Instance”.

Launch Instance
Launch Instance

Step 3: Choose “Name” as “Jenkins-“ in the “Name and tags” section.

Instance Name
Instance Name

Step 4: Choose “AMI” as “ubuntu”.

Ubuntu AMI
Ubuntu AMI

Step 5: Choose “Instance type” as “t2.micro”.

t2.micro instance
t2.micro instance

Step 6: Choose “key pair (login)” as “Jenkins2”.

Choose key pair
Choose key pair

Step 7: In “Network settings”, choose “Firewall (security groups)” as “Select existing security group”. While we have chosen “launch-wizard-9” as the “Common security groups”.

Network Settings
Network Settings

Step 8: In “Summary”, write “Number of Instances” as “2”. Click on “Launch Instance”.

Launch Instance
Launch Instance

Step 9: Two Instances will be successfully launched. Click on “Instances” in “Breadcrumbs”.

Instances Launched
Instances Launched

Step 10: Rename the First “Instances” as “Jenkins-Master” & the second one as “Jenkins-Slave”.

Instance Launched
Instance Launched
Rename Jenkins Instance
Rename Jenkins Instance

b. Install “Jenkins” Over the Master (Jenkins-Master) Machine

Step 1: Select the “Jenkins-Master” & click on “Connect”.

Select Jenkins-Master Instance
Select Jenkins-Master Instance

Step 2: Click on “Connect” Again.

Connect Jenkins-Master Instance
Connect Jenkins-Master Instance

Step 3: The “Jenkins-Master” machine will be opened. Run this command to update the machine:

sudo apt-get update
Update Master Machine
Update Master Machine

Step 4: Now, we will install Jenkins on the “Ubuntu” machine. Run this given command:

sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian/jenkins.io-2023.key

Press “Enter” from the keyboard to run this command.

Jenkins Repo will be configured on the machine.

Configure Jenkins Repo
Configure Jenkins Repo

Step 5: Now, append the Debian package repository to the server’s sources.list

Run this given command:

echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
https://pkg.jenkins.io/debian binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null

Press “Enter” from the keyboard to run this command.

Append Debian Repository
Append Debian Repository

Step 6: Run this command to install the “java” on “Ubuntu Machine”:

sudo apt install fontconfig openjdk-17-jre

Press “Enter” from the keyboard to run this command.

Install OpenJDK
Install OpenJDK

Press “Y” to execute the “Openjdk “ installation.

Press “Y”
Press “Y”

“Java Package” will be successfully installed.

Step 7: First, update the machine & run the below-given commands to update & install the “Jenkins” over the “ubuntu machine.”:

sudo apt-get update
sudo apt-get install jenkins -y

Press “Enter” from the keyboard to run these commands.

Install Jenkins
Install Jenkins

Step 8: Now, enable the Jenkins to start the boot. Run the given command:

sudo systemctl enable jenkins

Press “Enter” from the keyboard to run this command.

Enable Jenkins
Enable Jenkins

Step 9: Now, start the “Jenkins” as a service:

sudo systemctl start jenkins

Press “Enter” from the keyboard to run this command.

Start Jenkins
Start Jenkins

Step 10: Now, check the “Jenkins service” status using this command:

sudo systemctl status jenkins

Press “Enter” from the keyboard to run this command.

Jenkins Status
Jenkins Status

Step 11: Copy the “Public IP” of “Jenkins-Master”. Put in the browser address bar with port no 8080 [http://18.117.152.173:8080/]. The “Unlock Jenkins” option will be shown.

Copy the given command
Copy the given command

Step 12: Put the above-given command with sudo cat in “Jenkins-Master”.

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Press “Enter” from the keyboard.

A token will be given. Copy this token:

c4d980dc78f04672ad8b3d7e01e027af

Copy the Token
Copy the Token

Step 13: Paste the given token in the “Administrator password” section. Click on “Continue”.

Paste the token
Paste the token

Step 14: Click on “Install Suggested Plugins”.

Install Suggested Plugins
Install Suggested Plugins

Step 15: Plugin will be started installing like this:

Plugin Installation Started
Plugin Installation Started

Step 16: Now, create your admin user. Put the following entries here:

Username: — admin

Password: — admin

Confirm Password: — admin

Create Admin User
Create Admin User

Full Name: — admin

E-mail Address: — admin@admin.com

Click on “Save and Continue”.

Save and Continue
Save and Continue

Step 17: Jenkins has been successfully installed. A configuration URL will be given. Click on “Save and Finish”.

Save and Finish
Save and Finish

Step 18: Click on “Start using Jenkins”.

Start Using Jenkins
Start Using Jenkins

Step 19: Your “Jenkins Dashboard” setup will be successfully done.

Jenkins Dashboard
Jenkins Dashboard

c. Install “Java” Over Jenkins-Slave Machine

Step 1: Select the “Jenkins-Slave” & click on “Connect”.

Connect Jenkins Slave Machine
Connect Jenkins Slave Machine

Step 2: Click on “Connect” Again.

Connect the Jenkins-Slave Machine
Connect the Jenkins-Slave Machine

Step 3: The “Jenkins-Slave” machine will be opened. Run the below-given command to update the machine:

sudo apt-get update
Update the Slave Machine
Update the Slave Machine

Step 4: Run the command to install “Java” on “Ubuntu”.

sudo apt install fontconfig openjdk-17-jre

Press “Enter” from the keyboard to run this command.

Install Java on the Slave Machine
Install Java on the Slave Machine

Type “Y” to install java.

Type “Y”
Type “Y”

“Java Package” will be successfully installed.

Step 5: Run this command to check the Java version:

java --version
Check the Java Version
Check the Java Version

d. Add Node (Jenkins-Slave) in Jenkins-Master using .pem

Step 1: Click on “Manage Jenkins”.

Manage Jenkins
Manage Jenkins

Step 2: Click on “Nodes”.

Nodes
Nodes

Step 3: Click on “New Node”.

Click on New Node
Click on New Node

Step 4: Choose “Node name” as “Slave” & “Type” as “Permanent Agent”. Click on “Create”.

Create Slave Node Here
Create a Slave Node Here

Step 5: Put the following entries here:

Name: Slave

Description: This is a slave machine.

Number of executors: 1

Choose Slave Node Description Here
Choose Slave Node Description Here

Choose where you want to save the “Jenkins” data. We have installed Jenkins over “ubuntu”.

While choosing “label” as “Slave”. Because Jenkins identifies the node using the “label” feature.

Usage will remain as it is.

Choose the Root directory & Label it here
Choose the Root directory & Label it here

Choose “Launch Method” as “Launch Agents via SSH”.

Host: — 172.31.31.159

Click on “Add” in “Credentials”.

Launch Agent Via SSH

Click on “Jenkins”.

Click Jenkins
Click Jenkins

Choose the following entries here:

Domain: — Global credentials (unrestricted)Remain as it is.

Kind: — SSH username with private key

Scope: — Global (Jenkins, nodes, items, all child items, etc)

Id & Description: — pwdless

Choose ID & Description
Choose ID & Description

Username: — ubuntu

Choose “Enter Directly” in “Private Key”.

Click on “Add”.

Click on Add
Click on Add

Copy the “Jenkins2.pem” content from here.

Jenkins Key
Jenkins Key

Paste the .pem key in “Enter Directly” in the “Private Key” section.

Add PEM Key
Add PEM Key

Choose “Credentials” as “ubuntu (pwdlesss)”.

While choosing “Host Key Verification Strategy” as “Non verifying Verification Strategy”.

Click on “Save”.

Save Credentials
Save Credentials

Step 6: Agent “Slave” has been successfully created in “Jenkins” using the “passwordless authentication”.

Slave Node Added
Slave Node Added

Problem (1) Solution: Git workflow has to be implemented

A. Fork the Given Repository

Step 1: First, fork the given repository to your “GitHub Account” to perform this assignment.

Fork the Given Repository
Fork the Given Repository

Step 2: Login into your “Git Hub Account”. Click on “Sign in”.

Click Signin
Click Sign in

Step 3: Put your username & password here. Click on “Sign in”.

Git Hub Login
Git Hub Login

Step 4: Now, the “Fork” option will be shown. Click on “Fork”. It will automatically fetch “website” as the “Repository name”.

Create a Fork
Create a Fork

Description: Jenkins Case Study Website

Remain as it is selected “Copy the master branch only”.

Step 5: Click on “Create fork”.

Click on Create fork
Click on Create fork

Step 6: It will take some time to fork the repository.

Repository is Forking
Repository is Forking

Step 7: Given that “Repository [website]” has been successfully forked.

Repository Forked
Repository Forked

B. Clone the Given Repository Over the “Jenkins-Master” Node

Here, we will take Two Instances named “Jenkins-Master” & “Prod”.

Master Job —Hosted to Jenkins Master — Built-in Node

Develop Job —Hosted to Jenkins-Slave Instance —Slave Node

Step 1: Go to “Instances”. Select the “Jenkins-Master” node & click on “Connect”.

Select Jenkins-Master
Select Jenkins-Master

Step 2: Again, click on “Connect”.

Connect Jenkins-Master Instance
Connect to Jenkins-Master

Step 3: Run the below command to update the machine:

sudo apt-get update
Update Master Machine
Update the Master Machine

Step 4: Now, clone the repository using the below-given command:

git clone https://github.com/visaltyagi/website.git
Clone the Given Repository to the Machine
Clone the Given Repository to the Machine

Press “Enter” from the keyboard.

Step 5: Do “ls” here. Go to the “website” directory using the below-given command:

ls
cd website
Go to website directory
Go to the website directory

You will land inside the “website” directory.

Step 6: Do “ls” here & your “images” directory & “index.html” file will be present here.

Files Shown
Files Shown

C. Create a “Develop” Branch & Push it into the “GitHub” Repository

Step 1: To create a branch in git, use the below-given command:

git branch develop

Run the below-given command to view the branch:

git branch
Create develop branch
Create develop branch

The “develop” branch will be shown here.

Step 2: Now, run this command to check the “develop” branch:

git checkout develop
ls
Go to develop branch
Go to develop branch

Do “ls” here & all the files will be found here also.

Step 3: Now, switch to the “master” branch using the below-given command:

git checkout master
Go to the master branch
Go to the master branch

Step 4: Run the below-given command to push all the commits inside the master:

git push --all

Put your Git Hub username & password to push all the changes in the Git Hub Account.

Push All Changes to the Master Repository in the Remote
Push All Changes to the Master Repository in the Remote

Step 5: Now, go to your “GitHub Repository” & check the branches. The “develop” branch will be successfully shown here.

develop branch
develop branch

Problem (2) Solution: Code Build should automatically be triggered once a commit is made to the master branch or develop branch. If a commit is made to the master branch, build and publish a website on port 82. If a commit is made to develop branch, build the product, do not publish.

A. Install Docker Over Jenkins-Master Machine to Implement the Jenkins Pipeline

Step 1: Run this command to update the “Jenkins-Master” machine:

sudo apt-get update
Update the Master Machine
Update the Master Machine

Step 2: Run this command to install the docker over the “Jenkins-Master” machine:

sudo apt-get install docker.io -y
Install Docker
Install Docker

Step 3: The Docker has been successfully installed. Run the below-given commands one by one to run the docker.

a. Use this command to enable the docker:

sudo systemctl enable docker

b. Use this command to start the docker:

sudo systemctl start docker

c. Use this command to check the docker status:

sudo systemctl status docker
Docker Start & Docker Status
Docker Start & Docker Status

B. Create a Webhook for Trigger the Jobs

Step 1: Go to “Settings”.

Go to Settings
Go to Settings

Step 2: Click on “Webhooks”.

Go to Webhooks
Go to Webhooks

Step 3: Click on “Add webhook”.

Add Webhook
Add Webhook

Step 4: Choose the following options here:

Payload URL: http://18.117.243.252:8080/github-webhook/

Which events would you like to trigger this webhook?: Send me everything

Add webhook
Add webhook

Click on “Add webhook”.

Step 5: Your webhook will be successfully created.

WebHook Created
WebHook Created

C. Create a Develop Job & Don’t Publish It

Step 1: Click on “New Item”.

Go to New Item
Go to New Item

Step 3: Choose the following options here:

Enter an item name: Develop-Job

Option: Freestyle project

Click on “OK”.

Create develop job
Create develop job

Step 4: Choose “Description” as “For develop branch, don’t publish it”.

Develop Job Description
Develop Job Description

Step 5: Go to the “GitHub” account & click on Code. Go to the “HTTPS” section & copy the link.

Git Hub URL & Choose Label
Git Hub URL & Choose Label

While Choosing “Label Expression” as “Slave”.

Step 6: In “Source Code Management”, choose “Git”. Put “Repo URL” in the “Repository URL” section.

Choose SCM as Git
Choose SCM as Git

Step 7: Choose “Branch” as “develop” in “Branch Specifier”.

Choose Branch Specifier as “develop”
Choose Branch Specifier as “develop”

Step 8: Choose “GitHub hook trigger for GITScm polling” in “Build Triggers”.

Choose Git Hub Hook Trigger Option
Choose Git Hub Hook Trigger Option

Step 9: Click on “Apply”. After clicking on “Apply”, choose “Save”.

Save “Job” Configuration
Save “Job” Configuration

Step 10: Your “Develop-Job” has been successfully created.

Develop Job Created
Develop Job Created

Step 11: Click on “Build Now” to create a build.

Create a Build
Create a Build

Step 12: The build has been successfully created.

Build Created
Build Created

Step 13: Go to the “Jenkins-Slave” Instance. Type cd /home/ubuntu/jenkins/workspace/Develop-Job. Press “Enter” from the keyboard.

Files in Jenkins-Slave Instance
Files in Jenkins-Slave Instance

Type “ls” here & all the files present here will be shown. Here, the product has been built, but not published.

D. Create a Master Job

Step 1: Click on “New Item”.

New Item
New Item

Step 2: Choose the following options here:

Enter an item name: Master-Job

Option: Freestyle project

Create a Master Job
Create a Master Job

Step 3: Choose “Copy from” as “Develop-Job”. Click on “OK”.

Developer Job Creation
Developer Job Creation

Step 4: In “General”, put the description as “For master branch, publish it”.

Choose the description here
Choose the description here

Note: We don’t run this operation on Slave. We will run this job on the “Jenkins-Master” Instance & not choose any particular label here.

Step 5: Remain other settings as it is. Choose “Branch Specifier (blank for ‘any’) as “*/master”.

Choose master branch here
Choose master branch here

Step 6: Click on “Apply” & “Save”.

Save Master-Job Configuration
Save Master-Job Configuration

Step 7: The “Master-Job” has been successfully created.

Master Job Created
Master Job Created

Problem (2) & Problem (4) Integrated Solution:

If a commit is made to the master branch, build and publish a website on port 82.

Create a container with Ubuntu and Apache installed in it and use that container to build the code the code should be on ‘/var/www/html’.

A. Create a Dockerfile & Push it into the “Master” Branch

Step 1: Run the below-given command to create a “Dockerfile”:

sudo nano DockerFile
Create a Dockerfile
Create a Dockerfile

Step 2: Press “Enter” from the keyboard. Dockerfile will be opened & type the given command here:

FROM ubuntu
RUN apt-get update
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt-get clean
ENTRYPOINT apache2ctl -D FOREGROUND
ADD . /var/www/html/
Dockerfile
Dockerfile

Do “CTRL+X” to exit & To save the file, type “Y” from the keyboard & press “Enter”. Your Dockerfile will be successfully saved & created.

Step 2: You can view the content using the below-given command:

cat Dockerfile
View Dockerfile Content
View Dockerfile Content

Step 3: First, put this Dockerfile into the “Website” Repository in “GitHub”.

Run these below-given commands to insert the file into git:

git add .
git commit –m "Dockerfile"
Dockerfile Committed Successfully
Dockerfile Committed Successfully

Step 4: The “Dockerfile” is not inserted into the “Master Branch”. Run the below-given command to view the branch:

git branch
master branch

The “master” & “develop” both branches will be shown.

Step 5: After running the branch command, run the below-given command to check which branch you have accessed:

git branch

Run the below-given command to publish all the commits:

git push --all

Put your “GitHub Username” & “Password” Here.

Username: visaltyagi@gmail.com

Password: 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 (GitHub Token)

After doing “git push”, all the content is successfully pushed to the “master” branch.

Dockerfile Pushed to Master
Dockerfile Pushed to Master

Step 6: Go to the “GitHub” account & view the “master” branch. You will notice that “Dockerfile” has been successfully pushed to the “master” branch.

Dockerfile Shown in Remote Repository
Dockerfile Shown in Remote Repository

B. If a commit is made to the master branch, build and publish a website on port 82.

Step 1: Click on “Configure” in “Master-Job”.

Go to Configure
Go to Configure

Step 2: Click on “Build Steps”.

Click on Build Steps
Click on Build Steps

Step 3: Click on “Add build step”.

Click on “Add build step”
Click on “Add build step”

Step 4: Click on “Execute Shell”.

Click on “Execute Shell”
Click on “Execute Shell”

Step 5: Put these commands to build a container on port 82:

sudo docker build . -t masterapp
sudo docker run -itd -p 82:80 masterapp
Paste Docker Commands Here
Paste Docker Commands Here

Note: If “sudo” gives a problem in creating the Jenkins pipeline, use these commands like this:

docker build . -t masterapp
docker run -itd -p 82:80 masterapp

Step 6: Click on “Save>Apply”.

Save Configuration Settings
Save Configuration Settings

Step 7: Now, click on “Build Now”.

Create a Build in Master-Job
Create a Build in Master-Job

Step 8: Click on #6.

Click #6
Click #6

Step 9: The Docker container has been successfully created.

Container Created
Container Created

Step 10: Go to the “Jenkins-Master” instance & copy the “Public IP Address” & paste it into the “Browser Address Bar” with port number 82.

Integration of DevOps Tools with Jenkins — Jenkins Case Study
Website Main Page

The “Website Page” has been successfully shown.

Step 11: Every time, we run the command, the container is not created because of the same name & we must run a website on port 82.

Paste this command before these two commands & click on “Save>Apply”.

sudo docker rm -f $(sudo docker ps -a -q)
Save the Configuration Again
Save the Configuration Again

Note: If “sudo” gives a problem in creating the Jenkins pipeline, use these commands like this:

docker rm -f $(docker ps -a -q)
docker build . -t masterapp
docker run -itd -p 82:80 masterapp

Step 12: Click on “Build Now”.

Create a Build in Master-Job
Create a Build

Step 13: Click on “#10”.

Click on Build
Click on Build

Step 14: Click on “Console Output”. Again, the container has been successfully created.

Click Console Output
Click Console Output

Step 15: Refresh the “IP Address” Again. The same website is shown again, meaning the job is working successfully.

Integration of DevOps Tools with Jenkins — Jenkins Case Study
Website Main Page

Note: Before creating a pipeline, we have to disable the “GitHub hook trigger” option in both the “Develop-Job” & “Master-Job”.

Disable Git Hub Hook Trigger
Disable Git Hub Hook Trigger

Problem (3) Solution: Create a pipeline for the above tasks

Now, we will create the pipeline for the above task & make some changes in the index.html file. We will trigger the “master” job via the “develop” job through “Jenkins Pipeline”.

A. Do change in the index.html file

Step 1: Go to “index.html” in the “GitHub” account.

index.html file
index.html file

Step 2: Click on the “Edit” Icon & click on “Edit in place”.

Edit index.html file
Edit index.html file

Step 3: Now, we will replace “Intellipaat” with “Hello Intellipaat”.

Title Unchanged
Title Unchanged
Title Changed
Title Changed

Step 4: Click on “Commit changes”.

Click Commit Changes
Click Commit Changes

Step 5: Again, click on “Commit changes”.

Again Commit
Again Commit

Step 6: The file has been successfully saved.

File Saved
File Saved

B. Add post-build action in the “Develop” Job

Step 1: Go to “Develop” Job & click on “Configure”.

Click Configure
Click Configure

Step 2: Click on “Post-build Actions”.

Go to Post-build Actions
Go to Post-build Actions

Step 3: Click on “Add post-build action>Build other projects”.

Go to “Build other projects”
Go to “Build other projects”

Step 4: Choose the following options here:

Projects to build: Master-Job

Choose “Trigger only if the build is stable”.

Click on “Save”.

Save Post-Build Actions
Save Post-Build Actions

C. Create a Pipeline to Trigger the “Master” Job via the “Develop” Job

Step 1: Click on the “+” sign.

Click Plus Sign
Click Plus Sign

Step 2: In “New View”, choose the following options:

Name: Case-Study-Pipeline

Type: Build Pipeline View

Click on “Create”.

Create Case-Study-Pipeline
\Create Case-Study-Pipeline

Step 3: Choose the following options:

Description: Case-Study-Pipeline

Build-Pipeline View Title: Case-Study-Pipeline

Pipeline Title & Description
Pipeline Title & Description

Step 4: Remain other settings as it is shown in the image.

Other Settings
Other Settings

Step 5: Click on “OK”.

Click OK
Click OK

Step 6: Now, the pipeline has been successfully created. Click on the “Run” Button.

Run Pipeline
Run Pipeline

Step 7: After, clicking on “Run”, the “Master” job will be automatically triggered like this.

Master-Job Triggered Successfully
Master-Job Triggered Successfully

Step 8: Refresh the browser. The “Intellipaat” title has been replaced with the “Hello Intellipaat” title.

Website Page
Website Page

This means, our pipeline is working properly.

Other DevOps Case Studies

Containerized an HTML Website using Docker on Production Environment — Docker Case Study

Creating an Architecture using Terraform on AWS — Terraform Case Study

Suggested a Git Workflow Architecture to Manage the Product Release — Git Case Study 1

Resolve Merge Conflict in The GitHub Repository — Git Case Study 2

Create Two Server Groups & Install Apache on The First Group & NGINX on the Second Group — Ansible Case Study

Create Two Server Groups & Install Apache on The First Group & NGINX on the Second Group — Ansible Case Study

--

--