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"
}
5 Comments