jenkins扩展了参数插件的groovy脚本 [英] jenkins extended parameter plugin groovy script
问题描述
插件网站表示您可以创建一个groovy脚本运行来确定参数列表。
The website for the plugin says that you can create a groovy script to run to determine the parameter list.
虽然这个解决方法如何?说明没有说什么。
how is this resolved though? The instructions don't say anything.
- 在脚本运行的上下文中
- 我应该从脚本返回什么?
- 脚本的cwd是什么目录?它是环境变量WORKSPACE吗?
- 有一个额外的字段叫做
变量绑定
。这是如何使用的?
- In what context is the script run?
- What am i supposed to return from the script?
- What directory is the cwd of the script? is it the environment variable WORKSPACE?
- there is an extra field called
variable bindings
. How is this used?
推荐答案
我不得不深入源代码以找到答案对这些问题,所以我希望这可以帮助其他人。
I had to dig into the source code to find the answer to these questions so i hope this helps everyone else.
1。脚本在什么情况下运行?
脚本在groovy.lang.GroovyShell中运行。该类目前来自Groovy 1.8.5库。这里是代码摘录:
The script is run inside a groovy.lang.GroovyShell. This class is currently from the Groovy 1.8.5 library. here is an excerpt from the code:
// line 419 - 443 of the ExtendedChoiceParamaterDefinition
else if(!StringUtils.isBlank(groovyScript)) {
try {
GroovyShell groovyShell = new GroovyShell();
setBindings(groovyShell, bindings);
Object groovyValue = groovyShell.evaluate(groovyScript);
String processedGroovyValue = processGroovyValue(isDefault, groovyValue);
return processedGroovyValue;
}
catch(Exception e) {
}
}
else if(!StringUtils.isBlank(groovyScriptFile)) {
try {
GroovyShell groovyShell = new GroovyShell();
setBindings(groovyShell, bindings);
groovyScript = Util.loadFile(new File(groovyScriptFile));
Object groovyValue = groovyShell.evaluate(groovyScript);
String processedGroovyValue = processGroovyValue(isDefault, groovyValue);
return processedGroovyValue;
}
catch(Exception e) {
}
}
2。我应该从脚本中返回什么?
如上面的代码所示,脚本应返回一个字符串,其中包含您在参数中指定的任何分隔符或一个String []数组。这里是处理从脚本返回的值的函数片段:
As the above code demonstrates, the script should return a string with whatever delimiter you have specified in the paramater or a String[] array. here is a snippet of the function that processes the value returned from the script:
// line 450 - 465 of ExtendedChoiceParameterDefinition
private String processGroovyValue(boolean isDefault, Object groovyValue) {
String value = null;
if(groovyValue instanceof String[]) {
String[] groovyValues = (String[])groovyValue;
if(!isDefault) {
value = StringUtils.join((String[])groovyValue, multiSelectDelimiter);
}
else if(groovyValues.length > 0) {
value = groovyValues[0];
}
}
else if(groovyValue instanceof String) {
value = (String)groovyValue;
}
return value;
}
3。脚本的cwd是什么目录?它是环境变量WORKSPACE吗?
重要吗?您可以在脚本中使用
Does it matter? You can access the environment variable WORKSPACE from within the script using
Map<String, String> props = System.getenv();
def currentDir = props.get('WORKSPACE');
4。有一个额外的字段称为变量绑定。这是如何使用的?
这是属性文件格式化的key = value文件。这些名称然后可以在groovy脚本中解析。
This is a property file formatted key=value file. these names are then resolvable in the groovy script.
e.g.
key1=foo
prop2=bar
这篇关于jenkins扩展了参数插件的groovy脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!