Table of Contents
Introduction
Jenkins Pipeline is the workflow that implements the Continuous Delivery pipeline with the Jenkins features, tools, and plugins. There are two different ways to create a Jenkins pipeline. One is Declarative Pipeline, and another is a Scripted Pipeline. In this article, we will see how to create a Jenkins Declarative pipeline. This tutorial will give you insight into the structure and directives of declarative pipeline and syntax. Let’s Get into the Jenkins Declarative Pipeline Tutorial.
Jenkins, From Zero To Hero: Become a DevOps Jenkins Master
Become a DevOps Master learning Jenkins & integrations with powerful tools like Docker, Ansible, AWS, GIT & more!
Limited time 97% offer on Udemy.
Structure of Declarative Pipeline.
As part of The Jenkins Declarative Pipeline Tutorial, We will discuss the structure of the Declarative Pipeline first. Declarative Pipeline starts with the “Pipeline” label. Basically, this pipeline label opens the block where the following directives and blocks are present.
Let us see all these directives and blocks one by one in this Jenkins Declarative Pipeline Tutorial.
Pipeline
The Pipeline is a block where all other blocks and directives are declared. This is the starting of the Declarative pipeline. So, Pipeline label will be declared as follows.
pipeline { agent… stages… step.. }
Agent
Jenkins is popular for one of its best features called distributed build process to the agent nodes. Jenkins build pipeline plugin ensures the same feature present in the pipeline that are created in the Declarative method. Normally, the agent will be declared at the top-level as a global agent declaration. So, the Agent label is ideally mentioned with the following parameters.
Stages
Stages is the section which contain one or more stage tag. So, In this section we can write the part of continuous delivery process.
Example:
pipeline { agent any stages { stage('Example') { ... ... } } }
Stage
This is the section that contains steps section or agent section which is isolated for the particular stage and other directives. So, A Stage section will have a command that runs processes like Build, Deploy, Test, or any other Continuous Delivery process.
Example:
pipeline { agent any stages { stage('Example') { agent { docker 'maven:3-alpine' } steps { sh 'mvn compile' } } } }
Steps
A stage can have multiple commands to run for completing the continuous delivery process. basically, Step is the set of commands that performs an action and It will be executed one by one.
Example:
pipeline { agent any stages { stage('Example') { steps { sh 'mvn compile' } } } }
Environment (Defined at stage or pipeline level)
The environment is the directive that contains the Key-value pairs of the environment variable that should be available for the steps that are going to be executed in the stages.
Ideally, Environment directive supports a special helper method called credentials type which is very helpful for dynamically passing the credentials into the steps. So, Following are the types of credentials that can be passed into the environments
Example:
pipeline { agent any environment { example key = 'example value' } stages { stage('Example') { environment { Secret_key = credentials('secret-text') } } } }
Input (Defined at stage level)
Input Directive is declared inside stage and it will help us getting input by prompting users. Basically, The stage section will wait until any Input is submitted. So, the following are the configuration options.
Example:
pipeline { agent any stages { stage('Example') { input { message "Can we Proceed?" ok "Yes" submitter "Digital Varys" parameters { string(name: 'PERSON', defaultValue: 'DigiralVarys', description: 'Member') } } steps { echo "${PERSON}, is proceeding..." } } } }
options (Defined at stage or pipeline level)
Options derivatives will provide options for configuring the pipeline with in the pipeline. So, Let us discuss the options one by one.
Example:
pipeline { agent any options { timeout(time: 30, unit: 'MINUTES') } stages { stage('Example') { steps { echo 'Hello World' } } } }
When
“When” is the conditional derivate of the Jenkins declarative pipeline. Just like “IF” conditions, “when” will have the one or more condition statement. This can be either for from the user input or from the Built-in conditions.
“When” condition can be placed before “Agent” in stage or before input directive. If “beforeAgent” is set true, “when” condition will be executed before the agent declaration. If “beforeInput” is set true, “when” condition will be executed before the Input derivative.
So, Let us see some built-in conditions.
Example:
pipeline { agent any stages { stage('Example Build') { when { anyOf { branch 'master'; branch 'staging' } } steps { echo 'Hello World' } } stage('Example Deploy') { when { branch 'production' } steps { echo 'Deploying' } } } }
Parallel
Parallel block will allow us to run the stages concurrently. Idealy, Stages that are declared inside the Parallel derivative will run concurrently at the same time. If you want to stop the build process, You need to mention “failFast” true in the parallel section.
Example:
pipeline { agent any stages { stage('NORMAL Stage') { steps { echo 'I am one' } } stage('Parallel Stage') { when { branch 'master' } failFast true parallel { stage('stage one') { agent { label "stageonebranch" } steps { echo "Me in stage one" } } stage('Stage two') { agent { label "stage two" } steps { echo "Me in stage two" } } stage('Stage three') { agent { label "Stage Three" } } } } } }
Parameters
Parameter Directive will provide the trigger parameters to the pipeline. Following are the available Parameters
Example:
pipeline { agent any parameters { string(name: 'yyyyy', defaultValue: 'XXX', description: 'Hello world') text(name: 'Demo', defaultValue: '', description: 'Demo parameter') booleanParam(name: 'Boolean', defaultValue: true, description: 'Boolean value') choice(name: 'CHOICE', choices: ['A', 'B', 'C'], description: ‘Choose one') password(name: 'PASSWORD', defaultValue: 'Key', description: 'Enter a password') file(name: "FILE", description: "file to upload") } stages { stage('Example') { steps { echo "Hello ${params.yyyyy}" echo "Biography: ${params.Demo}" echo "Toggle: ${params.Boolean }" echo "Choice: ${params.CHOICE}" echo "Password: ${params.PASSWORD}" } } } }
Post
Post is another directive which is like “When”. This will execute some commands after the stage or steps as per the condition. the following are some examples of the conditions.
Example:
pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' } } } post { always { echo 'Running after the stages' } } }
Tools
This will install and configure the tools in pipeline. This block will support following tools
Example:
pipeline { agent any tools { maven 'apache-maven-1.0.1' } stages { stage('Example') { steps { sh 'mvn compile' } } } }
Triggers
Triggers are the typical Build triggers. This can be happening in any form like cron, pollscm, Upstream.
Eample:
pipeline { agent any triggers { cron('H */4 * * 1-5') } stages { stage('Example') { steps { echo 'Hello World' } } } }
Conclusion
I hope this Jenkins Declarative Pipeline Tutorial helps you understand the basic structure of the Declarative pipeline. If not, please comment on your concern and I will try to add as soon as possible. In our next Article, We will discuss the Jenkins Scripted pipeline tutorial. Stay Tuned. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOps, Agile, DevSecOps, and App Development.
Certified Cloud Automation Architect and DevSecOps expert, skilled in optimizing IT workflows with Six Sigma and Value Stream Management. Proficient in both technical and leadership roles, I deliver robust solutions and lead teams to success.
Good
This is the BEST explanation of declarative pipeline steps I’ve seen in a while! Straight to the point, good examples, easy to read. Thanks a lot! This is very helpful 🙂
Glad you found my article very helpful. Subscribe to my website to get updates on new articles and content. Happy Learning!
This is something i was looking for. Can you prepare some exercise to practice declarative pipeline.
Sure, Vishal. Actually I am drafting some exercises for many other tutorials and concepts. Soon I will post an Exercise for Jenkins Declarative Pipeline. Please Subscribe and Stay tuned. Thank You.
Thanks Prabhu its really helpful.. please if you can help with any exercise will be great
Pingback: URL
USELESS
skipDefaultCheckout – This will skip the default SCM checkout process in the agent (For Example: options { skipDefaultCheckout() })
How the hell do I actually USE THIS? There is no example as promised! No one gives REAL examples with a full pipeline to see what it SHOULD look like with SCM defined and checkout skipped.
Pingback: Https agent pipeline agent login Portal Guide Instructions Help - centtip.com
Very nice article. Easy to understand. I’m a beginner and can you please share some exercise to practice declarative pipeline.
nice explain prabhu, please share if you having demo project with source to implement this stuff, Thanks again.
Pingback: Jenkins, Pipeline to create custom input step with timeuot – Jaehoo Weblog
Pingback: Jenkins Hashicorp Vault Pipeline Plugin? Quick Answer