0% found this document useful (0 votes)
12 views73 pages

Jenkins CI/CD Pipeline Overview

The lecture covers Continuous Integration (CI) using Jenkins, detailing its architecture, including master-slave configurations for testing and job execution. It explains the differences between continuous delivery and deployment, and introduces Jenkins pipelines with both declarative and scripted syntax for automating CI/CD processes. Key features include environment variables, job parameters, and the ability to manage multiple jobs and stages efficiently.

Uploaded by

muthersaeed948
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views73 pages

Jenkins CI/CD Pipeline Overview

The lecture covers Continuous Integration (CI) using Jenkins, detailing its architecture, including master-slave configurations for testing and job execution. It explains the differences between continuous delivery and deployment, and introduces Jenkins pipelines with both declarative and scripted syntax for automating CI/CD processes. Key features include environment variables, job parameters, and the ability to manage multiple jobs and stages efficiently.

Uploaded by

muthersaeed948
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Software

Quality Engineering
Lecture 13
CI using Jenkins

Instructor:
Dr. Natalia Chaudhry,
Assistant Professor, Software Engineering Department, PU.
Agenda
• Continuous integration with Jenkins

2
CI
It lets the developers to commit changes to the source code in shared repository
several times a day or more frequent
Continuous delivery vs continuous deployment
Jenkins architecture
How the architecture
works?
Master slave architecture…
• Jenkins master uses multiple slaves (for various environments such as
ubuntu windows etc) for testing and generate test report.
demo
Add slave
• Move to Jenkins homepage/dashboard
• [Link]
• Go to Manage Jenkins->Nodes
• Select New Node
• Name it SlaveA or any other name
• Check permanent agent and Create

Labels represent a way of naming one or more slaves. We leverage this labeling system to tie the execution of a
job directly to one or more slave nodes.
powerful load-balanced Jenkins solutions
• When Jenkins discovers a job execution is pending, which is tied to a
label, it will attempt to locate any available slave nodes tagged with
that label that are not in use.
• If any nodes with that label are free Jenkins will run the job on the
available node.
• If no nodes are available, Jenkins will queue the job for the next
available node that has the specified .
• No of executers is the number of jobs a slave is able to run in parallel way
• Remote root directory
• Create workspace for slave. For that create a folder named slaveA_ws on your PC
• Place location path of this folder in Jenkins remote root directory
• To get option in launch method “…connect to master”. Goto dashboard->manage
jenkins.-> security-> put option as Random
• Launch by typing following commands for Windows (copy/paste) on
cmd
Configure
master and
slave on same
machine
Configure already created job (last lecture) and select Configure
Select Restrict where this project can be run
[Link]
Click on apply
Build now
Job executed
Jenkins scalability
• Working on multiple jobs simultaneously needs scalability
Distributed Jenkins Build
• To run heavy projects which gets build on regular basis on a single
machine is not a good option
• We need to offload load from master by configuring more slaves
• Jenkins master will distribute load to slaves
• Master and slaves communicate using tcp/ip protocol
Pipeline
• Build Pipeline plugin are collection of Jenkins jobs.
Creating simple pipeline using pipeline plugin
w/o jenkinsfile
Create job 1
**Freestyle project
Type following
sleep 5
echo "building p1"
Create job 2 (deploy)
**Freestyle project
Goto Dashboard->Manage Jenkins->Plugins
Select available plugins
Type build pipeline. The plugin gets visible. Install
Configure jobs to make pipeline
Configure job1 (build)
Post-build Actions-> Build other project
Type the title of job2 (deploy)
Apply and save
View of pipelines
+ sign
Move to “select initial job”
Press Run
Yellow means running
green means finished running
If build fails, set shell path using steps

In Manage Jenkins → Configure System → Shell, set the shell path as

C:\Windows\system32\[Link]
Demo Continuous delivery and deployment
Again create a job and link it with last ceated job (job 2)
Continuous deployment achieved
To achieve continuous delivery…edit job 2 (deploy) and select “build
manual” and set downstream project to third job
Pipeline (Summarized)
• Build Pipeline plugin are collection of Jenkins jobs. Jobs are chained
together. E.g.
Job 1: build, job 2: test, job 3: deploy
Limitations: complex pipelines hard to manage, maintenance cost increases
with the increase in number of processes, good for small count of jobs
Solution: Jenkins pipeline
• Jenkins pipeline: single platform that runs entire pipeline as a code
• suite of plugins which supports implementing and integrating continuous
delivery pipelines into Jenkins.
• The definition of a Jenkins Pipeline is written into a text file (called a
Jenkinsfile)
Declarative versus Scripted Pipeline syntax
A Jenkinsfile can be written using two types of syntax - Declarative and Scripted.

Declarative and Scripted Pipelines are constructed fundamentally differently.


Declarative Pipeline is a more recent feature of Jenkins Pipeline which:

provides richer syntactical features over Scripted Pipeline syntax, and

is designed to make writing and reading Pipeline code easier.


Key features of Jenkins Pipeline
• Code can be checked into a version control system
• Incorporate user input (users can interact, restart from saved checkpoint)
• Runs the jobs in parallel
You can create pipeline files using two main approaches: Declarative and Scripted pipelines.

pipeline {
1. Declarative Pipeline: agent any // Specifies where the pipeline should run (e.g., any available agent)

Declarative pipelines are recommended for stages {


most use cases as they provide a simpler and stage('Checkout') {
steps {
more concise way to define your CI/CD // Checkout your source code from a version control system (e.g., Git)
process. checkout scm
}
}
stage('Build') {
steps {
// Define your build steps (e.g., using Maven or Gradle)
sh 'mvn clean package'
}
}
stage('Test') {
steps {
// Define your testing steps (e.g., running unit tests)
sh 'mvn test'
}
}
stage('Deploy') {
steps {
// Define deployment steps (e.g., deploying to a server)
sh './[Link]'
}
}
}
}
Here's how you can create a declarative pipeline file:

