Pipeline - How to add an input step, that continues if aborted, using value

Article ID:230922428
1 minute readKnowledge base

Issue

How do I add an input step, that continues if aborted, using a 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. An if statement checking the input result (userInput) is used to determine what to do next:

def userInput
try {
    userInput = input(
        id: 'Proceed1', message: 'Was this successful?', parameters: [
        [$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']
        ])
} catch(err) { // input false
    def user = err.getCauses()[0].getUser()
    userInput = false
    echo "Aborted by: [${user}]"
}

node {
    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) { // input false
    def user = err.getCauses()[0].getUser()
    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) { // input false
    echo "This Job has been Aborted"
}
This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.