add

Jenkins Pipeline Script basics

A walk through of Pipeline script skeleton

jenkins

Let us discuss the basic skeleton of the basic skeleton of a Jenkins pipeline script.

Jenkins pipeline script supports many powerful constructs like parallel execution,triggering multiple jobs in parallel or in series and efficient error handling etc by very simple workflow to complex delivery pipeline.

Stage and Step are two area sections (or think as a task blocks) we arrange in various manners to build a delivery pipeline.
 

    Stage – a named block that contains series of steps.
    Step – contains the action that carries out the purpose of named Stage block.

 
There are two types of syntax used for defining your JenkinsFile. Either we can write in Declarative style or scripted style.

Below is the sample that follows the scripted file:
 

Basic skeleton of jenkinsfile:

 

pipeline {
 agent any
 environment { 
                 //contains global environment variables like system paths etc that are accessible all over the stages. 
 } 
     options {  
           
            // Section for specifying optional needs like log roatation, checkout style, timestamp formats etc 
 }
 
 stages {   // container for all the stage activities.
 
           stage('Display Environment') { // each stage is a named Unit of work/Activity that contains steps.
 
 steps {    // Container for Steps that must be executed to complete the named stage. In this case, "Display Environment" }
 }
 
 stage('Prepare and Checkout') { 
 
 steps {
 
 script {    if ( )  {      } else { }   } // Optional in Steps section to execute groovy legacy scripted pipeline code
 
     } 
 } 
 
 stage('Build and Create bar') {   // Stages are executed in order and subsequent/prospective stage will be executed only if preceding stage is successful
 steps { } 
 
 }
 
 stage('Bar Override') {
 steps { } 
 }
 
 stage('Deploy to Broker') {
 
 when { 

// This construct is used for conditional execution of steps section. In other words, below steps section will be executed only if "when" construct returns True.

 expression { return Value ; }
 
 }

 steps {  

// This steps sections will be executed if above when condition is true. there are more keywords like "not","anyof" etc to construct more complicated expression constructs.
 
 }

 }

        }    post   { // This section is always executed last by default by sandbox.
 always { } // Place the mundane/finalizing tasks under this section for a final wrap up.
 
 success { }  // This section is executed if the current build is success. 
 failure { }  // This section is executed only if the current Build is a failure. 
 unstable { } // this section is executed if the build abruptly ends.                
 change   { } // This section is executed if the build is different than previous build.                           }
}

Written by Ramesh Metta

Previous Article