jenkins extended parameter plugin groovy script

匿名 (未验证) 提交于 2019-12-03 02:50:02

问题:

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.

  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? is it the environment variable WORKSPACE?
  4. there is an extra field called variable bindings. How is this used?

回答1:

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?

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. What am i supposed to return from the script?

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. What directory is the cwd of the script? is it the environment variable 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. there is an extra field called variable bindings. How is this used?

This is a property file formatted key=value file. these names are then resolvable in the groovy script.

    e.g.     key1=foo     prop2=bar 


回答2:

For parse json object (from parametres) to groovy object - Parsing and producing JSON

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!