Pipeline: How to manage user inputs

Article ID:204986450
1 minute readKnowledge base

Issue

In a pipeline it might be sometime useful to insert some human interactions to choose an option or fill some parameters.

Without the Pipeline plugin Jenkins users often used the Promoted Builds Plugin to implement such behaviour.

How do we reproduce this behaviour with the pipeline plugin ?

Environment

Resolution

You have to use the input step to achieve it. It will give you something like this :

stage 'promotion'
def userInput = input(
 id: 'userInput', message: 'Let\'s promote?', parameters: [
 [$class: 'TextParameterDefinition', defaultValue: 'uat', description: 'Environment', name: 'env'],
 [$class: 'TextParameterDefinition', defaultValue: 'uat1', description: 'Target', name: 'target']
])
echo ("Env: "+userInput['env'])
echo ("Target: "+userInput['target'])

and the result will be
Running: promotion
Entering stage promotion
Proceeding
Running: Input
Input requested
Running: Print Message
Env: uat
Running: Print Message
Target: uat1
Running: End of Workflow
Finished: SUCCESS

If you have only one parameter, its value will be directly returned instead of a Map

stage 'promotion'
def userInput = input(
 id: 'userInput', message: 'Let\'s promote?', parameters: [
 [$class: 'TextParameterDefinition', defaultValue: 'uat', description: 'Environment', name: 'env']
])
echo ("Env: "+userInput)

will give you this result :

Running: promotion
Entering stage promotion
Proceeding
Running: Input
Input requested
Running: Print Message
Env: uat
Running: End of Workflow
Finished: SUCCESS
It is best to avoid inserting input steps inside a node{} block, so as not occupy a node/agent while waiting for user input.