Pipeline: How to manage user inputs

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

  • CloudBees Jenkins Enterprise
  • Pipeline plugin

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

Tip: It is best to avoid inserting input steps inside a node{} block, so as not occupy a node/agent while waiting for user input.

Have more questions? Submit a request

10 Comments

  • 1
    Avatar
    Sumit Nagal

    It would be nice if we have link on input box, to input link otherwise user won't get it, where to input values
    link -> http://<jenkins URL>/<JobNumber>/input/

  • 0
    Avatar
    Goutham Nithyananda

    I have few questions here.

    1.How do i make the user input optional

    2.If i have a cron to run a build periodically, is it possible to run the build by automatically taking default value defined for user input defined or assuming that value as null without user interference??

  • 1
    Avatar
    Carlos Rodriguez Lopez

    try{} catch{} statements could help you to achieve what you requested. Please review the following articles:

    And adapt the above scripts to your particular case

     

     

     

     

  • 0
    Avatar
    Goutham Nithyananda

    thanks, I tried the second example and looks like when ever there is a timeout the getUser() is returning null , which ideally should return 'SYSTEM' as per example. please help here

     

     def user = err.getCauses()[0].getUser()
       if('SYSTEM' == user.toString()) { // SYSTEM means timeout.
            didTimeout = true
        }
    Edited by Goutham Nithyananda
  • 2
    Avatar
    Bert Roos

    It would be very helpful if you could extend this example with a variant in the declarative syntax. I'm looking into a combination of this article (getting user input) and conditions (what's described here).

    Thanks in advance!

  • 0
    Avatar
    Arati Kulkarni

    How do i use it to accept gitparameter

  • 0
    Avatar
    Nabil Tahri

    Is there a way to response to the input function from another pipeline not from a REST API???

  • 0
    Avatar
    Irma Jakic

    Hi, does anyone know how can I stop waiting for user input? I have two parallel stages, and if something happens in one I want to stop waiting for user input (which is in second parallel stage)

  • 0
    Avatar
    Rainer Haseneder

    Where do I have to enter all these "code lines", please ?

  • 0
    Avatar
    Denys Digtiar

    Hi Rainer,

    This is for Jenkins Pipeline (https://jenkins.io/doc/book/pipeline/). Therefore, you would use it as part of your `Jenkinsfile`

Please sign in to leave a comment.