Timeout on a Build Step of Jenkins

JenkinsBuild ProcessBuild AutomationJenkins Plugins

Jenkins Problem Overview


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.

Jenkins Solutions


Solution 1 - Jenkins

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'
          }
        }
    }
}

Docs: https://jenkins.io/doc/book/pipeline/syntax/#options

Solution 2 - Jenkins

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

Here's an screenshot: enter image description here

Solution 3 - Jenkins

In pipeline jobs you can wrap your step(s) with timeout as follows:

timeout(time: 5, unit: 'MINUTES') {
   // steps to execute
}

Solution 4 - Jenkins

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

Solution 5 - Jenkins

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: https://stackoverflow.com/questions/38096004/how-to-add-a-timeout-step-to-jenkins-pipeline

Solution 6 - Jenkins

There is no such functionality that I am aware of. JENKINS-8900 requests it.

Solution 7 - Jenkins

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.

Solution 8 - Jenkins

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

Solution 9 - Jenkins

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...

Solution 10 - Jenkins

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrohitsView Question on Stackoverflow
Solution 1 - JenkinsChris RView Answer on Stackoverflow
Solution 2 - JenkinsJESiiView Answer on Stackoverflow
Solution 3 - JenkinskatrashView Answer on Stackoverflow
Solution 4 - JenkinsAlex LoweView Answer on Stackoverflow
Solution 5 - JenkinsChuanView Answer on Stackoverflow
Solution 6 - JenkinsJesse GlickView Answer on Stackoverflow
Solution 7 - JenkinsAbhi_SharmaView Answer on Stackoverflow
Solution 8 - JenkinsIanView Answer on Stackoverflow
Solution 9 - JenkinsŁukasz RżanekView Answer on Stackoverflow
Solution 10 - JenkinsHemantView Answer on Stackoverflow