Jenkins
Introduction to Jenkins
Jenkins is an open-source automation server used primarily for continuous integration and
continuous delivery (CI/CD). It automates tasks like building, testing, and deploying software, making
it a vital part of modern DevOps pipelines.
Why Jenkins?
● Open Source: Jenkins is free to use and can be customized.
● Extensible: Thousands of plugins are available to integrate Jenkins with other tools.
● Cross-platform: Jenkins runs on various operating systems including Windows, Linux, and
macOS.
● Easy to set up: Jenkins can be easily installed and configured, especially with its wide support
for plugins.
Relation Between Jenkins and Hudson
● Hudson was the predecessor to Jenkins. Jenkins was originally created as Hudson, but due to
conflicts within the project community in 2010, it was renamed Jenkins. Hudson is no longer
actively maintained, and Jenkins has become the preferred tool.
● Jenkins: A fully supported, open-source CI/CD tool.
● Hudson: An earlier version of Jenkins with a similar purpose.
History of Jenkins
Jenkins was originally created by Kohsuke Kawaguchi in 2004. It started as a part of the Hudson
project but was rebranded as Jenkins in 2011 due to a split with Oracle, which was the primary
backer of Hudson at the time.
Why Jenkins is so Popular
● Large Community: Jenkins has a large, active community of contributors and users, leading
to continuous updates, plugins, and improvements.
● Ease of Use: Its web-based interface and ease of configuration make Jenkins a popular
choice for CI/CD.
● Integration Support: Jenkins integrates with a wide variety of tools, including Git, Maven,
Docker, Kubernetes, and more.
● Plugin Ecosystem: Jenkins supports over 1,500 plugins, making it highly customizable.
Features of Jenkins
● Pipeline as Code: Jenkins supports defining CI/CD pipelines as code using DSL
(Domain-Specific Language).
● Extensive Plugins: Hundreds of plugins to integrate Jenkins with external tools.
● Distributed Builds: Jenkins can distribute builds across multiple machines (master-slave
architecture).
● Real-time Build Logs: View build progress and logs in real-time.
● Customizable Dashboards: Personalized dashboard views to monitor various jobs.
Jenkins Architecture
Jenkins uses a master-slave architecture:
● Master: The main server that controls the build process.
● Slaves (Nodes): Machines that Jenkins uses to distribute the workload of building and testing
applications.
Jenkins Prerequisites
● Java: Jenkins requires Java (JDK) to run.
● Web Server: Jenkins is typically accessed via a web browser.
● Maven (optional): If you use Maven for builds.
● Git (optional): For version control integration.
Continuous Integration (CI)
Continuous Integration is the practice of automatically integrating code into a shared repository
frequently. Jenkins automates the process by running tests, building applications, and ensuring that
integration does not break the codebase.
Jenkins Workflow
The Jenkins workflow is the sequence of steps Jenkins follows to process a build:
1. Source Code Management (SCM): Fetches code from a repository (e.g., Git).
2. Build: Compiles and packages the code.
3. Test: Runs unit tests and integration tests.
4. Deploy: Deploys the application to the target environment.
5. Notify: Sends feedback about the build status (success/failure).
Ways of CI
● Centralized CI: The build and tests are run on a single server.
● Distributed CI: Jenkins can distribute builds across different machines to speed up the
process.
Benefits of CI
● Early Detection of Issues: Bugs and integration issues are detected early.
● Faster Development Cycle: Continuous testing and building lead to faster development.
● Improved Code Quality: CI ensures that code changes are automatically tested, improving
code quality.
Why Only Jenkins?
● Flexibility: Jenkins can be integrated with a wide variety of tools and services.
● Scalability: Jenkins can scale from a small team to large organizations with many projects.
● Ease of Use: Jenkins offers a user-friendly interface for configuring builds.
Git for Windows
● Git Installation: Git can be installed on Windows to integrate with Jenkins. After installation,
configure it in Jenkins for SCM operations.
● Git Bash: A terminal for Git operations on Windows.
Java Installation & Configuration
● Install Java: Download and install the JDK.
● Configure in Jenkins: In Jenkins, set up the Java JDK path under "Manage Jenkins" > "Global
Tool Configuration."
Maven Installation & Configuration
● Install Maven: Download and install Maven.
● Configure in Jenkins: Set the Maven installation path in Jenkins under "Manage Jenkins" >
"Global Tool Configuration."
Jenkins Installation & Configuration
● Install Jenkins: Download the Jenkins WAR file and run it, or install via package managers for
your OS (e.g., apt-get, yum, or brew).
● Initial Setup: Access Jenkins via [Link] and unlock the admin
account using the initial password.
Freestyle Project
A Freestyle Project is the simplest type of Jenkins project. It allows you to configure the build process
through the web UI.
● Steps to Create:
1. Go to the Jenkins Dashboard.
2. Click “New Item”.
3. Select “Freestyle Project” and provide a name.
4. Configure source code management, build triggers, and build steps.
Maven Project by Jenkins
Jenkins can create Maven projects, which are configured similarly to Freestyle projects, but
specifically for building Maven-based applications.
Jenkins Plugins
● Purpose: Jenkins plugins extend Jenkins' functionality to integrate with other tools.
● Common Plugins:
○ Git Plugin: Integrates Git with Jenkins.
○ Maven Plugin: Provides Maven support for Jenkins jobs.
○ Email Extension Plugin: Sends customizable email notifications.
Scheduled Projects
You can schedule Jenkins projects to run at specific times using cron expressions or simple intervals.
Source Code Polling (Git)
Jenkins can poll a Git repository to detect changes and trigger builds automatically based on changes
in the repository.
Related/Linked Projects
● Upstream & Downstream Projects: Jenkins allows you to link jobs together so that an
upstream job triggers a downstream job when it completes.
CI/CD Pipeline
A CI/CD pipeline is a series of automated steps to build, test, and deploy an application. Jenkins is
commonly used to implement these pipelines.
Jenkins Views
Views are customized dashboards to organize and view multiple jobs in Jenkins. Users can create new
views to filter projects based on certain criteria (e.g., All Jobs, Failed Jobs, etc.).
User Management
Jenkins allows for user authentication and authorization. You can define permissions for different
users, ensuring that only authorized individuals can configure jobs or access certain information.
Jenkins Slaves
● Slaves or Nodes: Machines connected to the Jenkins master where the builds are executed.
● Purpose: Distribute the workload of building and testing across multiple machines.
Tomcat Web Server
You can configure Jenkins to deploy web applications to a Tomcat server as part of the CI/CD
pipeline.
Minimum Hardware Requirements
● CPU: At least 1 core.
● RAM: At least 512 MB.
● Disk Space: Minimum 2 GB of free space for Jenkins files.
Recommended Hardware Requirements
● CPU: Multi-core CPU for distributed builds.
● RAM: 2 GB or more.
● Disk Space: At least 10 GB for Jenkins files, plugins, and build artifacts.
Setup Environment Variables
Environment variables like JAVA_HOME, MAVEN_HOME, and others need to be set up in the Jenkins
environment to ensure that Jenkins can find tools like Java and Maven.
Jenkins Terminologies
● Master: The main Jenkins server that controls the build process.
● Slave (Node): Remote machines that execute build jobs.
● Job: A project or task that Jenkins executes, such as building or deploying an application.
● Executor: A computational resource on the master or slave where the job runs.
● Build: The process of compiling code and creating artifacts.
● Plugin: Extensions that add functionality to Jenkins.
Jenkins Dashboard Overview
● Job Listing Section: Displays all Jenkins jobs.
● Build Queue Section: Shows jobs waiting to be built.
● Build Executor Status: Displays the status of Jenkins executors.
● Jenkins Menu Sections:
○ Manage Jenkins: Manage configurations, plugins, and system settings.
○ People: Displays Jenkins users and their activities.
○ Build History: Shows the history of builds for each job.
Creating Jobs in Jenkins
● Naming Projects: A unique name is required for each Jenkins project.
● Project Description: Describes the purpose of the job.
● Disable Build Systems: Jobs can be temporarily disabled to prevent them from running.
Source Code Management (SCM)
Jenkins integrates with version control systems like Git and Subversion for source code management.
You can specify the repository URL and credentials in job configurations.
Build Triggers
Jenkins can trigger jobs in multiple ways:
1. Poll SCM: Polls the repository for changes at specified intervals.
2. GitHub Hook Trigger: Triggers build when code is pushed to a GitHub repository.
3. Scheduled Builds: Use cron syntax to schedule jobs to run at specific times.
CI/CD Pipeline in Jenkins
A CI/CD pipeline in Jenkins automates the entire process of building, testing, and deploying software.
You can define a pipeline using Jenkins Pipeline DSL or by using Declarative Pipeline in the
Jenkinsfile.
Practicals
installations
install ec2
launch an ec2 linux with 30 storage with hvm
connect your server
search on google install jenkins
refer these
[Link]
go here
>sudo wget -O /etc/[Link].d/[Link] \
[Link]
>sudo rpm --import [Link]
sudo yum upgrade && sudo yum update
# Add required dependencies for the jenkins package
sudo yum install java-17-amazon-corretto-devel
>java –version
sudo yum install jenkins
>sudo systemctl start jenkins
to access it using web
for password
>sudo cat /var/lib/jenkins/secrets/initialAdminPassword
>install suggested plugins
Click on install suggested plugins
>username mantu
>password Allen@786
it will download the requirement plugins
click on continue and your are done
>go to manage plugins >plugins >add>searche amazon ec2
create a new job
>on dashboard
>create new
>name it
>click on okay click on freestyle
>what all thing you wanna do in your job
goto build steps
>execute shell
click on save
now click on build now
and now you can check on your output
to modify it ‘
click on configure add changes
and modify it accordingly
console output
To add parameters
Again go to configure
>select string
echo "hello buddy"
echo "hello buyyd " >>/tmp/mantu/[Link]
sh /tmp/mantu/[Link]
echo $name
echo $allen
Now it asking build with parameters
Modify according to you and build
And it run successfully
You can add choice parameters as well
Again build with parameters
Scheduling jobs
For this create new jobs
You will get one option
>triggers>build periodically
Set your cron job
Click on build now
On your server you will see the output
now it will run automatically so you don't have to run it again and again
To get project from github and run it on jenkins
>create a github repo and add some python code in it
we need plugins to connect it
>manage jenkins
if it throw an error install git on your server
>sudo yum install git -y
>plugins >>plugins>installed plugins >search “git” you will see plugins
already installed
Now create new
>source code >git
Click on save
And build
and it will clone the whole repo
It did all the stuff but it self
Where does all the file goes
>dashboard>workspace
Here you will see all your file
All your files stored in jenkins
Cd /var/lib/jenkins
Here you will see logs and file that are download from the github to
server
To run your code
Add in your bash shell
python3 [Link]
Save and build now
Your file run successfully
Auto github repo polling
Add some changes in your file in github
It will automate the file
Now will use poll SCM
It will check your git gub time to tie in every minute
Add some changes in your github file
Now whenever you makes some change in you your github file then only
it will run your file or job
Email notify from jenkins
What if your code didn't
work properly
>if it has some error then we need some notify right ?
Then it will happen using email
Will use gmail/smtp
Goto manage jenkins >system
Scroll to down
Goto your gmail account
>app password
>add app name
Password
yclu jaxx mibt ougs
ansari0mantasha786@[Link]
now go to jenkins manage plugins>system >scroll to last you will see email
select smtp server
Add port no 465
Test it before you save
You will received a mail from jenkins
Now to send email notification
>configure >scroll to last >Post-build Actions
>email notification
Now to verify add some wrong thing in your code
As you can see your build fail
And you got the mail for the same
Even if your job succeed then only you will received mail to
How to access server remotely using jenkins
>it will help to manage or edit the file on remote server
launch 2 server
for access create ssh key in your master
and copy the [Link] file in slave authorize and save it
now if you try to access it then you can without even a password
Your both server should be able to communicate
to access it using ssh
>manage jenkins > plugins >available plugins
Now we need to to configure ssh key on jenkins
>manage jenkins >system
>scroll down
Add your master private key here
>ssh server >add
click on test connecting
Now lets create a job for it
Now goto>build stage >Send files or execute commands over SSH
here you can see your server added
create one file and add n the workspace inside ssh
if you didint mention the remote then it will paste it in the home dir
keep rest as it is and click on save
it will show successfully check it console output
it will show one file transferred
here it will create a dir and paste the file
now to run your script
again it will transferred file
it run successful and created a file in the server
if you wanted to transfer multiple files
now save and run
if you want to exclude soem file then click on exclude file add name
website update on slave server
here we will pull code from github and upload it in the serverB
in your slave server install
>sudo yum install httpd -y
start the server in your slave
>sudo systemctl start httpd
paste the ip in the browser
now to deploy the code
get your html code from github
now go to jenkins and create a new job
copy the link from github and paste it in the jenkins
[Link]
in the branch option make it blank
now goto build stage file share over ssh
here menting the file name
and now thar file will be copy to the user home dir to paste it into the /path add the exec command
now refresh the slave page
User management in jenkins
if you want other to manage your jenkins job that you created then we required these
manages jenkins >user > click on create user
field these info
by default every user have full permission
we need to install one plugins and install it
to assign the role
>dashboard>manage jenkins >security
>authorization
>here its return that logged in user can do anything
here you will see an option that role based strategy select and click on save
now if the user logged in from the console so he didn't have any permission
now again to manage jenkins > >manage and assign roles
here you can create multiple role
so let's create read only roles
read-only > click on save
now to assign role to user
>click on assign role >add user and
> allen > add read only permission and click on save
now login into user and you can see permission
Environment Variable
we can use predefined variable that is define by jenkins
list of env variable
[Link]
lets create a simple job and and user env variable
go to .build step > execute shell
>echo $WORKSPACE
click on build >>check its output
let's see how to create a env variable
manages jenkins> system .
scroll down you will see env variable inside global properties
click on same so these is hoe you can add env variables
and here is the output
Maven with jenkins
maven is build automation tool used primarily for java projects step to do that
● working with git and maven
● build jar using maven
● testing
● deploy jar locally
● graphical representation of result
● send email notification
go to google sample maven projects
we will refer these for project
create new project free style > freestyle
git > link of your repo
[Link]
save and build now
to install maven we need to install plugging
>manage plugins >available . MAVEN INTEGRATION >install
>manage plugins >tools
scroll down to bit you will see an option >>. maven installation
click on add
click on save
now again go to your job
goto add build step > invoke top-level maven targets
>select the version
goals
here you need to provide steps paste it here from git hub
-B -DskipTests clean package
>now click on build now
if it throw an error
then check if your server has javac or not
>javac
rpm -qa | grep -i jdk
>sudo yum install java-17-openjdk-devel.aarch64
>enter password and install it
javac
now it will install javac
now again click on build now
now you will see building jar
now if you go to your maven-dem othen you will see an folder target folder
you will see your jar file
to test the application
configure
add build step top level maven target
select your maven > just type test
to build now
to run your jar file
configure > execute shell
echo deployment shell >>> java -jar path to your jarfile
now you will see your file running >>> save and run
in the console you will see the output
now if you want to see the graphical representation of your
now go to your work space here you will see the reports folder
here you will see .csv file in that you will get the logs and data
now if you want to check it in the graph
configure
>post build action >publish junit test result report
now give the path
target/surefileefg-repfdg/*.xml
save and click on build now again
go to maven your job you will see some changes
Archive artifact
configure
target/surefileefg-repfdg/*.xml
>archive the artifact >advance >archive artifact only if the build is success
click on build now
and here you will see the last successful artifacts
you can directly download the artifact from here
CICD Pipeline
now will see pipeline
now let's get started >> create a new job and >>> select pipeline
pipeline script
select hello world click on save and run
it gui is a lot diff from the freestyle
even you can see logs
multi step pipeline
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Hello World'
}
}
stage('Test') {
steps {
echo 'Hello World'
stage('Dev') {
steps {
echo 'Hello World'
go to build one and click on pipeline console
if you wanna run shell command
now run and check
pipeline {
agent any
environment{
NAME="mantu"
AGE="25"
password="mantu@23456"
PASS=credentials('pass')
stages {
stage('Build') {
steps {
sh '''
echo $NAME
echo $AGE
echo $PASS
'''
timeout(time:5, unit:'SECONDS'){
sh 'sleep 30'
}
}
stage('Test') {
steps {
echo 'Hello World'
retry(3){
sh 'echo fiogdfigd'
stage('Dev') {
steps {
echo 'Hello World'
sh "sleep 30"
for password
manage jenkins> credentials
credentials > system >global credentials >>add credentials
>secret text> id password secret > password >save it
now you can use password
pass=credentials(‘PASS’)
wanna know more about it then refer docs
[Link]
jenkins with maven java application pipeline