Search code examples
jenkinsjenkins-pipeline

How to add a timeout step to Jenkins Pipeline


When you are using a free style project you can set that after 20 minutes the build is aborted if not concluded. How is this possible with a Jenkins Multi Branch Pipeline Project?


Solution

  • You can use the timeout step:

    timeout(20) {
      node {
        sh 'foo'
      }
    }
    

    If you need a different TimeUnit than MINUTES, you can supply the unit argument:

    timeout(time: 20, unit: 'SECONDS') {
    

    EDIT Aug 2018: Nowadays with the more common declarative pipelines (easily recognized by the top-level pipeline construct), timeouts can also be specified using options on different levels (per overall pipeline or per stage):

    pipeline {
      options {
          timeout(time: 1, unit: 'HOURS') 
      }
      stages { .. }
      // ..
    }
    

    Still, if you want to apply a timeout to a single step in a declarative pipeline, it can be used as described above.