Jenkins:如何使用扩展选择参数插件的“绑定”字段?

问题描述

我不明白如何使用extendedChoice参数的'bindings'字段,我检查了插件的源代码,看到它在groovyshell的上下文中添加了变量,但我不明白如何访问这个上下文。

我尝试像这样设置绑定:

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

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

然后在“groovyScript”中,我希望能够访问我的“foo”变量...

更新:我创建了一个简单的测试,“绑定”是全局的,我可以访问它!为什么不在我的 groovyscript 中使用插件

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"

插件版本:0.78

解决方法

绑定字段格式不是 Map.toString() 格式而是 'key=value',其中条目由 '\n' 分隔:

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

然后,为了使用这些变量,只需在 groovyScript 中调用 getProperty

getProperty("key1")

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...