Skip to content

Running multiple steps

Michael Neale edited this page Nov 3, 2016 · 6 revisions

Pipelines are made up of multiple steps that allow you to build, test and deploy applications. Jenkins Pipeline allows you to compose multiple steps in an easy way that can help you model any sort of automation process.

Multiple Steps

Think of a step as like a single command that does a single action. When a step succeeds it moves onto the next step. When a step fails to execute correctly the Pipeline will fail.

When all the steps in the Pipeline have successfully completed the Pipeline is considered successfully executed.

Linux, BSD and macOS

The sh step is used to execute a shell command.

pipeline {
    agent label:'master'
    stages {
       stage('build') {
          steps {
             sh 'echo step1'
             sh 'echo step2'
             sh '''
                echo 'Multiline'
                echo 'Example'
             '''
             echo 'not using shell'
          }
       }
    }
}

Windows

The bat step is used to execute a batch command.

pipeline {
    agent label:'master'
    stages {
       stage('build') {
          steps {
             bat 'echo step1'
             bat 'echo step2'
             bat '''
                echo 'Multiline'
                echo 'Example'
             '''
             echo 'not using shell'
          }
       }
    }
}

Timeouts, retries and more

There are some powerful steps that "wrap" other steps that can easily solve problems like retrying (retry) steps until successful or bailing out if a step takes too long (timeout).

pipeline {
     agent label:'master'
     stages {
         stage('deploy') {
             steps {
                 retry(3) {
                    sh 'echo deploying...'
                 }
                 timeout(time: 3, unit: 'MINUTES') {
                    sh 'echo checking health...' 
                 }            
             }
         }
     }
}

The deploy stage retries a script 3 times, and then waits for up to 3 minutes for the "health check" script to run. If it did not complete in 3 minutes, the pipeline will have failed at the "deploy" stage.

Steps like timeout and retry contain other steps and you can have any steps run within them as you like - even timeout and retry.

We can compose these steps together. For example, if we wanted to retry our deployment 5 times, but never want to spend more than 3 minutes before failing the pipeline stage:

   timeout(time: 3, unit: 'MINUTES') {
       retry(5) {    
           sh 'echo deploying'
           sh 'echo verifying...'
       } 
   } 

Cleaning up after yourself

When the build has run, you may need to run steps to clean up, or take some action based on the outcome. This is where the postBuild section comes in handy:

pipeline {
    agent label:'master'
    stages {
       stage('test') {
          steps {
             sh 'fail me please'
          }
       }
    }

    postBuild {
        always {
          sh 'This will always run'
        }
        success {
          sh 'This will run only if successful'
        }
        failure {
          sh 'This will run only if failed'
        }
        unstable {
          sh 'This will run only if the run was marked as unstable'
        }
        changed {
          sh 'This will run only if the state of the Pipeline has changed')
          sh 'For example, the Pipeline was previously failing but is now successful'
        }
    }
}

You can also run cleanup steps per stage as well if you like:

pipeline {
    agent label:'master'
    stages {
       stage('test') {
        post {
            always {
              sh 'This will always run'
            }
            failure {
              sh 'This will run only if failed'
            }
            changed {
              sh 'This will run only if the state of the Pipeline has changed')
            }
         }

         steps {
             sh 'fail me please'
         }
       }
    }

}