Jenkins Notes
Jenkins Notes
JENKINS
➔ Jenkins :- This is a tool used for implementing CI-CD
➔ Stage in CI-CD
The code downloaded in the previous stage had to converted into a setup file commonly known are
aritfact.To create this artifact jenkins uses certain build tools like ANT, Maven etc the artifact can be in the
format of a .jar,.war.ear file etc This stage is called as Continuous Build
1|Page
Jenkins MADHAV
Setup of Jenkins
1 Create 3 AWS ubuntu instances and Name them
Jenkins Server ,Qaserver, Prodserver
4 Install jdk
sudo apt-get install -y openjdk-11-jdk
6 Downloaded jenkins.war
wget https://get.jenkins.io/war-stable/2.361.3/jenkins.war
7 To start jenkins
java -jar jenkins.war
2|Page
Jenkins MADHAV
3 Install tomcat9
sudo apt-get install -y tomcat9
4 Install tomcat9-admin
sudo apt-get install -y tomcat9-admin
6 Restart tomcat
sudo service tomcat9 restart
Continuous Download
1 Open the dashboard of Jenkins
2 Click on New item---->Enter the item name as Development
3 Select Free style project-->OK
4 Go to Source code Management
5 Click on Git
6 Enter the GitHub URL where developers have uploaded the code
https://github.com/intelliqittrainings/maven.git
7 Click on Apply--->Save
Continuous Build
1 Open the dashboard of Jenkins
2 Go to the Development job--->Click on Configure
3 Go to Build section
4 Click on Add build step
5 Click on Top level maven targets
6 Enter the maven goal: package
7 Apply--->Save
3|Page
Jenkins MADHAV
Continuous Deployment
1 Open the dashboard of Jenkins
2 Go to Manage Jenkins
3 Click on Manage Plugins
4 Click on Available\e section
5 Search for Deploy to container plugin
6 Install it
7 Go to the dashboard of Jenkins
8 Go to the Development job--->Click on configure
9 Go to Post build actions
10 Click on Add post build action
11 Click on Deploy war/ear to container
war/ear file: **/*.war
Context path: testapp (This is the name that testers will enter in browser to access the
application)
Click on Add container
Select tomcat9
Enter tomcat9 credentials
Tomcat url: private_ip_qaserver:8080
12 click on Apply--->Save
Continuous Testing
1 Open the dashboard of Jenkins
2 Click on New items
3 Enter some item name (Testing)
4 Select Free style project
5 Enter the GitHub URL where testers have uploaded the selenium scripts
https://github.com/intelliqittrainings/FunctionalTesting.git
6 Go to Build section
7 Click on Add build step
8 Click on Execute shell
java -jar path/testing.jar
9 Apply--->Save
Day 5
5|Page
Jenkins MADHAV
User Administration in Jenkins
6|Page
Jenkins MADHAV
Alternate ways of setup of Jenkins
1 Update the apt repository
2 Install jdk:1.8
6 Install jenkins
7|Page
Jenkins MADHAV
Master Slave Architecture of Jenkins
➔ This is used distribute the work load to additional Linux servers called as slaves. This is used when we
want to run multiple jobs on jenkins parallelly.
Setup
c) Restart ssh
sudo service ssh restart
d) Connect to Master using git bash and login inti jenkins user
sudo su - jenkins
8|Page
Jenkins MADHAV
8 Click on Manage Jenkins--->Click on Manage Nodes and Clouds
14 Go to Launch Method and select "Launch agent via execution of command on master"
15 Click on Save
18 Go to General section
9|Page
Jenkins MADHAV
Pipeline as Code
➔ This is the process of implementing all the stages of CI-CD from the level of a Groovy script file called as
the Jenkinsfile
➔ Advantages
➢ 1 Since this is a code it can be uploaded into git and all the team members can review and edit the
code and still git will maintain multiple versions and we can decide what version to use
➢ 2 Jenkins files can withstand planned and unplanned restart of the Jenkins master
➢ 3 They can perform all stages of ci-cd with minimum no of plugins so they are faster and more
secure
➢ 4 We can handle real world challenges like if conditions, loops exception handling etc.ie if a stage in
ci-cd passes we want to execute some steps and it fails we want to execute some other
steps
10 | P a g e
Jenkins MADHAV
Syntax of Scripted Pipeline
node('built-in')
{
stage('Stage name in ci-cd')
{
Groovy code to implement this stage
}
}
pipeline
{
agent any
stages
{
}
}
}
11 | P a g e
Jenkins MADHAV
Scripted Pipeline
1 Go to the dashboard of jenkins
2 Click on New items
3 Enter item name as "Scripted Pipeline"
4 Select Pipeline--->Click on OK
node('built-in')
{
stage('Continuous Download')
{
git 'https://github.com/intelliqittrainings/maven.git'
}
stage('Continuous Build')
{
sh 'mvn package'
}
stage('Continuous Deployment')
{
deploy adapters: [tomcat9(credentialsId: '8cc7d40a-bab0-438d-8dc2-f0d886815228', path: '', url:
'http://172.31.16.84:8080')], contextPath: 'testapp', war: '**/*.war'
}
stage('Continuous Testing')
{
git 'https://github.com/intelliqittrainings/FunctionalTesting.git'
sh 'java -jar /home/ubuntu/.jenkins/workspace/ScriptedPipeline/testing.jar'
}
stage('Continuous Delivery')
{
12 | P a g e
Jenkins MADHAV
}
Declarative Pipeline
pipeline
{
agent any
stages
{
stage('ContinuousDownload')
{
steps
{
git 'https://github.com/intelliqittrainings/maven.git'
}
}
stage('ContinuousBuild')
{
steps
{
sh 'mvn package'
}
}
stage('ContinuousDeployment')
{
steps
{
deploy adapters: [tomcat9(credentialsId: '8cc7d40a-bab0-438d-8dc2-f0d886815228', path: '', url:
'http://172.31.16.84:8080')], contextPath: 'test1', war: '**/*.war'
}
}
stage('ContinuousTesting')
{
steps
{
git 'https://github.com/intelliqittrainings/FunctionalTesting.git'
sh 'java -jar /home/ubuntu/.jenkins/workspace/DeclarativePipeline/testing.jar'
}
}
stage('ContinuosuDelivery')
{
steps
{
input message: 'Waiting for Approval from the DM!', submitter: 'srinivas'
deploy adapters: [tomcat9(credentialsId: '8cc7d40a-bab0-438d-8dc2-f0d886815228', path: '', url:
'http://172.31.29.58:8080')], contextPath: 'prod1', war: '**/*.war'
13 | P a g e
Jenkins MADHAV
}
}
}
}
➔ Now if we make any changes to the code in GitHub then GitHub will send a notification to jenkins and
jenkins will run that job
14 | P a g e
Jenkins MADHAV
Declarative Pipeline with post conditions
pipeline
{
agent any
stages
{
stage('ContinuousDownload')
{
steps
{
git 'https://github.com/krishnain/mavenab.git'
}
}
stage('ContinuousBuild')
{
steps
{
sh 'mvn package'
}
}
stage('ContinuousDeployment')
{
steps
{
deploy adapters: [tomcat9(credentialsId: '376e01e8-e628-40d2-aaec-6452f707a3ff', path: '', url:
'http://172.31.20.211:8080')], contextPath: 'qaaapp', war: '**/*.war'
}
}
stage('ContinuousTesting')
{
steps
{
git 'https://github.com/intelliqittrainings/FunctionalTesting.git'
sh 'java -jar /home/ubuntu/.jenkins/workspace/DeclarativePipeline2/testing.jar'
}
}
}
post
{
success
{
input message: 'Required approvals', submitter: 'srinivas'
15 | P a g e
Jenkins MADHAV
deploy adapters: [tomcat9(credentialsId: '376e01e8-e628-40d2-aaec-6452f707a3ff', path: '', url:
'http://172.31.21.226:8080')], contextPath: 'myprodapp', war: '**/*.war'
}
failure
{
mail bcc: '', body: 'Continuous Integration is giving a failure msg', cc: '', from: '', replyTo: '', subject: 'CI
Failed', to: '[email protected]'
}
}
}
Exception Handling
➔ This is the process of overcoming a potential error and continuing the execution of the program, this is
implemented using try, catch
➔ The section of code that can generate an error is given in the try block if it generates an error the
control comes into the catch section
try
{
}
catch(Exception e)
{
}
Declarative Pipeline with exception handling
pipeline
{
agent any
stages
{
stage('ContinuousDownload')
{
steps
{
script
{
try
{
git 'https://github.com/krishnain/mavenab.git'
}
16 | P a g e
Jenkins MADHAV
catch(Exception e1)
{
mail bcc: '', body: 'Jenkins is unable to download from the remote github', cc: '', from: '',
replyTo: '', subject: 'Download Failed', to: '[email protected]'
exit(1)
}
}
}
}
stage('ContinuousBuild')
{
steps
{
script
{
try
{
sh 'mvn package'
}
catch(Exception e2)
{
mail bcc: '', body: 'Jenkins is unable to create an artifact from the downloaded code', cc: '',
from: '', replyTo: '', subject: 'Build Failed', to: '[email protected]'
exit(1)
}
}
}
}
stage('ContinuousDeployment')
{
steps
{
script
{
try
{
sh 'scp /home/ubuntu/.jenkins/workspace/DeclarativePipeline3/webapp/target/webapp.war
[email protected]:/var/lib/tomcat9/webapps/testapp.war'
}
catch(Exception e3)
{
mail bcc: '', body: 'Jenkins is unable to deploy into tomcat on the QAservers', cc: '', from: '',
replyTo: '', subject: 'Deployment Failed', to: '[email protected]'
17 | P a g e
Jenkins MADHAV
exit(1)
}
}
}
}
stage('ContinuousTesting')
{
steps
{
script
{
try
{
git 'https://github.com/intelliqittrainings/FunctionalTesting.git'
sh 'java -jar /home/ubuntu/.jenkins/workspace/DeclarativePipeline3/testing.jar'
}
catch(Exception e4)
{
mail bcc: '', body: 'Selenium scripts are showing a failure status', cc: '', from: '', replyTo: '',
subject: 'Testing Failed', to: '[email protected]'
exit(1)
}
}
}
}
stage('ContinuousDelivery')
{
steps
{
script
{
try
{
input message: 'Required approvals', submitter: 'srinivas'
deploy adapters: [tomcat9(credentialsId: '376e01e8-e628-40d2-aaec-6452f707a3ff', path: '',
url: 'http://172.31.21.226:8080')], contextPath: 'myprodapp', war: '**/*.war'
}
catch(Exception e5)
{
mail bcc: '', body: 'Jenkins is unable to deploy into tomcat on the prodservers', cc: '', from: '',
replyTo: '', subject: 'Delivery Failed', to: '[email protected]'
}
}
18 | P a g e
Jenkins MADHAV
}
}
}
}
node('built-in')
{
stage('ContinuousDownload')
{
try
{
git 'https://github.com/intelliqittrainings/maven.git'
}
catch(Exception e1)
{
mail bcc: '', body: 'Jenkins is unable to download from the remote github', cc: '', from: '', replyTo: '',
subject: 'Download Failed', to: '[email protected]'
exit(1)
}
}
stage('ContinuousBuild')
{
try
{
sh 'mvn package'
}
catch(Exception e2)
{
mail bcc: '', body: 'Jenkins is unable to create an artifact from the downloaded code', cc: '', from: '',
replyTo: '', subject: 'Build Failed', to: '[email protected]'
exit(1)
}
}
stage('ContinuousDeployment')
{
try
{
deploy adapters: [tomcat9(credentialsId: '376e01e8-e628-40d2-aaec-6452f707a3ff', path: '', url:
'http://172.31.20.211:8080')], contextPath: 'testapp', war: '**/*.war'
}
catch(Exception e3)
{
19 | P a g e
Jenkins MADHAV
mail bcc: '', body: 'Jenkins is unable to deploy into tomcat on the QAservers', cc: '', from: '', replyTo:
'', subject: 'Deployment Failed', to: '[email protected]'
exit(1)
}
}
stage('ContinuousTesting')
{
try
{
git 'https://github.com/intelliqittrainings/FunctionalTesting.git'
sh 'java -jar /home/ubuntu/.jenkins/workspace/ScriptedPipeline2/testing.jar'
}
catch(Exception e4)
{
mail bcc: '', body: 'Selenium scripts are showing a failure status', cc: '', from: '', replyTo: '', subject:
'Testing Failed', to: '[email protected]'
exit(1)
}
}
stage('ContinuousDelivery')
{
try
{
input message: 'Need approval from the DM!', submitter: 'srinivas'
deploy adapters: [tomcat9(credentialsId: '376e01e8-e628-40d2-aaec-6452f707a3ff', path: '', url:
'http://172.31.21.226:8080')], contextPath: 'prodapp', war: '**/*.war'
}
catch(Exception e5)
{
mail bcc: '', body: 'Jenkins is unable to deploy into tomcat on the prodservers', cc: '', from: '', replyTo:
'', subject: 'Delivery Failed', to: '[email protected]'
}
}
20 | P a g e
Jenkins MADHAV
Shared Libraries
➔ This is used for creating the Jenkins code ina reusable manner
➔ Create a GitHub repo and name its "libraries"
➔ In the repo create folder called "vars" and in vars create a file cicd.groovy
➔ In the file create the below code
def newDownload(repo)
{
git "https://github.com/intelliqittrainings/${repo}"
}
def newBuild()
{
sh 'mvn package'
}
def newDeploy(jobname,ip,appname)
{
sh "scp /var/lib/jenkins/workspace/${jobname}/webapp/target/webapp.war
ubuntu@${ip}:/var/lib/tomcat9/webapps/${appname}.war"
}
def runSelenium(jobname)
{
sh "java -jar /var/lib/jenkins/workspace/${jobname}/testing.jar"
}
@Library('mylibrary')_
pipeline
{
agent any
stages
{
stage('ContDownload')
{
steps
{
script
{
cicd.newDownload("maven.git")
}
21 | P a g e
Jenkins MADHAV
}
}
stage('ContBuild')
{
steps
{
script
{
cicd.newBuild()
}
}
}
stage('ContDeployment')
{
steps
{
script
{
cicd.newDeploy("DeclarativePipelinewithSharedLibrarires","172.31.32.68","testapp")
}
}
}
stage('ContTesting')
{
steps
{
script
{
cicd.newDownload("FunctionalTesting.git")
cicd.runSelenium("DeclarativePipelinewithSharedLibrarires")
}
}
}
stage('ContDelivery')
{
steps
{
script
{
cicd.newDeploy("DeclarativePipelinewithSharedLibrarires","172.31.32.210","prodapp")
}
}
}
}
}
22 | P a g e
Jenkins MADHAV
Scripted Pipeline with shared libraries
@Library('mylibrary')_
node('built-in')
{
stage('ContDownload')
{
cicd.newDownload("maven.git")
}
stage('ContBuild')
{
cicd.newBuild()
}
stage('ContDeployment')
{
cicd.newDeploy("ScriptedPipelinewithsharedlibraries","172.31.32.68","testapp")
}
stage('ContTesting')
{
cicd.newDownload("FunctionalTesting.git")
cicd.runSelenium("ScriptedPipelinewithsharedlibraries")
}
stage('ContDelivery')
{
cicd.newDeploy("ScriptedPipelinewithsharedlibraries","172.31.32.210","prodapp")
}
}
23 | P a g e
Jenkins MADHAV
Multi Branch Pipeline
➔ Generally developers create multiple branches and upload code related to various functionalities on
these branches We have to configure Jenkins in such a way that it triggers CI-CD process for all these
branches parallelly.
➔ To do this we need to have a copy of Jenkins file on each branch and then based on the instructions in
the Jenkins file all the stages have to be triggered
Developers Activity
1 Clone the maven repository
git clone https://github.com/intelliqittrainings/maven.git
5 Create a jenkins file and put the stages of CI that should happen
on master branch
vim Jenkinsfile
3 Select MultiBranchPipeline--->OK
6 Apply--->Save
25 | P a g e