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?
Using Parameter Value in the Pipeline: Parameter values can be accessed using params helper. In addition to this preferred method, they can be accessed likewise through the environment variables described previously in the previous article. To access the parameter value use $params.NAME or ${params.NAME} syntax.
Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on. Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.
Unlike default parameter types, the Active choice parameter type gives you more control over the parameters using a groovy script. You can have dynamic parameters based on user parameter selection. To use the active choice parameter, you need to have an Active Choices plugin installed in Jenkins.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With