1.Jenkins WORD
1.Jenkins WORD
Jenkins
This is a tool used for implementing CI-CD
Stage in CI-CD
==================
Stage 1 (Continuous Download)
-----------------------------
Whenever developers upload some code into the Git repository
(A Git repository is a virtual storage of your project.It allows you
to save versions of your code, which you can access when neede)
Jenkins will receive a notification and it will download all that
code.This is called as Continuous Download.
================================================================
Setup of Jenkins
====================
1 Create 3 AWS ubuntu instances and name then
JenkinsServer,QAServer,ProdServer
7 To start jenkins
java -jar jenkins.war
3 Install tomcat9
sudo apt-get install -y tomcat9
4 Install tomcat9-admin
sudo apt-get install -y tomcat9-admin
5 Edit the tomcat-users.xml file
cd /etc/tomcat9
sudo vim tomcat-users.xml
Delete all the content from the file and add the below content
<tomcat-users>
<user username="intelliqit" password="intelliqit" roles="manager-
script"/>
</tomcat-users>
6 Restart tomcat
sudo service tomcat9 restart
================================================================
Day 3
================================================================
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 Clcik 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 Aplly--->Save
================================================================
Continuous Deployment
===========================
1 Open the dashboard of Jenkins
2 Go to Manage Jenkins
3 Click on Manage Plugins
4 Click on Availabl\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
================================================================
Day 4
================================================================
Continuous Testing
=======================
1 Open the dashboard of Jenkins
2 Click on New item
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
================================================================
Copying artifacts from Development job to Testing job
==========================================================
1 Open the dashboard of Jenkins
2 Click on Manage Jenkins--->Manage plugins
3 Go to Availbale section--->Search for "Copy Artifact" plugun
4 Click on Install without restart
5 Go to the dashboard of Jenkins
6 Go to the Development job--->Click on Configure
7 Go to Post build actions
8 Click on Add post build actions
9 Click on Archive the artifacts
10 Enter files to be archived as **\*.war
11 Click on Apply--->>Save
12 Go to the dashboard of jenkins
13 Go to the Testing job---->Click on configure
14 Go to Build section
15 Click on Add build step
15 Click on Copy artifacts from other project
16 Enter project name as "Development"
17 Apply---->Save
================================================================
Stage 5 (Continuous Delivery)
=============================
1 Open the dashboard of jenkins
2 Go to Testing job--->Configure
3 Go to Post build actions
4 Click on Add post build action
5 Click on Deploy war/ear to container
war/ear files: **\*.war
contextpath: prodapp
Click on Add container
Select tomcat9
Enter username and password of tomcat9
Romcat url: private_ip_of_prodserver:8080
6 Apply---->Save
================================================================
Day 5
================================================================
================================================================
2 Install jdk:1.8
sudo apt-get install -y openjdk-8-jdk
6 Install jenkins
sudo apt-get install -y jenkins
================================================================
Setup
=====
1 Create a new AWS ubuntu20 instance
d)Connect to Master using git bash and login inti jenkins user
sudo su - jenkins
This will copy the content of the public keys to a file called
"authorised_keys" on the slave machine
Connect to slave using git bash
15 Click on Save
18 Go to General section
================================================================
Day 7
================================================================
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
3 They can perform all stages of ci-cd with minimum number of plugins
so they are more faster and secure
================================================================
1 Scripted Pipeline
2 Declarative Pipeline
================================================================
Scripted Pipeline
======================
1 Go to the dashboard of jenkins
2 Click on New item
3 Enter item name as "ScriptedPipeline"
4 Select Pipeline--->Click on OK
node('built-in')
{
stage('ContinuousDownload')
{
git 'https://github.com/intelliqittrainings/maven.git'
}
stage('ContinuousBuild')
{
sh 'mvn package'
}
stage('ContinuousDeployment')
{
deploy adapters: [tomcat9(credentialsId: '8cc7d40a-bab0-438d-
8dc2-f0d886815228', path: '', url: 'http://172.31.16.84:8080')],
contextPath: 'testapp', war: '**/*.war'
}
stage('ContinuousTesting')
{
git
'https://github.com/intelliqittrainings/FunctionalTesting.git'
sh 'java -jar
/home/ubuntu/.jenkins/workspace/ScriptedPipeline/testing.jar'
}
stage('ContinuousDelivery')
{
input message: 'Need approvals from the DM!', submitter:
'srinivas'
deploy adapters: [tomcat9(credentialsId: '8cc7d40a-bab0-438d-
8dc2-f0d886815228', path: '', url: 'http://172.31.29.58:8080')],
contextPath: 'prodapp', war: '**/*.war'
}
}
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'
}
}
}
}
================================================================
Scheduling the job for a particular date and time
======================================================
================================================================
POLL SCM (source code management)
==================
This is a process where Jenkins will check the remote github for any
new commits
================================================================
Webhooks
===========
This is used to send notifications from github to jenkins
Whenever any code changes are done and that is checkdin into
github, webhook will send an immediate notifiction to Jenkins
and Jenkins will trigger the job
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
================================================================
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'
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 contol comes into the catch sextion
try
{
}
catch(Exception e)
{
}
================================================================
}
}
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]'
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]'
}
}
}
}
}
}
Scripted Pipeline with Exception Handling
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)
{
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]'
}
}
Shared Libraries:
====================
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"
}
================================================================
DeclarativePipeline with Sahred Libraries
=========================================
@Library('mylibrary')_
pipeline
{
agent any
stages
{
stage('ContDownload')
{
steps
{
script
{
cicd.newDownload("maven.git")
}
}
}
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")
}
}
}
}
}
@Library('mylibrary')_
node('built-in')
{
stage('ContDownload')
{
cicd.newDownload("maven.git")
}
stage('ContBuild')
{
cicd.newBuild()
}
stage('ContDeployment')
{
cicd.newDeploy("ScriptedPipelinewithsharedlibraries","172.31.32.68","t
estapp")
}
stage('ContTesting')
{
cicd.newDownload("FunctionalTesting.git")
cicd.runSelenium("ScriptedPipelinewithsharedlibraries")
}
stage('ContDelivery')
{
cicd.newDeploy("ScriptedPipelinewithsharedlibraries","172.31.32.210","
prodapp")
}
================================================================
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.
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
================================================================
=========================== THE END ============================