Issue
How do I add an input step, with timeout, that continues if timeout is reached, using a default value, in a Pipeline job?
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' } }
Note: This code will require you to approve the getCauses()
method inside of script security under Manage Jenkins> In-process Script Approval
:
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}]" } }
If you do not want to approve that method(or dont have admin permissions) then use this instead:
catch(err) { // timeout reached or input false echo "This Job has been Aborted" }