Jenkins Notes
Jenkins Notes
Table of Contents
1. Introduction to Jenkins
1.1. What is Jenkins?
1.2. Continuous Integration and Continuous Delivery (CI/CD) Explained
1.3. Bene ts of Using Jenkins for CI/CD
2
6.2. Building a Job with GitHub Plugin
6.3. Building a Job Using Trigger Builds Remotely (Authentication Token)
Conclusion
3
Jenkins
Jenkins is a powerful application that allows continuous integration and continuous
delivery of projects, regardless of the platform you are working on. It is a free source
that can handle any kind of build or continuous integration. You can integrate Jenkins
with a number of testing and deployment technologies. In this notes, we will explain
how you can use Jenkins to build and test your software projects continuously.
Automatic code integration: Developers frequently merge their code changes into a
central repository, where automated build and testing processes are triggered.
Automated build and tests: CI systems automatically compile the code, run unit tests,
and perform other validation checks to ensure the codebase remains stable.
4
Early feedback: By catching issues early, CI allows developers to fix problems quickly,
preventing the accumulation of bugs.
Key features of CD
● Automated deployment: The CD pipeline automates the deployment of code
changes to various environments, including staging and production, with
consistent configurations.
● Release orchestration: CD pipelines manage the entire release process,
coordinating tasks such as database updates and infrastructure provisioning.
● Rollbacks and monitoring: CD ensures that automated rollbacks are possible if
any issues arise during deployment. Monitoring is also integrated to track the
application's health post-deployment.
Benefits of CI/CD
● Faster time-to-market: CI/CD streamlines the development and deployment
process, reducing the time it takes to deliver new features and updates to
end-users.
● Higher software quality: Automated testing and validation catch bugs early,
leading to a more stable and reliable codebase.
● Risk reduction: Frequent integration and automated deployment enable faster
bug identification and recovery, minimizing risks associated with manual
processes.
● Increased collaboration: CI/CD encourages collaboration among developers,
testers, and operations teams, fostering a culture of continuous improvement.
5
Jenkins features of every tool:
3. Disable/Enable Job- It will be found under the status of a specific project. You
can use this feature to disable or enable the job build. If you want that nobody
can build the job except you then, you can disable the job. And whenever you
want to enable it you can do that too.
6
4. Build a Job concurrently- As you are aware you couldn’t build a job
concurrently. But if you want to do this then, you have to enable the feature that
will be found under the section of Description named -> Execute concurrent builds
if necessary.
5. Retry count- When your job is not built successfully. So you can retry as many
as you want but for that, you have to enable it to define time and number of
counts.
6. Throttle Builds- When you want that there should be a limited number of
builds asked then, you will use throttle builds. For eg, the Number of builds is 3
and the Time period is minute. Then there are only 3 builds that will be
successful and possible, not more than that.
7
Creating First Job
1. Click on New Item.
8
2. Enter the job name and choose freestyle project.
9
10
4. Click on Build Now
11
6. Now, To see the output of your created job. Click on the
Console Output.
7. The Output:
12
Change Jenkins Theme using Plugin
1. Click on Manage Jenkins -> Manage Plugins.
13
3. Now, that the plugin has been installed check the restart
Jenkins server and log in again.
14
4. Now, go to the Manage Jenkins again and configure the
Plugin by clicking on Configure System.
15
To change the Jenkins URL
Manage Jenkins -> Configure System
Enter the desired location or URL.
By Default URL-> http://localhost:8080/
16
2. Click on Create user. And enter the further details.
3. New User
17
Jenkins Role-Based Access Control
(RBAC)
1. Install the plugin named Role-based Authorization
Strategy by going into the Manage-Jenkins ->
Manage Plugins - Available and then search the
plugin name which is written above.
18
3. Now, the Manage and Assign roles option will
appear under the security section in the Manage
Jenkins.
19
5. Now, when you log in with another user e.g. Jon
Snow. You’ll get this.
20
7. If you give access by changing the roles and
assigning the roles to read and build jobs to the
other user, It will look like this.
21
8. If you add one more permission which is job read
then, the other can see the jobs.
22
GitHub Job without GitHub Plugin
23
2. Take Source code Management as None(for not
github).
24
3. Build Environment -> Check the Delete workspace before build
starts. It is checked because when you are going to build
25
4. The Output
26
GitHub Job with GitHub Plugin
1. Create a freestyle project.
27
The Output
28
How to build a job using Trigger
Builds remotely (Authentication
token)
1. Go to any Job.
29
2. Enter the token name inside the Trigger build
remotely section.
3. Now, cop the URL that was under the token name
field and paste in the new tab
30
4. Now, come to the Jenkins dashboard.
You will see the build has been automatically started.
Build Successful
31
Build Triggers
1. Build Job through Incognito
Mode and Terminal
Why?
As I am building a job through just a URL. But when I put the same URL in
incognito mode of any browser it asked me to log in again, to resolve that
issue. Follow the steps below:
32
2. Enter the URL which is written in the tab.
33
The build has been successful.
34
Through Terminal
1. Enter the same URL and make one change before the &
symbol. Use backslash (\).
35
2. Build after other projects are built
Why?
When you have to build your specific project after some desired
projects. You can use this Build Trigger.
36
2. Now, run the project that you entered under the Projects to
Watch section.
37
4. If you observe, Both build numbers are incremented by 1.
38
3. Build Job Periodically
Why?
When you have to build your job at a specific time or same interval e.g. every two
minutes. You can use Build periodically under the Build Triggers section.
1. Write the time interval the same as written in the cron job.
39
2. Write the command and apply then save it.
3. Here, you can check it out. The build is automatically done every two
minutes.
40
4. Poll SCM(Source Code
Management)
Why?
This Build trigger is the same as Build Periodically. But there is only one
difference which is, that the project will fetch from the GitHub
repository(mandatory) and if there is no commit in the repository then, the
job will not build. But, if there is any commit change in the GitHub
repository, then the job will build as a per-time scheduler which is also
known as Cronjob/Crontab.
41
42
2. Job is built automatically. But then no job build
43
4. As GitHub repository has a new commit. Now, it is building a job.
44
The Output
45
How to define variables globally
1. Go to the Manage Jenkins -> Configure System and scroll down then,
look for Global properties.
2. Now, Create a job and print that value of variable by passing the
environment variable.
46
3. Here, you can see the value of variable is appeared.
47
Parameterised in Job
1. Define the variable and pass the default value.
48
49
50
Console Output
51
Custom Workspace
1. Create a new job and do the configuration as given below:
52
2. Now, the directory has been created and file too.
To check it, go to your terminal.
53
Change display name and project
name
1. Go to in the Configure of the Specific Project-> Enter the desired
name that you want in the text field.
The Output
But the Project hasn’t changed. For that you have to click on the Rename
button which is left on the screen and enter the desired name.
54
Now, The project name has changed too.
55
Building Upstream and Downstream
Project
Upstream- Parent job
Downstream- Child job
That job that is triggering another job is known as the Parent job and that
job that is triggered by another job/parent job is known as the Child job.
56
57
Block build when downstream project is building
Here, the scenario is if you are building a child job then, you couldn’t build a
parent job at the same time. To build the parent job, you have to complete
the build of the child job first.
Complete Reverse of the previous one.
58
59
60
Jenkins Pipeline using Build Pipeline
61
3. Click on “+” to create the Build Pipeline.
62
4. Choose Type as “Build Pipeline view” and assign the
name of your Pipeline.
63
6. In the last, Click on the run button and the result will
be in front of you like this:
64
Continuous Deployment vs
Continuous Delivery
In Continuous Deployment, whenever the code changes to
an application is released automatically into the production
environment. There is no inclusion of human intervention
or manual click.
E.g
65
66
2. Click on “+” to create the Build Pipeline.
67
5. The Final Output:
68
Continuous Delivery
E.g,
69
70
2. Click on “+” to create the Build Pipeline.
71
5. Now, when you run the Pipeline then you will face like
this.
72
7. The Final Output:
73
74
Run Two Job Parallel in Jenkins
Pipeline
1. Create three Job, first will be GrandParent, the
second will be Parent job and third will be Child job
and assign the Post-build actions in Grand Parent Job
for Parent job and in Parent Job for Child Job as per
below:
75
76
6. Click on “+” to create the Build Pipeline.
77
8. Now, all you have to do is “Select the Initial Job” and
which will be your Parent job.
78
9. The Final Output:
79
Deploy WAR to Tomcat Server
through Jenkins (Automation)
Reference:
● Web Link
● Video Link
<role rolename="admin-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<user username="aman" password="mypassword" roles="admin-gui,manager-gui,manager-script"/>
80
2. Now, You have to Create Four Jobs:
a. HelloWorld-Test: This Job will perform the testing of
the Project which is a Spring Boot-based Project.
b. Hello-World-Build: This Job will perform the building of
the Spring Boot Project which will be .war file as in the
result.
c. HelloWorld-Deploy-Test: In this Job, the deployment
will be performed as a testing deployment to check
whether the Job is successful or not.
d. HelloWorld-Deploy-Prod: This job will perform the
same task that was performed by the last job
(HelloWorld-Deploy-Test) but it will be on the
Production level where the inclusion is present of
human intervention or manual clicks.
Now, All the jobs have their configuration, which is given below
sequentially:
a. HelloWorld-Test
81
82
83
b. Hello-World-Build:
84
85
c. HelloWorld-Deploy-Test
86
87
88
89
d. HelloWorld-Deploy-Prod
90
91
The Output
92
Creating Jenkins Slave
1. First of all, go into Manage Jenkins -> Manage Nodes and
Clouds.
93
3. Give the name to your slave and check the radio button of
Permanent Agent then, click on the Create button.
94
5. Enter the number of executors as per your choice, then enter
the Remote root directory which will be /var/jenkins then, the
launch type must be “Launch agents via SSH” Enter the
Host Name which will be the Public IP of your instance, then
add the credentials of your EC2 Instance (username=
ubuntu and password= aman@123) and
In the last Host, the Key verification Strategy must be
“Non-verifying Verification Strategy”.
95
96
6. Now go to that Slave and click on Relaunch agent.
97
8. You can check on the left side named Linux1.
98
Create Jenkins Pipeline as a Code
1. Create a Job that will be a Pipeline.
99
3. Now, when you build the job you’ll see look like this:
4. To see the output hover on the green box and click on the
logs.
100
Multi-Staging Pipeline as a Code
101
3. The Output:
102
How to run Commands in Pipeline as a Code
103
How to run multiple commands in Pipeline as a
code
104
The Output:
105
How to set the Environment variable Globally in
the Code Pipeline
106
How to set the Environment variable Locally in
Code Pipeline
The Output
107
How to take input from the users in the Code
Pipeline
The Output:
108
Pipeline Script of the Last Job
pipeline {
agent any
parameters {
string(name: 'username', defaultValue: 'Jenkins', description: 'Who is the Current User')
booleanParam(name: 'Graduated?', defaultValue: 'true', description: 'Are you Graduated')
choice(name: 'hobby', choices: ['Cricket','Chess','Football','Hockey'], description: 'Favourite Sport')
}
stages {
stage('String Value') {
input {
message "Do we start?"
ok "Yes, Let's do this"
}
steps {
sh 'echo $username'
}
}
stage('Boolean Value') {
steps {
sh 'echo $Graduated?'
}
}
stage('Choices Values') {
input {
message "Want to Continue?"
ok "Yeah! Definitely"
}
steps {
sh 'echo $hobby'
}
}
}
post {
always {
echo 'The Jenkins!!!!'
}
success {
echo 'The Final Acheivement is Here!!!'
}
failure {
echo 'Seriouslly! We got the Failure from the other End!!!!'
}
}
}
109
In conclusion, the above notes provide a beginner-level introduction to Jenkins and its
role in enabling Continuous Integration and Continuous Delivery (CI/CD) practices. By
understanding the fundamental concepts and features of Jenkins, beginners can
kickstart their journey towards automating their software development processes and
achieving faster, more reliable project deliveries.
For those seeking to advance their knowledge and skills in Jenkins, there is a world of
possibilities to explore. Jenkins offers a vast array of plugins and integrations with
various tools, allowing advanced users to tailor their CI/CD pipelines to meet specific
project requirements. By delving deeper into Jenkins' capabilities, experienced users
can optimize their workflows, integrate advanced testing and deployment strategies,
and harness the full potential of CI/CD for continuous improvement.
Moreover, Jenkins Pipelines offers an exciting avenue for advanced users to adopt a
"pipeline-as-code" approach, enabling better version control, collaboration, and
scalability. With declarative pipeline scripts, developers can encapsulate complex build
and deployment logic, making it easier to manage and maintain their CI/CD workflows
effectively.
While the notes provide a solid foundation for beginners, it is essential to continue
exploring Jenkins and other related technologies to stay up-to-date with the latest
advancements in CI/CD practices. Online tutorials, documentation, forums, and
community resources are valuable sources to deepen one's expertise and stay ahead in
the rapidly evolving landscape of DevOps and CI/CD.
Whether at the beginner or advanced level, embracing Jenkins as a pivotal tool in the
software development lifecycle empowers teams to automate, collaborate, and deliver
high-quality software efficiently. By leveraging Jenkins' capabilities and staying
curious, developers can continually refine their CI/CD processes and drive innovation in
the dynamic world of modern software development.
110