Integration of DevOps Tools with Jenkins — Jenkins Case Study
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:
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
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”.
Step 2: Click on “Launch Instance”.
Step 3: Choose “Name” as “Jenkins-“ in the “Name and tags” section.
Step 4: Choose “AMI” as “ubuntu”.
Step 5: Choose “Instance type” as “t2.micro”.
Step 6: Choose “key pair (login)” as “Jenkins2”.
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”.
Step 8: In “Summary”, write “Number of Instances” as “2”. Click on “Launch Instance”.
Step 9: Two Instances will be successfully launched. Click on “Instances” in “Breadcrumbs”.
Step 10: Rename the First “Instances” as “Jenkins-Master” & the second one as “Jenkins-Slave”.
b. Install “Jenkins” Over the Master (Jenkins-Master) Machine
Step 1: Select the “Jenkins-Master” & click on “Connect”.
Step 2: Click on “Connect” Again.
Step 3: The “Jenkins-Master” machine will be opened. Run this command to update the machine:
sudo apt-get update
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.
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.
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.
Press “Y” to execute the “Openjdk “ installation.
“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.
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.
Step 9: Now, start the “Jenkins” as a service:
sudo systemctl start jenkins
Press “Enter” from the keyboard to run this command.
Step 10: Now, check the “Jenkins service” status using this command:
sudo systemctl status jenkins
Press “Enter” from the keyboard to run this command.
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.
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
Step 13: Paste the given token in the “Administrator password” section. Click on “Continue”.
Step 14: Click on “Install Suggested Plugins”.
Step 15: Plugin will be started installing like this:
Step 16: Now, create your admin user. Put the following entries here:
Username: — admin
Password: — admin
Confirm Password: — admin
Full Name: — admin
E-mail Address: — admin@admin.com
Click on “Save and Continue”.
Step 17: Jenkins has been successfully installed. A configuration URL will be given. Click on “Save and Finish”.
Step 18: Click on “Start using Jenkins”.
Step 19: Your “Jenkins Dashboard” setup will be successfully done.
c. Install “Java” Over Jenkins-Slave Machine
Step 1: Select the “Jenkins-Slave” & click on “Connect”.
Step 2: Click on “Connect” Again.
Step 3: The “Jenkins-Slave” machine will be opened. Run the below-given command to update the machine:
sudo apt-get update
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.
Type “Y” to install java.
“Java Package” will be successfully installed.
Step 5: Run this command to check the Java version:
java --version
d. Add Node (Jenkins-Slave) in Jenkins-Master using .pem
Step 1: Click on “Manage Jenkins”.
Step 2: Click on “Nodes”.
Step 3: Click on “New Node”.
Step 4: Choose “Node name” as “Slave” & “Type” as “Permanent Agent”. Click on “Create”.
Step 5: Put the following entries here:
Name: Slave
Description: This is a slave machine.
Number of executors: 1
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 “Launch Method” as “Launch Agents via SSH”.
Host: — 172.31.31.159
Click on “Add” in “Credentials”.
Click on “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
Username: — ubuntu
Choose “Enter Directly” in “Private Key”.
Click on “Add”.
Copy the “Jenkins2.pem” content from here.
Paste the .pem key in “Enter Directly” in the “Private Key” section.
Choose “Credentials” as “ubuntu (pwdlesss)”.
While choosing “Host Key Verification Strategy” as “Non verifying Verification Strategy”.
Click on “Save”.
Step 6: Agent “Slave” has been successfully created in “Jenkins” using the “passwordless authentication”.
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.
Step 2: Login into your “Git Hub Account”. Click on “Sign in”.
Step 3: Put your username & password here. Click on “Sign in”.
Step 4: Now, the “Fork” option will be shown. Click on “Fork”. It will automatically fetch “website” as the “Repository name”.
Description: Jenkins Case Study Website
Remain as it is selected “Copy the master branch only”.
Step 5: Click on “Create fork”.
Step 6: It will take some time to fork the repository.
Step 7: Given that “Repository [website]” has been successfully 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”.
Step 2: Again, click on “Connect”.
Step 3: Run the below command to update the machine:
sudo apt-get update
Step 4: Now, clone the repository using the below-given command:
git clone https://github.com/visaltyagi/website.git
Press “Enter” from the keyboard.
Step 5: Do “ls” here. Go to the “website” directory using the below-given command:
ls
cd website
You will land inside the “website” directory.
Step 6: Do “ls” here & your “images” directory & “index.html” file will be present here.
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
The “develop” branch will be shown here.
Step 2: Now, run this command to check the “develop” branch:
git checkout develop
ls
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
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.
Step 5: Now, go to your “GitHub Repository” & check the branches. The “develop” branch will be successfully shown here.
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
Step 2: Run this command to install the docker over the “Jenkins-Master” machine:
sudo apt-get install docker.io -y
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
B. Create a Webhook for Trigger the Jobs
Step 1: Go to “Settings”.
Step 2: Click on “Webhooks”.
Step 3: Click on “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
Click on “Add webhook”.
Step 5: Your webhook will be successfully created.
C. Create a Develop Job & Don’t Publish It
Step 1: Click on “New Item”.
Step 3: Choose the following options here:
Enter an item name: Develop-Job
Option: Freestyle project
Click on “OK”.
Step 4: Choose “Description” as “For develop branch, don’t publish it”.
Step 5: Go to the “GitHub” account & click on Code. Go to the “HTTPS” section & copy the link.
While Choosing “Label Expression” as “Slave”.
Step 6: In “Source Code Management”, choose “Git”. Put “Repo URL” in the “Repository URL” section.
Step 7: Choose “Branch” as “develop” in “Branch Specifier”.
Step 8: Choose “GitHub hook trigger for GITScm polling” in “Build Triggers”.
Step 9: Click on “Apply”. After clicking on “Apply”, choose “Save”.
Step 10: Your “Develop-Job” has been successfully created.
Step 11: Click on “Build Now” to create a build.
Step 12: The build has been successfully created.
Step 13: Go to the “Jenkins-Slave” Instance. Type cd /home/ubuntu/jenkins/workspace/Develop-Job. Press “Enter” from the keyboard.
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”.
Step 2: Choose the following options here:
Enter an item name: Master-Job
Option: Freestyle project
Step 3: Choose “Copy from” as “Develop-Job”. Click on “OK”.
Step 4: In “General”, put the description as “For master branch, publish it”.
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”.
Step 6: Click on “Apply” & “Save”.
Step 7: The “Master-Job” has been successfully 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
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/
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
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"
Step 4: The “Dockerfile” is not inserted into the “Master Branch”. Run the below-given command to view the branch:
git 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.
Step 6: Go to the “GitHub” account & view the “master” branch. You will notice that “Dockerfile” has been successfully pushed to the “master” branch.
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”.
Step 2: Click on “Build Steps”.
Step 3: Click on “Add build step”.
Step 4: 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
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”.
Step 7: Now, click on “Build Now”.
Step 8: Click on #6.
Step 9: The Docker container has been successfully 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.
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)
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”.
Step 13: Click on “#10”.
Step 14: Click on “Console Output”. Again, the container has been successfully created.
Step 15: Refresh the “IP Address” Again. The same website is shown again, meaning the job is working successfully.
Note: Before creating a pipeline, we have to disable the “GitHub hook trigger” option in both the “Develop-Job” & “Master-Job”.
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.
Step 2: Click on the “Edit” Icon & click on “Edit in place”.
Step 3: Now, we will replace “Intellipaat” with “Hello Intellipaat”.
Step 4: Click on “Commit changes”.
Step 5: Again, click on “Commit changes”.
Step 6: The file has been successfully saved.
B. Add post-build action in the “Develop” Job
Step 1: Go to “Develop” Job & click on “Configure”.
Step 2: Click on “Post-build Actions”.
Step 3: Click on “Add post-build action>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”.
C. Create a Pipeline to Trigger the “Master” Job via the “Develop” Job
Step 1: Click on the “+” sign.
Step 2: In “New View”, choose the following options:
Name: Case-Study-Pipeline
Type: Build Pipeline View
Click on “Create”.
Step 3: Choose the following options:
Description: Case-Study-Pipeline
Build-Pipeline View Title: Case-Study-Pipeline
Step 4: Remain other settings as it is shown in the image.
Step 5: Click on “OK”.
Step 6: Now, the pipeline has been successfully created. Click on the “Run” Button.
Step 7: After, clicking on “Run”, the “Master” job will be automatically triggered like this.
Step 8: Refresh the browser. The “Intellipaat” title has been replaced with the “Hello Intellipaat” title.
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