[go: up one dir, main page]

0% found this document useful (0 votes)
53 views14 pages

Scripted Pipeline and Declarative Pipeline

The document provides an overview of Jenkins, an open-source automation tool for Continuous Integration and Continuous Delivery, focusing on its pipeline features. It explains two types of Jenkins pipelines: Declarative and Scripted, detailing their syntax and execution methods. The document also outlines the steps to create and run a Jenkins pipeline, emphasizing the importance of these pipelines in facilitating continuous delivery across various environments.

Uploaded by

Kisham
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)
53 views14 pages

Scripted Pipeline and Declarative Pipeline

The document provides an overview of Jenkins, an open-source automation tool for Continuous Integration and Continuous Delivery, focusing on its pipeline features. It explains two types of Jenkins pipelines: Declarative and Scripted, detailing their syntax and execution methods. The document also outlines the steps to create and run a Jenkins pipeline, emphasizing the importance of these pipelines in facilitating continuous delivery across various environments.

Uploaded by

Kisham
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
You are on page 1/ 14

7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

Digital Varys

Continuous Delivery with Jenkins Pipeline.


Leave a Comment / Build and Package, DevOps, Release and Operation / By DigitalVarys

Table of Contents 
1. About Jenkins
1.1. Jenkins pipeline
1.2. Types of Jenkins pipeline
1.3. Run the pipeline

https://digitalvarys.com/jenkins-pipeline/ 1/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

1.4. Scripted Pipeline Execution


1.5. Declarative Pipeline Execution

About Jenkins

Jenkins is an Open source, Java-based automation tool. This tool


automates the Software Integration and delivery process called
Continuous Integration and Continuous Delivery. Jenkins support various
popular Version control system, Software build, and delivery tools. Over
the Years, Jenkins become giant in CICD process, especially new features
like Jenkins Pipelines (Scripted and Declarative Pipeline) makes the
delivery process very easy and help Team to adopt DevOps easily.

Jenkins pipeline

Jenkins Pipeline is a stack of Jenkins plugins and other tools which helps implementing and
continuous integration and delivery pipelines. In Jenkins, Pipelines are written in DSL code
which implements this continuous integration and delivery pipeline jobs.

Get the pipeline plugin from the Jenkins plugin market place and install into the Jenkins
instance. For the same, Go to Manage Jenkins > Manage Plugins. > Available tab, search for
“build pipeline”.

This Jenkins pipeline can be built using Web UI or Scripted Jenkins le. This Jenkins le is written
with Groovy DSL and updated in con guration page on the Jenkins job. Overall, Jenkins le can
also be called pipeline as code.

Types of Jenkins pipeline

There are two type of Jenkins pipeline based on which format the Jenkins le is written.

1. Declarative pipeline

2. Scripted pipeline

Declarative Pipeline

https://digitalvarys.com/jenkins-pipeline/ 2/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

The Declarative pipeline is a new feature that is added to create the pipeline. This is basically
written in a Jenkins le which can be stored into a source code management system such as
Git. Declarative pipelines is an ideal solution for the simple continuous delivery pipeline as it has
very limited and pre-de ned structure.

Lets see the structure and syntax of the Declarative pipeline.

The Agent is where the whole pipeline runs. Example, Docker. The Agent has following
parameters:

any – Which mean the whole pipeline will run on any available agent.
none – Which mean all the stages under the block will have to declared with agent
separately.
label – this is just a label for the Jenkins environment
docker – this is to run the pipeline in Docker environment.

The Declarative pipeline code will looks like this:

pipeline {
agent { label 'node-1' }
stages {
stage('Source') {
steps {
git 'https://github.com/digitalvarys/jenkins-tutorials.git''
}
}
stage('Compile') {
tools {
gradle 'gradle4'
}
steps {
sh 'gradle clean compileJava test'
}
}
}
}

https://digitalvarys.com/jenkins-pipeline/ 3/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

Scripted Pipeline

The scripted pipeline is a traditional way of writing the Jenkins pipeline as code. Ideally, Scripted
pipeline is written in Jenkins le on web UI of Jenkins. Unlike Declarative pipeline, the scripted
pipeline strictly uses groovy based syntax. Since this, The scripted pipeline provides huge
control over the script and can manipulate the ow of script extensively. This helps developers
to develop advance and complex pipeline as code.

Let us see the structure and syntax of the scripted pipeline

Node Block:

Node is the part of the Jenkins architecture where Node or agent node will run the part of the
workload of the jobs and master node will handle the con guration of the job. So this will be
de ned in the rst place as

node {
}

Stage Block:

Stage block can be a single stage or multiple as the task goes. And it may have common stages
like

Cloning the code from SCM


Building the project
Running the Unit Test cases
Deploying the code
Other functional and performance tests.

So the stages can be written as mentioned below:

stage {
}

https://digitalvarys.com/jenkins-pipeline/ 4/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

So, Together, the scripted pipeline can be written as mentioned below.

