34

I'm trying to access a variable from an input step using the declarative pipelines syntax but it seems not to be available via env or params. This is my stage definition:

stage('User Input') {
    steps {
        input message: 'User input required', ok: 'Release!',
            parameters: [choice(name: 'RELEASE_SCOPE', choices: 'patch\nminor\nmajor', description: 'What is the release scope?')]
        echo "env: ${env.RELEASE_SCOPE}"
        echo "params: ${params.RELEASE_SCOPE}"
    }
}

Both echo steps print null. I also tried to access the variable directly but I got the following error:

groovy.lang.MissingPropertyException: No such property: RELEASE_SCOPE for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)

What is the correct way to access this choice parameter?

piet.t
  • 11,718
  • 21
  • 43
  • 52
LeoLuz
  • 388
  • 1
  • 3
  • 7

3 Answers3

68

Since you are using declarative pipelines we will need to do some tricks. Normally you save the return value from the input stage, like this

def returnValue = input message: 'Need some input', parameters: [string(defaultValue: '', description: '', name: 'Give me a value')]

However this is not allowed directly in declarative pipeline steps. Instead, what you need to do is wrap the input step in a script step and then propagate the value into approprierte place (env seems to work good, beware that the variable is exposed to the rest of the pipeline though).

pipeline {
    agent any
    stages {
        stage("foo") {
            steps {
                script {
                    env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
                            parameters: [choice(name: 'RELEASE_SCOPE', choices: 'patch\nminor\nmajor', description: 'What is the release scope?')]
                }
                echo "${env.RELEASE_SCOPE}"
            }
        }
    }
}

Note that if you have multiple parameters in the input step, then input will return a map and you need to use map references to get the entry that you want. From the snippet generator in Jenkins:

If just one parameter is listed, its value will become the value of the input step. If multiple parameters are listed, the return value will be a map keyed by the parameter names. If parameters are not requested, the step returns nothing if approved.

Jon S
  • 15,846
  • 4
  • 44
  • 45
  • 1
    I tried adding the id as you said but I got the same error when trying to read from the map variable: `groovy.lang.MissingPropertyException: No such property: userInput for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63)` – LeoLuz Feb 28 '17 at 15:10
  • 1
    My bad, didn't realiced that you we're using declarative pipelines. Updated my answer now. – Jon S Feb 28 '17 at 15:35
  • How to make it take default value (say firs value) when triggered through SCM changes? – aholbreich May 12 '17 at 17:43
  • I have a similar requirement but instead accepting user input as radio button selection (patch/major/minor) in this case, I want to give users the flexibility of updating the input as free text (for ex: a unix path to a file). Kindly let me know how can it be achieved. I'm not sure if i should ask it as a new question as my requirement is almost same. – Yash Sep 15 '17 at 05:08
  • @Yash see https://support.cloudbees.com/hc/en-us/articles/204986450-Pipeline-How-to-manage-user-inputs – Jon S Sep 15 '17 at 05:36
  • Thanks @Jon, this link has teh exact implementation of what I was looking for. I am just curious to know how can i store both the user inputs into an environment variable which can be used elsewhere. In the example described in the cloudbees link they just print the 2 user inputs. – Yash Sep 15 '17 at 06:26
  • @Yash I don't full follow you, maybe post as a new question? – Jon S Sep 15 '17 at 09:33
  • 3
    I realise this is really old now, but of all the searching for "how to make input work in Jenkins declarative pipeline" this is the only page that has an answer that works .. even the docs (https://jenkins.io/doc/book/pipeline/syntax/) are wrong. The trouble is that I want to access multiple params, and I can't work out how to do so. If I'm returning CHOICE1 and CHOICE2 into env.RELEASE_SCOPE, how do I actually get at the value of RELEASE_SCOPE.CHOICE1 and RELEASE_SCOPE.CHOICE2, without having to write code that parses the json string {"CHOICE1": "x", "CHOICE2": "y"} from RELEASE_SCOPE? – DavidParker Apr 18 '18 at 16:50
  • @DavidParker env.RELEASE_SCOPE is a map. Hence you can do env.RELEASE_SCOPE[CHOICE1] – ssc327 Nov 22 '18 at 03:05
  • @DavidParker I had same problem. I realized the returned map from input ist a map of Strings. For instance echo env.params[0] returns '{' and not an expected CHOICE1 Value. My solution was to create a Map manually and use the paramsmap later on: paramsmap = [:] tempstringparams = (env.params - "{" - " " - "}").split(',') tempstringparams.each { keyvalue -> keyvalueitems = keyvalue.split("=") paramsmap.put(keyvalueitems[0], keyvalueitems[1]) } – harryssuperman Sep 03 '21 at 13:53
3

Using the Input Step in the following form works for multiparameters:

script {
       def params = input message: 'Message',
                          parameters: [choice(name: 'param1', choices: ['1', '2', '3', '4', '5'],description: 'description'),
                                       booleanParam(name: 'param2', defaultValue: true, description: 'description')]
echo params['param1']
echo params['param2']
}
harryssuperman
  • 465
  • 3
  • 7
0

@DavidParker, I have figured out what you are asking. Just try to use it in your code. I have input params and using them 1 by 1. I am printing only "variable" key in job which is working. You can also try this way.

        steps{
                    script {
                        deploymentParams.findAll{ key, value -> key.startsWith('backendIP') }.each  { entry ->
                            stage("Node_$entry.key "){ 
                echo $entry.value
            }
            }
        }
        } 
Mayank C Koli
  • 21
  • 1
  • 3