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
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)
activity (optional, boolean)
unit (optional, values: NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS)
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'
}
}
}
}
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.