1. Open your text editor or integrated development environment (IDE).

2. Create a new file with the extension .groovy (e.g., [Link]).

3. Copy and paste the above code into the file.

4. Customize the stages and steps to match your specific CI/CD process, including source code management, build
tools, testing, and deployment.

5. Save the file.

6. You can then configure your Jenkins job to use this pipeline file by specifying its location in your Jenkins job
configuration.
Scripted pipelines provide more fine-grained control node {
and flexibility but can be more complex to write. try {
stage('Checkout') {
checkout scm
}

stage('Build') {
sh 'mvn clean package'
}

stage('Test') {
sh 'mvn test'
}

stage('Deploy') {
sh './[Link]'
}
} catch (Exception e) {
[Link] = 'FAILURE'
throw e
} finally {
// Post-build cleanup or notifications can be added here
}
}
To create a scripted pipeline file:
[Link] steps 1 and 2 as mentioned above.
[Link] and paste the above code into the file.
[Link] the stages and steps to match your specific CI/CD process, including source
code management, build tools, testing, and deployment.
[Link] the file.
[Link] your Jenkins job to use this pipeline file by specifying its location in your
Jenkins job configuration.
Once you've created and configured your pipeline file, Jenkins will automatically execute
the defined steps whenever you trigger the job, allowing you to automate your CI/CD
process as code. Make sure to test and iterate on your pipeline as needed to ensure it
meets your requirements.
• Pipeline block is a user defined block which contains all the stages
• Agent instructs Jenkins to allocate an executor for the builds
• Stages contains all the work, each stage performs a specific task

Pipeline{
agent any
stages
{
stage (‘build’)
{
}
stage (‘test’)
{
}
stage (‘deploy’)
{
}

}
Create pipeline
Dashboard-> New item-> Pipeline
Move to pipeline section
Agents and stages are known as sections
Echo … are known as declaratives
Build now
Now adding a new job is simply copy pasting a new
stage..simple in contrast to basic build pipeline plugin
(advantage of Jenkins pipeline (pipeline as a code)
1. Run a command

type n step of a stage


sh “date”

2. Environment variables in Jenkins are global key-value


pairs that can be accessed and injected into a project.
Here are some of the roles of environment variables in
pipeline {
Jenkins:
agent any
•Storing sensitive data: Environment variables can be used to
store sensitive data, such as passwords. This data can then be environment {
accessed by build scripts or other tools without having to MY_SECRET_KEY = "!@#hello#@!"
hard code it into the project code. }
•Configuring the build environment: Environment variables
can be used to configure the build environment, such as the
path to the source code or the location of the build artifacts.
Boolean parameter added
pipeline {
agent any
3. Using Parameters in Jenkinsfile stages {
[Link] parameters stage('Setup parameters') {
steps {
[Link] parameter script {
properties([
[Link]-line string parameter parameters([
[Link] Parameter choice(
choices: ['ONE', 'TWO'],
name: 'PARAMETER_01'
),
booleanParam(
defaultValue: true,
description: '',
name: 'BOOLEAN'
),
text(
defaultValue: '''
this is a multi-line
string parameter example
''',
name: 'MULTI-LINE-STRING'
),
string(
defaultValue: 'scriptcrunch',
name: 'STRING-PARAMETER',
trim: true
)
])
])
}
} print "Parameter: ${param}, Value: ${value}"
}
}
}
4. Input

message : String
This parameter gives a prompt which will be shown to a human:

Ready to go?
Proceed or Abort

If you click "Proceed" the build will proceed to the next step, if you click "Abort" the build will be aborted.
Jenkins Input Step Example
pipeline {
agent any
stages {
stage ("Prompt for input") {
steps {
script {
[Link] = input message: 'Please enter the username',
parameters: [string(defaultValue: '',
description: '',
name: 'Username')]
[Link] = input message: 'Please enter the password',
parameters: [password(defaultValue: '',
description: '',
name: 'Password')]
}
echo "Username: ${[Link]}"
echo "Password: ${[Link]}"
}
}
}
}
Scripted pipeline
• Scripted Pipeline is the original pipeline syntax for Jenkins, and it is based on
Groovy scripting language.
• In Scripted Pipeline, the entire workflow is defined in a single file called a
Jenkinsfile. The Jenkinsfile is written in Groovy and is executed by the Jenkins
Pipeline plugin.
• Scripted Pipeline provides a lot of flexibility and control over the workflow, but it
can be more complex than Declarative Pipeline.
sh step to run shell commands

node {
stage('Build') {
// Build the application
sh 'mvn clean install'
}
stage('Test') {
// Run the tests
sh 'mvn test'
}
stage('Deploy') {
// Deploy the application
sh '[Link]'
}
}
Jenkins scripted pipeline has the On the other hand, a declarative pipeline can be
following skeleton: written by using more elements, as shown next:
node {
stage (‘Build’ { pipeline {
//... agent any
} stages {
stage (‘Test’) { stage(‘Build’) {
//... steps {
} //…
} }
}
stage (‘Test’) {
The script has the elements “pipeline”, “agent” and “steps”
steps {
which are specific to Declarative Pipeline syntax; “stage” is
//…
common to both Declarative and Scripted; and finally, node”
}
is specific for the Scripted one.
}
}
“Pipeline” defines the block that will contain all the script
}
content.
“Agent” defines where the pipeline will be run, similar to the
“node” for the scripted one.
“Stages” contains all of the stages.
Thank you

You might also like