How to add a timeout step to Jenkins Pipeline

后端 未结 2 2037
野性不改
野性不改 2020-12-02 17:46

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 Pr

相关标签:
2条回答
  • 2020-12-02 18:40

    For a Declarative Pipeline it is adviced to use the timeout step in the options-section.

    Executes the code inside the block with a determined time out limit. If the time limit is reached, an exception (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException) is thrown, which leads in aborting the build (unless it is caught and processed somehow). Unit is optional but defaults to minutes.

    The timeout-step has 3 parameters you can configure:

    • time (required, int)

      • The amount of the timeout, if no unit is stated duration in minutes
    • activity (optional, boolean)

      • Timeout after no activity in logs for this block instead of absolute duration.
    • unit (optional, values: NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS)

      • The unit for the time, default is MINUTES

    Examples:

    timeout(time: 10) // would lead to a timeout of 10 minutes (MINUTES is default value)
    timeout(time: 10, unit: 'SECONDS') // a 10 seconds timeout
    timeout(time: 10, activity: false, unit: 'MILLISECONDS')
    

    The official Jenkins documentation has a very nice example for the use of a timeout:

    pipeline {
        agent any
        options {
            timeout(time: 1, unit: 'HOURS') 
        }
        stages {
            stage('Example') {
                steps {
                    echo 'Hello World'
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 18:41

    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.

    0 讨论(0)
提交回复
热议问题