Jenkins
Jenkins
Jenkins
Jenkins
Introduction
Continuous Integration (CI)
Continuous Delivery (CD)
Continuous Deployment (CD)
Installation
In Linux Server
Plugin Management
• Deploy to container
• Deploy WebLogic
• Maven Integration
• Safe Restart
• Next Build Number
• JACOCO
• SSH Agent
• Email Extension
• SonarQube Scanner
• Audit Trail Plugin
• Schedule Build
• Artifactory Plugin
• Cloud Foundry
• Blue Ocean
• Publish Over SSH
• ThinBackup
• Build Name Setter
• Convert To Pipeline
Create View
Jenkins Security
• Create Users (Default Admin)
• Provide the specific access Jenkins
• Provide the access to specific access to specific projects
Create Master/Slave
Jenkins Backup
Jenkins Migration
Optional Topics
• Jenkins Home Directory Change in RHEL 7.5 Version
• Jenkins CLI
• Integrate the Urban Code Deploy server with Jenkins
• Deploy the App into IBM Cloud
• Slack integration
-------------------------------------------------------------------------------------------------------------------------------------
Introduction
Jenkins, is an open source Continuous Integration, cross-platform tool written in Java. Kohsuke
Kawaguchi is Creator of the Jenkins CI server in 2004. Initially, it was called Hudson, but in 2011 it
was renamed to Jenkins because of disputes with Oracle.
The tool simplifies the process of integration of changes in to the project and delivery of fresh builds
to users.
Continuous Integration: Continuous Integration (CI) is the process of automating the build and
testing of code every time a team member commits changes to version control.
(OR)
Continuous Integration is a development practice where developers integrate their code into a
shared remote repository frequently, preferably several times a day. Each integration is verified by
an automated build (including test) to detect integration errors as quickly as possible.
CI Flow
CI – Benefits
• Immediate bug detection
• No integration step in the Software Development lifecycle
• A deployable system at any given point
• Record of evolution of the project
Continuous Delivery: Any and every successful build that has passed all the relevant automated
tests and quality gates can potentially be deployed in to production via fully automated one click
process.
Continuous Deployment: The practicing of automatically deploying every successful build directly
into production without any manual steps knows as Continuous deployment.
(OR)
It is closely related to Continuous Integration and refers to keeping your application deployable at
any point or even automatically releasing to a test or production environment if the latest version
passes all automated tests.
• Integrate with many different Version Control Systems (GitHub, CVS, SVN, TFS …)
• Generate test reports
(JUnit)
• Push the builds to various artifact repositories
• Deploys directly to production or test environments
• Notify stakeholders of build status
(Through Email)
Benefits of Jenkins
ü Its an open source tool with great community support.
ü Easy to install and It has a simple configuration through a web-based GUI, which speeds up
the Job
ü It has around 1000+ plugins to ease your work. If a plugin does not exist, just code it up and
share with the community (https://plugins.jenkins.io/).
ü Its built with Java and hence, it is portable on all major platforms.
ü Specifically, for a test only project, it is used to schedule jobs for regression testing without
manual intervention and hence monitor infrastructural and functional health of a application.
It can be used like a scheduler for integration testing and also can be used to validate new
deployments/environments on a single click on a Build now button.
The diagram below depicts that Jenkins is integrating various DevOps stages:
TBD (ToBeDocument)
-------------------------------------------------------------------------------------------------------------------------------------
1 Jenkins Yes
2 Cloudbees Jenkins No
2 Bamboo No
3 Cruise Control Yes
4 Travis CI Yes and Paid also
5 Circle CI Yes and Paid also
6 GitLab CI Yes and Paid
7 TeamCity Yes and Paid
Jenkins Installation
Ø Jenkins is java based CI tool, so we need to install jdk/jre before installing.
Ø Pre-Requisite Software: Java (Check weather java is installed or not with java -version
command)
-------------------------------------------------------------------------------------------------------------------------------------
Create the project/job in Jenkins
Step 1: Login into the Jenkins, go to the Jenkins dashboard left side top corner, click on New Item.
Step 2: Enter the project name in Enter an item name input box and select the Freestyle project
and click on OK Button.
Freestyle project: This is the central feature of Jenkins. Jenkins will build your project combining
any SCM and any build system.
A Free-Style project is a project that can incorporate almost any type of build. The Free-Style project
is the more "generic" form of a project. You can execute shell/dos scripts, invoke ant, and a lot more.
Majority of the plugins are written to use the free-style project.
Maven project: A maven project is a project that will analyze the pom.xml file in greater detail and
produce a project that's geared towards the targets that are invoked. The maven project is smart
enough to incorporate build targets like the javadoc or test targets and automatically setup the
reports for those targets.
different build machines. We will be looking at how to configure multiconfiguration build jobs later on
in the book.
Monitor an external job: The “Monitor an external job” build job lets you keep an eye on non-
interactive processes, such as cron jobs.
Specify when and how your build should be triggered. The following example polls the Git repository
every 5 min. It triggers a build, if something has changed in the repo.
Error:
Select any Job, which we need to configure Email notification ---> Click on Configure ---> Select the
Post-build Actions section.
We can enable to attach the build logs while sending mail, as follows.
Step 2: Select the job which you need to enable hook and click on Configure ---> In Build Triggers
Section enable the Poll SCM
And provide the values as follows.
GitHub webhook
Settings --> Webhooks --> Add webhook , Once you click on Add webhook url, it will ask the
Payload URL, give the Jenkins url and Content type as follows.
-------------------------------------------------------------------------------------------------------------------------------------
To restart Jenkins manually, you can use either of the following URLs:
(jenkins_url)/safeRestart - Allows all running jobs to complete. New jobs will remain in the queue to
run after the restart is complete.
Ex: http://13.233.230.247:8080/safeRestart
(jenkins_url)/restart - Forces a restart without waiting for builds to complete.
Ex: http://13.233.230.247:8080/restart
(OR)
You can install one plug called SafeRestart, once installed it will give one option Jenkins dashboard
as follows.
-------------------------------------------------------------------------------------------------------------------------------------
Disable Build:
A disabled Build will not be executed until you enable it again. This option often comes in handly to
suspend a build during maintenance work or major refactoring.
-------------------------------------------------------------------------------------------------------------------------------------
Once the project is configured in Jenkins then all future builds are automated. It has basic reporting
features like status and weather reports (job health).
-------------------------------------------------------------------------------------------------------------------------------------
jenkins : This is the default Jenkins home directory (may be .hudson in older installations) and it will
be placed in user’s home directory (C:\Users\MITHUN_ADMIN\ ---> Windows & /Users/mithunreddy/
--> MAC and /var/lib/jenkins à Linux).
Jenkins home directory contains the below sub directories and configuration files (.xml).
+- jobs
+- [JOBNAME] :Sub directory for each job
+- config.xml : Job configuration file
+- latest : Symbolic link to the last successful build)
+- builds
+- [BUILD_ID] : for each build one build id
+- build.xml : build result summary
+- log : log file
+- changelog.xml (change log)
+- logs ()
+- nodes ()
+- plugins : This directory contains all the plugins that you have installed.
+- secrets ()
+- updates : This is an internal directory used by Jenkins to store information
about available plugin updates.
+- userContent : You can use this directory to place your own custom content onto your Jenkins
server. You can access files in this directory at
http://localhost/jenkins/userContent (if
you are running Jenkins on an application server) or
http://localhost:8080/userContent (if you are running in stand-alone mode).
+- users : If you are using the native Jenkins user database, user accounts will
be stored in this directory.
+- war : This directory contains the expanded web application. When you start
Jenkins as a stand-alone application, it will extract the web application into
this directory.
Home directory: By default, Jenkins stores all of its data in this directory on the file system.
Under the Advanced section, you can choose to store build workspaces and build records
elsewhere.
Workspace Root Directory: Specifies where Jenkins will store workspaces for builds that are
executed on the master.
Build Record Root Directory: Specifies where Jenkins will store build records on the file system.
This includes the console output and other metadata generated by a build.
System Message: This message will be displayed at the top of the Jenkins main page.
# of executors: It shows the ow many builds run at a time. E.g.: If give 2, here two builds are
running.
Labels:
Quiet period:
Naming Strategy
Strategy
Default ---> This is the default configuration and allows the user to choose
any name they like.
Pattern ----> Define a pattern (regular expression) to check whether the job
name is valid or not. Forcing the check on existing jobs, will
allow you to enforce a naming convention on existing jobs - e.g.
even if the user does not change the name, it will be validated
with the given pattern at every submit and no updates can be
made until the name confirms.
This option does not affect the execution of jobs with non-
compliant names. It just controls the validation process when
saving job configurations.
Global properties
Environment variables
Tool Locations
SonarQube servers
etc….
Manage Jenkins ---> Manage Plugins ---> Select the Plugin name (HTML Published plugin) --->
Install Without Restart
Plugin Management
• Safe Restart
• Next Build Number
• Email Extension
• SonarQube Scanner
• Maven Integration
• Schedule Build
• Artifactory Plugin
• Cloud Foundry
• Blue Ocean
• Deploy to container
• Maven Integration
• JACOC
• SSH Agent
• Publish Over SSH
• ThinBackup
• Build Name Setter
• Convert To Pipeline
• JobConfigHistory: This plugin saves a copy of the configuration file of a job (config.xml) for
every change made and of the system configuration. You can also see what changes have
been made by which user if you configured a security policy.
• Repository browser
• Role-based Authorization Strategy:
-------------------------------------------------------------------------------------------------------------------------------------
Port number change for Jenkins
By default, 8080 is the default port, change from 8080 something like 8082 as follow.
Once you change the port, restart the jenkins service by using below command.
#service jenkins restart
-------------------------------------------------------------------------------------------------------------------------------------
Create the Maven project/job in Jenkins
Method 1:
Install the Maven Integration Plugin and follow the below steps.
Create the Job using Freestyle project and in the Build section click on Add build step and select the
Invoke Top level Maven targets.
Method 2:
Install the Maven Integration plugin and follow the below steps.
Once you click on OK, you will come to jobs configuration page as follows.
http://localhost:8080/configureTools/
Possible Errors
Solution1
Set the class path for Java.
Solution2
Go to the Jenkins Dashboard ---> Click on Manage Jenkins ---> Global Tool Configuration ---> in
JDK section give the full path where u have installed the Java.
------------------------------------------------------------------------------------------------------------------------------------
Jenkins - Security
Click on Manage Jenkins ---> Manage Users ---> Create User ---> Provide the below details
Username:
Password:
Confirm password:
Full name:
E-mail address:
Click on Create User
Click on Manage Jenkins ---> Manage Users ---> click on below Gear icon one circle with cross
symbol
It will ask Are you sure about deleting the user from Jenkins? confirmation message Click on --->
Yes
Note: TBD
Project-based Matrix Authorization Strategy is an authorization method using which we can define
which user or group can do what actions on which job. This gives us a fine-grained control over
user/group permissions per project.
To Enable the Project-based Matrix Authorization Strategy need to configure in Jenkins as follows.
Step 1: Click on Manage Jenkins and choose the ‘Configure Global Security’ option.
Step 3: Under Authorization, select “Project-based Matrix Authorization Strategy” and add 2 or 3
users, one administrator (say devops) and a regular user (say user1 and user2).
All the checkboxes present besides users are for setting global permissions. Select all
checkboxes against admin user to give admin full permissions.
For user1, we are selecting read permissions under jobs. With this, user1 would now
have read permission to view all jobs which we would be creating later on.
We have to provide read permission under “Overall” category to any regular user
otherwise the user won’t be able to see anything after login.
All the checkboxes present besides users are for setting global permissions. Select all
checkboxes against admin user to give admin full permissions. For user1, we are
selecting read permissions under jobs. With this, user1 would now have read
permission to view all jobs which we would be creating later on. We have to provide
read permission under “Overall” category to any regular user otherwise the user won’t
be able to see anything after login.
-------------------------------------------------------------------------------------------------------------------------------------
Below scenario will applicable in Matrix based security
Solution:
Click on Manage Jenkins ---> Configure Global Security ---> User/group to add: Enter the user
Name and click on Add button and --->
Enable the appropriate feature ---> Click on Save Button.
-------------------------------------------------------------------------------------------------------------------------------------
Jenkins Build Status Icon Colours
-------------------------------------------------------------------------------------------------------------------------------------
Deploy the Application Through Script
cp $WORKSPACE/war/SampleAntProject.war
/Users/bhaskarreddyl/BhaskarReddyL/Softwares/Running/apache-tomcat-9.0.6/webapps/
echo "Deployed the build artifact into tomcat server successfully"
Windows
echo "Starting to copy the build"
copy %WORKSPACE%\war\SampleAntProject.war C:\\apache-tomcat-8.5.23\webapps\
echo "Copied the build to tomcat"
Note: If we want to deploy in Tomcat, which is installed in any remote machine, use below lines of
code.
-----------------------------------------------------------------------------------------------------------------------------------
cp %JENKINS_HOME%\jobs\%JOB_NAME%\builds\%BUILD_NUMBER%\log
C:\Users\windows7\Downloads\newfolder\
-------------------------------------------------------------------------------------------------------------------------------------
Note: Once you entered all the details click on TEST CONNECTION. IF connection is
succeeded you will see the message like Found Artifactory <<Version>>.
-------------------------------------------------------------------------------------------------------------------------------------
Jenkins – Metrics and Trends
There are various plugins which are available in Jenkins to showcase metrics for builds which are
carried out over a period of time. These metrics are useful to understand your builds and how
frequently they fail/pass over time. As an example, let’s look at the ‘Build History Metrics plugin’.
This plugin calculates the following metrics for all of the builds once installed
Mean Time To Failure (MTTF)
Mean Time To Recovery (MTTR)
Standard Deviation of Build Times
-------------------------------------------------------------------------------------------------------------------------------------
Enable LDAP security to Jenkins
-------------------------------------------------------------------------------------------------------------------------------------
http://www.scmgalaxy.com/tutorials/complete-guide-to-use-jenkins-cli-command-line
Jenkins CLI
Jenkins has a built-in command line interface (CLI) that allows users and administrators to access
Jenkins from a script or shell environment. This can be convenient for scripting of routine tasks, bulk
updates, troubleshooting, and more.
Advantages of Jenkins CLI:
• Easier
• Faster
• Memory management
• Automation tasks.
Pre-Requisites
a) Jenkins server should run.
b) Enable security as follows.
Go to Jenkins dashboard in Home page ( e.g http://localhost:8080/ ) -> Manage Jenkins
-> Configure Global Security -> Click on “Enable security” checkbox
You can also configure “Access Control” and “Authorization” option in Global Security page.
Click on Jenkins-cli.jar.
Method 2
Example: http://localhost:8080/jnlpJars/jenkins-cli.jar
Here
Copy into any folder as follows
Go to the directory where Jenkins-cli.jar is there and run the below command to get the help.
While executing above command if you see Enter passphrase, follow the below configuration.
(Manage Jenkins ---> Configure Global Security ---> enable the Enable Security ---> Apply and Save.)
Manage Jenkins ---> Configure System --->SSH Public Keys (Enter here any value, same value u can
use in CLI)
-------------------------------------------------------------------------------------------------------------------------------------
Jenkins Pipeline Project
Required Plugins
1) Pipeline Maven Integration Plugin
2) JUnit Attachments Plugin
3) Task Scanner Plugin
In Jenkins Pipeline project, we will use one file called Jenkinsfile, in this file we will write groovy code
to build process.
We will write Jenkinsfile in 2 ways.
1) Declarative way
2) Scripted way.
-------------------------------------------------------------------------------------------------------------------------------------
Jenkins Multi Branch Pipeline Project
Required Plugins
1) Pipeline: Multibranch
-------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------
Jenkins Master-Slave setup
Note: Suppose if you don’t see “Launch agent via Java Web Start” option, do the below
configurations.
Manage Jenkins ---> Configure Global Security ---> enable the TCP port for JNLP agents
(by default, it is Disabled.)
Once you click on Save you will see the Nodes and Master detail, and select the Node which we
have created and click on configure.
You will see below screen and click download the slave.jar file.
Go to the path where slave.jar copied and run the below command.
Create one Freestyle project/any kind of project and select the Restrict where this project can be run
and select the Node which you have crated.
-------------------------------------------------------------------------------------------------------------------------------------
Jenkins Home Directory Change in RHEL 7.5 Version
---------------------------------------------------------------------------
sudo su -
service jenkins status
service jenkins stop
#mkdir -p /opt/mithuntechnologies
cp -r /var/lib/jenkins/ /opt/mithuntechnologies/
Issue:
Solution – Windows OS
Go to the Jenkins dashboard, Click on Manage Jenkins -à Global Tool Configuration
In Git option,
Give the Gitbash installed path in Path to Git executable text filed as follows.
Solution – Linux
Issue:
In Build step, give the build file name as in below screen shot.
Issue:
Solution:
Go to the Manage Jenkins ---> Global Tool Configuration ---> Ant ---> Ant Installations...
-------------------------------------------------------------------------------------------------------------------------------------
Installation Issues:
Issue 1: Offline
Solution
Once you changed from https to http, you need to restart the Jenkins.
Issue
Solution
If you see this error, generate SSH or PAT and use these keys instead of password.
#journalctl -xe
Solution
Issue:
Solution:
Issue:
Solution:
vi /etc/sysconfig/jenkins
-------------------------------------------------------------------------------------------------------------------------------------
Resources:
http://www.tothenew.com/blog/jenkins-implementing-project-based-matrix-authorization-strategy/ --->
User Access
https://support.cloudbees.com/hc/en-us/articles/216118748-How-to-Start-Stop-or-Restart-your-
Instance