Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkins extended parameter plugin groovy script

Tags:

jenkins

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.

  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?
like image 879
coderatchet Avatar asked Jul 14 '14 05:07

coderatchet


People also ask

How do I add extended choice parameters in Jenkins?

If your Jenkins server is 10.10. 10.10 on port 12345 the URL is 10.10. 10.10:12345/pipeline-syntax Then, On the Sample step dropdown select 'Properties: Set job properties'. There is a checkbox for 'This project is parameterized', then you can select Add parameter > Extended Choice Parameter.

How do you pass a choice parameter in Jenkinsfile?

Go to Jenkins Home, select New Item, add a name for your Job, for the project type, select Pipeline project and click on Ok. On the configure job page select the This project is parameterized checkbox in the general tab. Now, we will add an Active Choices Parameter which renders our Application Tiers as a Dropdown.

What makes Jenkins extensible?

Extension is an annotation that allows Jenkins to discover classes, instantiate them, and register them in global lists of implementations of their supertypes and interfaces.

How do you use active choice reactive parameters in Jenkins?

Active Choices Reactive Reference parameters are used to enhance a Jenkins job form UI with reference information. With this use case in mind, a Reactive Reference UI control can be rendered as: An HTML list (bulleted or numbered) An HTML input text box.


1 Answers

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 
like image 137
coderatchet Avatar answered Oct 14 '22 19:10

coderatchet