Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get input steps ouput in jenkins-pipeline

I used in my pipeline a input steps as you can see below :

input(
         message : "some message",
         parameters: [
          [$class: 'ChoiceParameterDefinition',
            choices: string ,
            description: 'description',
            name:'input'
         ]
        ]
      )

I wanted to use the name input that I configure to get the value put in the input like this ${input}, but it didn't work. I also tried to put it in a var like this :

def reg = input : messages : "", paramaters: [...]

But It doesn't work either, so I don't understand how I can get the param that the user chose and didn't find how to do in the do.

Regards,

like image 708
Nab Avatar asked Jun 21 '18 07:06

Nab


People also ask

What is the purpose of using the input step in Jenkins?

In Jenkins declarative pipelines it is possible to prompt a user for an interactive input by creating the input step. For example, at some stage of the Jenkins pipeline you may want to ask a user to provide the credentials. The user input can be saved as an environment variable and used in the next steps.


1 Answers

When using ChoiceParameterDefinition remember to define choices as string delimited with \n. You can assign value returned by input(...) step to a variable and use it later on. Take a look at following example:

node {
    stage('Test') {
        def reg = input(
            message: 'What is the reg value?', 
            parameters: [
                [$class: 'ChoiceParameterDefinition', 
                    choices: 'Choice 1\nChoice 2\nChoice 3', 
                    name: 'input', 
                    description: 'A select box option']
            ])

        echo "Reg is ${reg}"
    }
}

In this example I define a single select with 3 options. When I run this pipeline, I get this popup to select one of three options:

enter image description here

I pick the first one and pipeline finishes with following console output:

[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] input
Input requested
Approved by admin
[Pipeline] echo
Reg is Choice 1
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
like image 126
Szymon Stepniak Avatar answered Nov 03 '22 01:11

Szymon Stepniak