jenkinsjenkins-pipelinegroovyshellextended-choice-parameter

Jenkins: How to use 'bindings' field of Extended Choice Parameters plugin?


I don't understand how to use the 'bindings' field of extendedChoice parameter, I checked the source code of the plugin and see that it adds variables in a context in the groovyShell, but I don't understand how to access this context.

I try to set Binding like this:

  def bindings = new Binding()
  bindings.setProperty("foo", "foo value")

  return extendedChoice(
      name: 'jsonParameters',
      bindings: bindings.getVariables().toString(),
      type: 'PT_JSON',
      javascript: jsScript,
      groovyScript: groovyScript)

Then inside the "groovyScript", I expect to be able to access my "foo" variable...

UPDATE: I created a simple test, the 'binding' is global and I have access to it ! Why not in my groovyscript with the plugin ?

def bindings = new Binding()
bindings.setVariable("foo", "bar")
GroovyShell groovyShell = new GroovyShell();
Script compiledScript = groovyShell.parse("""
  println "foo: " + binding.variables.get("foo")
""");
compiledScript.setBinding(bindings);
compiledScript.run();

// print "foo: bar"

Plugin version: 0.78


Solution

  • Bindings field format is not as Map.toString() format but 'key=value' where entries are separated by '\n':

    return extendedChoice(
          name: 'jsonParameters',
          bindings: "key1=value2\nkey2=value2",
          type: 'PT_JSON',
          javascript: jsScript,
          groovyScript: groovyScript)
    

    Then, in order to use those variables, just call getProperty in the groovyScript:

    getProperty("key1")