Student Copy of CC Lab 8 Jenkins Manual
Student Copy of CC Lab 8 Jenkins Manual
Lab 08 – Jenkins
(Creating a DevOps Pipeline, CI/CD tool)
Introduction:
Welcome to lab 8! This simple exercise is designed to introduce you to Jenkins and continuous
integration.
Jenkins is an Open source, Java-based automation tool. This tool automates the Software
Integration and delivery process called Continuous Integration and Continuous Delivery.
What is Jenkins?
Jenkins supports various source code management, build, and delivery tools. Jenkins
provides features like Jenkins Pipelines which makes the delivery process very easy and
helps teams to adopt DevOps easily.
Overview of the Experiment
Prerequisites:
● Git installed on your system and a GitHub account also Follow this
tutorial to install and make yourself familiar with git.
● If you seriously and very very necessarily need help with issues , for
example it shouldn’t be less complex than “ sudo rm -rf /
–no-preserve-root” then you can raise a github issue and we’ll look into it
Task-1
Steps:
2. You will be given a Dockerfile, open a terminal in that Dockerfile folder.
3. Build the dockerfile using this command:“sudo docker build . -t
jenkins:YOUR_SRN” [Note: Omit sudo if working with Windows WSL
or MacOS]
4. Run your container using this command “sudo docker run -p 8080:8080
-p 50000:50000 -it jenkins:YOUR_SRN” (Expose any other port
for e.g. 8090:8080, if you are already using port 8080
for some other purpose).(Note down the password shown on
the terminal).
5.Open URL: localhost:8080 on your browser.
6. Enter the password shown on your terminal after running the container (You can
set the password to ADMIN later).
a. In case you did not note down the password displayed on the terminal, you
can find the password by connecting to the container (via “
sudo docker exec –it <container_id> /bin/bash”)
and checking the file /var/Jenkins_home
/secrets/initialAdminPassword inside the container.
7. Integrate GitHub to Jenkins: When prompted for plugin installation, click on
“Select Plugins to Install” and then search for GitHub and check the GitHub
option. (This step may take a few minutes to complete)
8.Take the necessary Screenshots as mentioned in Deliverables.
Task-2
Aim: Set up a job in Jenkins to connect to your repository and build C++ hello.cpp.
Deliverables:
1. Picture showing the console output after the build is successful
2. Picture showing the Stable state of the task in Build History of Jenkins (PS:ignore the multiple
jobs in the ss , you will be having a single job )
Steps:
1.Make changes to hello.cpp file if you wish. Complete prerequisite 3(If not done
already). Then, commit and push the Jenkins-main folder to your GitHub
repository. Open Git Bash, navigate to Jenkins-main folder. (Note:- Please make
sure to push both the main and dockerfile folders to your repository, otherwise
you may face errors in subsequent tasks). Use the following commands in Git
Bash
●git init
●git checkout -b main
●git remote add origin "Your repository’s URL"
●git add . # or specific files
●git commit -m "Describe this commit"
●git log # check if your commit is present
●git push -f origin main # forcepush just to be safe , else use -u
Aim: Set up a second job that automatically runs after the project builds. This is different
from the other job because this will not have a git repository - it doesn't even build
anything.
Just a note: In a real-life scenario you wouldn't run a program through a build job just like
this because I/O is not possible via this console. There are other tools people use at this step
like SeleniumHQ, SonarQube, or a Deployment. The point of this is to show
downstream/upstream job relationships.
Deliverable
1. Console output of second job [Note : it is a job that is started by a upstream project]
2. Status page of first job with your downstream project
1.Create a new Job in Jenkins, Click New Item in the left panel.
2.Enter a name for your second job as YOUR_SRN-2 (as this is your 2nd job), click
Freestyle Project, then OK.
3.Go immediately to the build step and select Execute Shell.
4. Enter the following Command
cd /var/jenkins_home/workspace/<the name of your first
project>/main/
./hello_exec
5. Click on Save.
6.Now, set your first job to call the second.
7. Go to your first job (i.e. YOUR_SRN-1) and open the Configure page in the
pull-down menu.
8.Scroll to bottom and add a Post-Build Action. Select Build other projects.
9.Enter the name of your second job.
10. Click on Save.
11.Run your first job.
12.Do this by clicking build now on the main page.
13.After that successfully builds, go, and check your second job. You should see it
successfully run.
first job :
second job :
14. Select a Build Job from History and go to the console log to see your program
output. If your program has run there, then you successfully set up a basic pipeline.
Task-4
Aim: Add a webhook trigger to your repository in order to automate builds in Jenkin
In the previous tasks, we were polling changes from the repository at an interval of every 5
mins. It is an expensive approach. There is, however, a better approach. By adding a
Webhook trigger to your repository and connecting it to your Jenkins server, the instant you
commit a change to your repository, your job is automatically executed.
Webhooks allow external services to be notified when certain events happen. When those
events happen, a POST request is sent to the designated URL.
Deliverables
2.Console Output of second job displaying the change made in hello.cpp file.
Steps:
1. This part of the assignment requires you to have bore installed and running
form the previous labs . We are just reusing such good functionality .
NOTE : the port should be the port that the jenkins container has exposed which is usually
8080
Copy the provided https URL.(ps : forwarding url) eg:
bore.smuz.me:xxxxx
Make sure to keep this running until the lab is done , basically don’t let stupidity
take over .
1. Go to settings of your GitHub repo, look for Webhooks→ Add webhook → Paste
the https URL in the Payload URL field and append /github-webhook/ to it. Keep
the default settings, however you can explore individual events to trigger this
webhook
eg : http://bore.smuz.me:xxxxx/github-webhook
also give a look at manage jenkins > system > github :
In simple words, Jenkins Pipeline is a combination of plugins that support the integration
and implementation of continuous delivery pipelines using Jenkins. The pipeline as Code
describes a set of features that allow Jenkins users to define pipelined job processes with
code, stored and versioned in a source repository.
- Pipelines are better than freestyle jobs, you can write a lot of complex tasks using
pipelines when compared to Freestyle jobs.
- You can see how long each stage takes to execute so you have more control
compared to freestyle.
- Pipeline is a Groovy based script that has a set of plug-ins integrated for
automating the builds, deployment and test execution.
- Pipeline defines your entire build process, which typically includes stages for
building an application, testing it and then delivering it.
- You can use a snippet generator to generate pipeline code for the stages where you
don't know how to write groovy code.
Task-5
Deliverables:
1. Code/script written to create basic pipeline using GitHub
repository Sample 1 (reference template only)
Sample 2
2.Output of working created pipeline, the screenshot should include
●Stage view / Execution status of pipeline with all stages succeeded
●Verify Declarative: Post Actions stage for handling failures.
3.Console Output of the Pipeline
4.Link to the created GitHub repository
Steps:
0. please go through the above templates and don’t use chatgpt , it’s for your own good … also you
can use the above templates itself , and in order to make sure you understand we’ve given you bad
res ss so you have to type it out yourself XD.
1.Create a job in Jenkins. Name the job/item as YOUR_SRN. Select Pipeline under projects.
2.Select Pipeline Script. Under sample pipelines, choose Hello World.
3. Save the pipeline and build. You should now have a basic working pipeline
containing 1 stage.
4.Write a Jenkinsfile to create a basic pipeline script:
• Go to your repository→Add file →create new file→Name the file
as Jenkinsfile.
● Write a script containing a Build, Test, and Deploy stage using Groovy. Also
add a post condition to display ‘pipeline failed’ incase of any errors within the
pipeline. Refer to attached scripts. Create a new working .cpp file, push it to
your repository.
●For Build stage :- Compile the .cpp file using shell script, build YOUR_SRN- 1.
●For the Test stage :- Print output of .cpp file using shell script.
●Explore pipeline syntax for references.
5.Configure the existing Hello World pipeline job.
6.In pipeline definition, choose Pipeline from SCM
7. Add the link to your GitHub repo in the URL section. Add branch
“*/main” in Branches to build (Do not delete the existing */master branch).
Save Pipeline .
8. Execute the pipeline and verify in the stage view whether all stages were
executed successfully.
9. Now edit your Jenkinsfile, make an intentional error in one of the stages and
commit. Execute the pipeline again, check if the expected stage fails and declarative
post action “pipeline failed” carried out successfully.
10.Take the required screenshot of the Stage View.
And that’s your Jenkins Lab done and congratulations on making it through . Hope you had fun, if you
need more fun checkout the this .
Credits : CC Faculty , CC TA group ‘25 , CC TA group ‘24 and C418 (do give it a shot…)