Building project with Active Choice Reactive Reference Parameter

jb27 picture jb27 · Aug 13, 2018 · Viewed 7.2k times · Source

I'm new to jenkins and groovy and I'm trying to create my own configuration which allows me to build my project with various parameters. To achieve that I use Active Choices Reactive Reference Parameter. As a Choice Type I set "Formatted HTML". It looks exactly as I want but unfortunately, no mater what, I cannot return parameters to build.

This is my groovy script:

if(useDefaultValues.equals("YES")) {
    return "defaultName"
 } else {
    inputBox = "<input name='name' class='setting-input' type='text'>"
    return inputBox
 }

This is how my configuration looks

Build with default parameters

Build with other parameters

Can anyone help me with this please?

Answer

Szymon Stepniak picture Szymon Stepniak · Aug 13, 2018

Update your Groovy script to something like this:

def defaultName = "default name"

if (useDefaultValues.equals("YES")) {
    return "<b>${defaultName}</b><input type=\"hidden\" name=\"value\" value=\"${defaultName}\" />"
 }

return "<input name=\"value\" class=\"setting-input\" type=\"text\">"

It's important that your input field uses name value - it does not change your parameter name, and if you named it name you will be able to access it as $name (if you use Groovy for instance).

It is also important that default value is passed as a hidden input field, otherwise parameter value is not set. This hidden input also has to use name value.

However there is one weird problem with HTML formatted input parameter - it always adds , in the end of the parameter value. So for instance if I pass lorem ipsum, when I read it with the parameter $name I will get lorem ipsum,. It looks like it treats it as a multiple parameters or something. To extract clean value from the parameter you can do something like (Groovy code):

name.split(',').first()