jenkinsgroovyextended-choice-parameter

Accessing environment variables in Extended Choice Parameter


I want to write a Groovy script for Extended Choice Parameter that would use access WORKSPACE variable. When I try:

List<String> artifacts = new ArrayList<String>()
artifacts.add(env.WORKSPACE)
asdf = env.WORKSPACE
println asdf
return artifacts

I get the following error:

No such property: env for class: _1775dc8d170bd01576ff2b650850017e
groovy.lang.MissingPropertyException: No such property: env for class: _1775dc8d170bd01576ff2b650850017e
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
at _1775dc8d170bd01576ff2b650850017e.run(_1775dc8d170bd01576ff2b650850017e:2)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:585)
at com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition.executeGroovyScript(ExtendedChoiceParameterDefinition.java:727)
at com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition.executeGroovyScriptAndProcessGroovyValue(ExtendedChoiceParameterDefinition.java:709)
at com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition.computeValue(ExtendedChoiceParameterDefinition.java:676)
at com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition.computeEffectiveValue(ExtendedChoiceParameterDefinition.java:855)
at com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition.getParameterDefinitionInfo(ExtendedChoiceParameterDefinition.java:1451)
at jdk.internal.reflect.GeneratedMethodAccessor701.invoke(Unknown Source)

What am I doing wrong?

Also, will I be able to call a python script from this plugin, that would provide me list of parameters that I wish to use?


Solution

  • The env is available in the environment of a Jenkins build. The extended choice groovy script runs before your build, as you are entering the parameters. It runs in a GroovyShell environment, and all it can do is run a simple script to render the choices for the parameter. For example, if you are creating a multi select parameter, the script to generate the choices could be:

    return ["DEV environment", "TEST environment", "PROD environment"]
    

    So you can use env.WORKSPACE in your Jenkinsfile or pipeline script, but in the extended choice parameter script box, it isn't defined.

    According to this this answer, you should be able to use something like

    System.getEnv().get('WORKSPACE')
    

    But I couldn't get that to do what you want.