How to specify multiple timeouts globally in a Jenkins Pipeline job?

Viewed 26

I know how to specify a timeout of a Jenkins job globally:

options 
{
    timeout(time: 10, unit: 'MINUTES')
}

But I want to have 2 Timeout conditions, similar to this:

options 
{
    timeout(time: 10, unit: 'MINUTES')
    timeout(activity: true, time: 2, unit: 'MINUTES')
}

This makes sense. There is a timeout if there is no activity, but even if there is activity, the build should terminal after some time.

But specifying it twice is not valid. Is there a way to specify 2 timeouts?

1 Answers

You cannot declare two timeouts the way you have mentioned. You can either move one of your timeouts one level down to Stages or you can do something like below. Here I have wrapped the entire pipeline with a timer.

call()

def call() {
    
    timeout(time:30, unit:'SECONDS') {
        pipeline {
            agent any
            options {
                 timeout(activity: true, time:10, unit:'SECONDS')
            }
            stages {
                    stage('Build_1') {
                        steps {
                            echo "Running"
                            sleep 50
                            // script{
                            // while(true) {
                            //     echo "Output"
                            //     sleep 5
                            // }
                            // }
                        }
                    }
            }
        }
    }
}
Related