In Jenkins, is there a way to give different timeouts to each or selected build step?
Build-time plugin out gives functionality of timeout "Abort the build if it's stuck" on complete project, what I need is to give different timeouts for each step. This way I can make my process more efficient.

- 974
- 1
- 8
- 12
10 Answers
If you are using Jenkins pipeline, and the newer declarative style (has a top level pipeline {
element) then there is a timeout
option
that can be used for the overall job, or on individual stages:
pipeline {
agent any
options {
timeout(time: 1, unit: 'HOURS') // timeout on whole pipeline job
}
stages {
stage('Example') {
options {
timeout(time: 1, unit: 'HOURS') // timeout on this stage
}
steps {
echo 'Hello World'
}
}
}
}

- 2,464
- 3
- 25
- 31
As of current versions of Jenkins, this can be done. Hit 'Configure', then select the 'Build Environment' tab, and then set your timeout.

- 4,678
- 2
- 39
- 44
-
1What version of Jenkins was this feature introduced? – Asaf Apr 26 '18 at 00:19
-
1Sorry; not sure of the details at this point as I've had updates applied to my Jenkins. However, this closed issue in Jenkins (https://issues.jenkins-ci.org/browse/JENKINS-23012?jql=text%20~%20%22%27build%20timeout%27%22) shows that the Build-timeout-plugin was active in May 2014. HTH. – JESii Apr 29 '18 at 14:28
-
1You need to install its plugin: https://plugins.jenkins.io/build-timeout/ – Payam Dec 20 '21 at 18:29
In pipeline jobs you can wrap your step(s) with timeout
as follows:
timeout(time: 5, unit: 'MINUTES') {
// steps to execute
}
-
2that timeout lets a pipe fail, or will it proceed to the next step? Maybe a full example would help. – Jan Hommes Nov 15 '18 at 09:53
-
1Once the timeout passes while still executing any step, everything that follows is cancelled and the whole job ends with a FAILED status. Here is how this is used in a pipeline script: ```pipeline { agent { label 'xxx' } options { timestamps(); timeout(time: 2, unit: 'HOURS') } stages { stage('Stage-One') { steps { /* one or more steps here */ } } }``` } – katrash Nov 17 '18 at 21:44
-
How to make the pipeline still success after one step aborted by timeout? – fahmi sidik Jan 17 '20 at 06:04
-
3In case you need to continue to other steps after timeout expires, you can surround it with a try/catch and continue execution as normal. But remember that this can only be applied inside a step. Example: ``` steps { try { timeout(time: 5, unit: 'MINUTES') { // steps to execute within these 5 minutes } } catch (Exception e) { // do whatever you want here } // continue executing other steps here }``` – katrash Jan 17 '20 at 16:38
This question was originally asked before the Jenkins Pipeline existed. Although you can continue to use and configure Jenkins through the GUI, it's currently recommended to transition your projects to the pipeline. Using the Pipeline allows you to track changes to your pipeline, and store it as code so it's easy to recreate your build on any machine if you need to move your Jenkins server.
Using the pipeline, adding a timeout to a very specific part of your build is trivial. The pipeline syntax is simple and easy to use.
timeout(time:5, unit:'DAYS') {
input message:'Approve deployment?', submitter: 'it-ops'
}
Related question: How to add a timeout step to Jenkins Pipeline
Example shamelessly taken from: CloudBees Top 10 Best Practices for Jenkins Pipeline Plugin

- 131
- 2
- 5
Build-timeout Plugin isn't applicable to pipelines. refer to wiki
For pipeline time out, try something like:
timeout(time: 30, unit: 'MINUTES') {
node {
sh 'foo'
}
}
Similar answer from another thread: How to add a timeout step to Jenkins Pipeline

- 3,103
- 1
- 19
- 23
There is no such functionality that I am aware of. JENKINS-8900 requests it.

- 24,539
- 10
- 90
- 112
Please install Build Timeout plugin for your Jenkins.
Jenkins> Manage Jenkins> Manage Plugins
search for Build Timeout in available tab.. Install it. You would be finding it in Build environment as "Abort the build if it's stuck". Set the Timeout strategy and time. Absolute Deadline Elastic Likely Stuck No Activity
in my case i have used No Activity.
Hope it Helps.

- 51
- 1
- 4
I think the timeout
command from GNU coreutils might be what you want. This is under the assumption that your build steps are scripts.

- 11,280
- 3
- 36
- 58
The easiest way (and that is the way I am doing that) is to actually have different project dependent on each other and to build them in a row. It's not perfect, but the other option would be to monitor execution of different plugins with different tools/build behaviour.
Still, the approach will work, although it does suck...
BTW, there is a nice plugin which can help you out using a set of project - Build Pipeline plugin. I am using it right now to both visualize and verify the pipeline I have created. It is really handy...

- 5,786
- 27
- 37
If you don't want to use the Jenkins plugin and want to timeout a script or any command then you can use the Linux inbuilt utility "timeout".
timeout -s KILL 1m ./test
The above command will run test
executable for 1 minute and if it continues to execute after the timeout then the command will timeout and kill the process by KILL
utility.

- 123
- 10