Pipeline - How to add an input step, with timeout, that continues if timeout is reached, using a default value

Issue

How do I add an input step, with timeout, that continues if timeout is reached, using a default value, in a Pipeline job?

Environment

  • Jenkins
  • CloudBees Jenkins Enterprise
  • Pipeline plugin

Resolution

You can use a try catch block to achieve this.

The following asks for input, with a timeout of 15 seconds. If the timeout is reached the default is true. An if statement checking the input result (userInput) is used to determine what to do next:

def userInput = true
def didTimeout = false
try {
    timeout(time: 15, unit: 'SECONDS') { // change to a convenient timeout for you
        userInput = input(
        id: 'Proceed1', message: 'Was this successful?', parameters: [
        [$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']
        ])
    }
} catch(err) { // timeout reached or input false
    def user = err.getCauses()[0].getUser()
    if('SYSTEM' == user.toString()) { // SYSTEM means timeout.
        didTimeout = true
    } else {
        userInput = false
        echo "Aborted by: [${user}]"
    }
}

node {
    if (didTimeout) {
        // do something on timeout
        echo "no input was received before timeout"
    } else if (userInput == true) {
        // do something
        echo "this was successful"
    } else {
        // do something else
        echo "this was not successful"
        currentBuild.result = 'FAILURE'
    } 
}

References

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.