简体   繁体   English

jenkins扩展参数插件groovy脚本

[英]jenkins extended parameter plugin groovy script

The website for the plugin says that you can create a groovy script to run to determine the parameter list. 插件网站说您可以创建一个groovy脚本来运行以确定参数列表。

how is this resolved though? 这怎么解决了? The instructions don't say anything. 说明书没有说什么。

  1. In what context is the script run? 在什么上下文中运行脚本?
  2. What am i supposed to return from the script? 我应该从脚本中返回什么?
  3. What directory is the cwd of the script? 什么目录是脚本的cwd? is it the environment variable WORKSPACE? 它是环境变量WORKSPACE吗?
  4. there is an extra field called variable bindings . 还有一个名为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. In what context is the script run? 1.脚本在什么上下文中运行?

The script is run inside a groovy.lang.GroovyShell. 该脚本在groovy.lang.GroovyShell中运行。 This class is currently from the Groovy 1.8.5 library. 该类目前来自Groovy 1.8.5库。 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. What am i supposed to return from the script? 2.我应该从脚本中返回什么?

As the above code demonstrates, the script should return a string with whatever delimiter you have specified in the paramater or a String[] array. 如上面的代码所示,脚本应返回一个字符串,其中包含您在paramater或String []数组中指定的任何分隔符。 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. What directory is the cwd of the script? 3.脚本的cwd是什么目录? is it the environment variable WORKSPACE? 它是环境变量WORKSPACE吗?

Does it matter? 有关系吗? You can access the environment variable WORKSPACE from within the script using 您可以使用脚本在脚本中访问环境变量WORKSPACE

Map<String, String> props = System.getenv();
def currentDir = props.get('WORKSPACE');

4. there is an extra field called variable bindings. 4.有一个额外的字段称为变量绑定。 How is this used? 这是怎么用的?

This is a property file formatted key=value file. 这是属性文件格式化的key = value文件。 these names are then resolvable in the groovy script. 这些名称然后可以在groovy脚本中解析。

    e.g.
    key1=foo
    prop2=bar

For parse json object (from parametres) to groovy object - Parsing and producing JSON 用于解析json对象(从参数)到groovy对象 - 解析和生成JSON

import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText('{ "myList": [4, 8, 15, 16, 23, 42] }')
println(object.myList)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM