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

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

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?

14 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`

  • 0
    Avatar
    Yves Schumann

    Hi@all,

    how can I trigger such a pipeline from another pipeline with given parameters? If I use 

    build job: 'pipelineRequestingInput', parameters: [string(name: 'FOO', value: "${pom.version}")], wait: true

    as usual for a parameterized job, it just hangs and wait for user input. So is there an option to just say "go-and-use-given-values"?

  • 0
    Avatar
    Denys Digtiar

    Hi Yves,

    The `input` step is different from the parametrized project. the `build` step can start a new build of a project whether it is parameterized or not but it cannot resume a Pipeline paused on the `input` step.

  • 0
    Avatar
    Ranjith Kumar

    Hi all,
    How i can fetch in inputs parameter from DB.
    I mean i don't want to hard code my input values.All values should from DB.
    Is three any plugin available to do this.

    How to achieve this

  • 0
    Avatar
    Hawk Base

    I have a git hub repository that got has single folder.As of now, the developer team using only one folder - dev. The folder is string parametrized. When user clicks the build it automatically goes to the git hub repo/ $Folder and build the code in it. As of now when the user submit build, it automatically builds the code from the repo based on a particular IP for the dev as well.

    Now, the dev team has four folders - dev, QA, Prod 1 and Prod 2  and each has different IP address. I created 2 choice parameters :

    First choice parameter: dev, QA, Prod 1 and Prod 2

    Second Choice parameter: IP of each environment.

    When I click build, it;s giving me options listed above. Now when I click the options and select build, I am struck with the following:

     What's the Folder name do I need to give now? In the earlier code, it automatically input the dev name to the folder and select the IP from the file. But now, I am not able to do this?

    What needs to be modified so that it selects the code on the folder i.e if I select Prod 1 and the corresponding IP , it must go to the github repo/Prod1 folder and build on the code

     

Please sign in to leave a comment.