stage {
}
So, Together, the scripted pipeline can be written as mentioned below.
node ('node-1') {
stage('Source') {
git 'https://github.com/digitalvarys/jenkins-tutorials.git''
}
stage('Compile') {
def gradle_home = tool 'gradle4'
sh "'${gradle_home}/bin/gradle' clean compileJava test"
}
}

Run the pipeline

Now lets con gure the Jenkins pipeline. Following are the steps to create Jenkins Pipeline.

Step 1:

Select “New Item” from the dashboard of the Jenkins instance

Step 2:

Name the item and select pipeline as the style of the job item.

https://digitalvarys.com/jenkins-pipeline/ 5/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

Step 3:

Scroll down and select the de nition of the Pipeline. Here we get two options.

1. One is the Pipeline script from SCM which is for Declarative Pipeline

2. Another is the Pipeline script which is for Scripted Pipeline that we will write directly on UI.

If it is the Pipeline Script, which will be written directly on UI, Need to write it on Script place
(Scripted pipeline)

If we are selecting the Pipeline script from SCM de nition, then we need to select the Git > SCM
Repository URL > Credentials. (Declarative Pipeline).

https://digitalvarys.com/jenkins-pipeline/ 6/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

Declarative Pipeline from SCM

In the Declarative pipeline, We need to select the repository of the Jenkins le, Credentials,
Branch and the path of the Jenkins le as shown in the above screenshot.

Now let us see how these jobs are executed.

Scripted Pipeline Execution

For testing, we are going to use the following code for the pipeline.

node {
for (i=0; i<2; i++) {
stage "Stage #"+i
print 'This is the stage #' +i
if (i==0)
{
echo 'Running on Stage #0'
}

https://digitalvarys.com/jenkins-pipeline/ 7/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

else {
build 'Tutorial-pipeline'
echo 'Running on Stage #1'
}
}
}

Basically,

This Script will run two stages as we mentioned in the “For loop”.
Next, the Condition will execute the stages mentioned in the block when i’ = 0.
Next condition will execute another job from the Jenkins when ‘i’ not equal to 0.

So once we run the job we will get output in the dashboard (As mentioned in the screenshot)

Scripted Pipeline Execution Result

Declarative Pipeline Execution

Let us take the Jenkins le from the SCM. In our case, we will take this from the Github. The
script inside the Jenkins le will look like this

pipeline {
agent any
stages {

https://digitalvarys.com/jenkins-pipeline/ 8/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

stage('One') {
steps {
echo 'I am executing stage 1'
}
}
stage('Two') {
steps {
input('In pipeline, We take decision. Can we proce
}
}
stage('Three') {
steps {
echo "Running Stage Three"
}
}
stage('Four') {
steps {
echo "Running another test job"
}
}
}
}

So basically, this script is having 4 stages. And each stage is running in any de ned agent. For
the Demo purpose, I made it so simple. Once we execute this, we can see the result in the
dashboard as shown in the screenshot.

https://digitalvarys.com/jenkins-pipeline/ 9/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

Declarative Pipeline Execution Result

Ideally, we can connect any Build job with these scripts to upstream or downstream the
pipelines. So this pipelines will provide continuous delivery right from the small changes from
the major changes that are happening from the source code. Especially the pipeline is very
useful when we connect multiple environments together and deliver the software. Like, Build –
Test – Deploy on every environment. say Dev environment to QA or UAT to Stage to Prod.

Stay tuned. We will see the detailed tutorial of the Scripted and Declarative pipelines and tools
like Blue oceans in upcoming articles.

0
Article Rating

← Previous Post Next Post →

 Subscribe  Connect with

Be the First to Comment!


https://digitalvarys.com/jenkins-pipeline/ 10/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

{} [+] 

0 COMMENTS  

Deal Of the Day

FREE

How To Crack Job Interview?

https://digitalvarys.com/jenkins-pipeline/ 11/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

Learn to market yourself in an #interview


through the "How To #CrackJobInterview"
course available for #free for 3 days using
coupon code "GETFREEINT"

GETFREEINT

Valid until May 11, 2020

GO TO THE DEAL

Recent Posts

Complete Terraform Tutorial Part – 4 – Terraform State

Complete Terraform Tutorial Part – 3 – Terraform CLI

Complete Terraform Tutorial Part – 2 – Terraform Con guration File.

Complete Terraform Tutorial Part – 1 – Introduction

How to Con gure and Running Jenkins Behind Apache Reverse Proxy?

Categories

App Development (17)

Cloud (9)

DevOps (48)

Build and Package (11)

DevOps and Business (1)

Monitoring and Analytics (1)

Planning and Development (4)

Release and Operation (15)

Testing and QA (3)

DevSecOps (4)

How To's (12)

https://digitalvarys.com/jenkins-pipeline/ 12/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

Project Management (2)

Whispers of DigitalVarys (7)

Services

Advertise
Partnership
Showcase

Deals

Offers and Deals


Learning Materials
Contact Us

Copyright © 2020 | Digital Varys


Contact Services

https://digitalvarys.com/jenkins-pipeline/ 13/14
7/1/2020 Jenkins Pipeline - Scripted Pipeline and Declarative Pipeline.

https://digitalvarys.com/jenkins-pipeline/ 14/14

You might also like