{{announcement.body}}
{{announcement.title}}

How to Create a Jenkins Declarative Pipeline

DZone 's Guide to

How to Create a Jenkins Declarative Pipeline

Jenkins introduced a Declarative pipeline which allows one to break the whole process into different stages/jobs which trigger each other in a sequence.

· DevOps Zone ·
Free Resource
Jenkins introduced a Declarative pipeline which allows one to break the whole process into different stages/jobs which trigger each other in a sequence. The key feature is Pipeline-as-a-code which allows us to define the entire pipeline flow as code in a text file.

JenkinsFile

A text file that contains the entire workflow as code that can be checked into SCM/VCS just like the rest of the code and triggers the pipeline if present otherwise the pipeline will be created.

Go to the application and create a new file (right-click on the project -> New -> File) at the same level as the POM file, give the name as JenkinsfilePaste the code given below in that.

pipeline { agent any stages { stage('Build') { steps 
{ bat 'mvn clean package' } } stage('Deploy') { steps
{ bat 'mvn deploy -DmuleDeploy' } } } }

The script starts with a pipeline tag that contains the entire process of the workflow. The agent specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed, it must be defined at the top level. Agents can be defined at stage level also. The next block, stages contain all the work that needs to be carried out. It specifies one or more stage which is executed sequentially. 

Each stage performs a specific task. The first stage "Build" simply builds the project and creates a JAR file that is deployed to the Cloudhub by the second stage.
Use sh for shell scripts and bat for Windows Batch scripts
Check-in the file along with the rest of the code.

Creating the Pipeline

Go to Jenkins -> New item. create a pipeline
Select Pipeline and click OK.
On the next screen provide Description (optional). Select GitHub hook trigger for GITScm polling under Build Triggers and configure webhook in Github as shown in here.

In the Pipeline section, select Pipeline script from SCM, select the SCM (selected Git here) and provide the repository URL and credentials. Specify the script path, by default the path is for the root level, change if Jenkinsfile is not at the root level. pipeline definition Click Save.
Trigger the build. demo-pipeline


Pipelines can be configured for human input or approval before continuing further.
Java
 




x


 
1
 stage('Deploy') { steps { input('Continue to Deploy?') bat 'mvn deploy -DmuleDeploy' } } 



SCM

The pipeline will wait for an unspecified amount of time for user input. This can be changed by introducing a timeout in the step.
Java
 




xxxxxxxxxx
1


1
 stage('Deploy') { steps { timeout(time: 100, unit: 'SECONDS') { input('Continue to Deploy?') } bat 'mvn deploy -DmuleDeploy' } }\



The pipeline will wait for 100 seconds before aborting the pipeline if no input is provided.

proceed or deploy

If the timeout expires, the pipeline status will be marked as Aborted.

checkout SCM

To mark the build as Success even if the timeout expires or the user chooses to abort, add try/catch to suppress the error.
Java
 




xxxxxxxxxx
1


1
 stage('Deploy') { steps { script { def proceed = true try { timeout(time: 100, unit: 'SECONDS') { input('Continue to Deploy?') } } catch (err) { proceed = false } if(proceed) { bat 'mvn deploy -DmuleDeploy' } } } }



The script block defines a variable proceed, then inside try block it sets the timeout for 100 seconds and asks for the input and if the timeout expires or pipeline is aborted it will go the catch block, set proceed to false and come out. If input provided is Proceed, the pipeline will continue as normal otherwise it will stop but the build status will be marked as Success.

324m

Post Conditions

Pipelines can define one or more steps to be run after the pipeline's or stage's completion using post block. This is useful to run cleanup processes, send notifications depending on the stage's or pipeline's status. The condition blocks run in the following order
Condition Description
always Run the enclosed step(s) regardless of pipeline's or stage's completion status
changed Run the enclosed step(s) only when the pipeline's or stage's completion status is different than the previous run.
fixed Run the enclosed step(s) only when the pipeline's or stage's completion status is success and the previous run failed or was unstable.
regression Run the enclosed step(s) only when the pipeline's or stage's completion status is a failure, aborted or unstable, and the previous run was successful.
aborted Run the enclosed step(s) only when the pipeline's or stage's completion status is aborted.
failure Run the enclosed step(s) only when the pipeline's or stage's completion status is failed.
success Run the enclosed step(s) only when the pipeline's or stage's completion status is a success.
unstable Run the enclosed step(s) only when the pipeline's or stage's completion status is unstable.
unsuccessful Run the enclosed step(s) only when the pipeline's or stage's completion status is not a success.
cleanup Run the enclosed step(s) after all the postconditions have been evaluated.
Add the below code after stages block.
Java
 




xxxxxxxxxx
1


 
1
 post { always { echo 'Always Condition!' } changed { echo 'Changed Condition!' } fixed { echo 'Fixed Condition!' } regression { echo 'Regression Condition!' } aborted { echo 'Aborted Condition!' } failure { echo 'Failure Condition!' } success { echo 'Success Condition!' } unstable { echo 'Unstable Condition!' } unsuccessful { echo 'Unsuccessful Condition!' } cleanup { echo 'Cleanup Condition!' } }



 Check in the code, the build will be triggered automatically. Cancel the pipeline run, go to console logs for the build.

pipeline

Since the previous build was a success and this one is Aborted, postconditions always, changed, regression, aborted, unsuccessful, and cleanup were executed.
Trigger the build again and let the pipeline finish, timeout or abort it by passing Abort when asked for input, the pipeline status will be Success as we have configured it for the same using try-catch
continue to deploy

The declarative pipeline has many other directives like parallel executions, triggers when conditions, and more. For further reading, refer here
Thanks for reading.
Topics:
declarative pipelines, devops, integration, jenkins, mule 4, pipeline as code, tutorial

Published at DZone with permission of Abhay Yadav . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}