In a Jenkins pipeline, i want to provide an option to the user to give an interactive input at run time. I want to understand how can we read the user input in the groovy script
Solution: In order to set ,get and access user input as a variable on jenkins pipeline, you should use ChoiceParameterDefinition , attaching a quick working snippet:
script {
// Define Variable
def USER_INPUT = input(
message: 'User input required - Some Yes or No question?',
parameters: [
[$class: 'ChoiceParameterDefinition',
choices: ['no','yes'].join('\n'),
name: 'input',
description: 'Menu - select box option']
])
echo "The answer is: ${USER_INPUT}"
if( "${USER_INPUT}" == "yes"){
//do something
} else {
//do something else
}
}