0
0
Jenkinsdevops~5 mins

Build timeouts in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes builds get stuck or take too long, wasting resources and blocking other work. Build timeouts help stop these builds automatically after a set time, keeping your pipeline efficient and reliable.
When a build step might hang due to network issues or infinite loops
When you want to limit how long a test suite can run to avoid delays
When running builds on shared servers to free up resources quickly
When you want to fail slow builds early to speed up feedback
When automating deployments that must complete within a time window
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  options {
    timeout(time: 10, unit: 'MINUTES')
  }
  stages {
    stage('Build') {
      steps {
        echo 'Building the project'
        // Simulate a long build
        sleep 600
      }
    }
  }
}

This Jenkinsfile defines a pipeline with a global timeout option.

options.timeout sets the maximum build time to 10 minutes.

If the build runs longer, Jenkins will abort it automatically.

The sleep 600 simulates a build that takes 10 minutes.

Commands
This command updates the Jenkins job configuration with the Jenkinsfile that includes the timeout setting.
Terminal
jenkins-jobs --conf jenkins.ini update my-pipeline
Expected OutputExpected
Job 'my-pipeline' updated successfully
Starts a build of the 'my-pipeline' job to test the timeout behavior.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
Started build #1 for job 'my-pipeline'
Fetches the console output of build #1 to verify the build was aborted after timeout.
Terminal
jenkins-cli console my-pipeline 1
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building the project [Pipeline] sleep Sleeping for 600 sec [Pipeline] timeout Timeout after 10 minutes Build was aborted Finished: ABORTED
Key Concept

If you remember nothing else from this pattern, remember: setting a build timeout stops stuck or slow builds automatically to save time and resources.

Common Mistakes
Not setting the timeout in the Jenkinsfile or job options
Without a timeout, builds can run indefinitely, blocking resources and delaying other jobs.
Always add a timeout option in your Jenkinsfile or job configuration to limit build duration.
Setting a timeout too short for the actual build needs
Builds may be aborted prematurely, causing false failures and wasted retries.
Estimate realistic build times and set the timeout slightly above the average duration.
Using timeout only on some stages but not globally when needed
Long-running stages without timeout can still block the pipeline even if others have timeouts.
Apply timeout globally or to all critical stages to ensure full coverage.
Summary
Add a timeout option in the Jenkinsfile to limit build duration.
Update the Jenkins job with the new Jenkinsfile containing the timeout.
Run a build and check the console output to confirm the build aborts after the timeout.