7

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?

Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
jb27
  • 93
  • 1
  • 1
  • 7
  • 1
    On jenkins wiki I found some useful informations and rewrite my code. Now I return this: 'return ""' But it still doesn't work... – jb27 Aug 13 '18 at 07:56

2 Answers2

10

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()
Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
  • Thanks for the information about comma ( , ) . I thought I messed up some code. I was confuse with comma ( , ) everytime I use the parameter value from Formatted HTML. – Isma Rekathakusuma Aug 19 '22 at 09:31
5
def defaultName = "default name"
if (useDefaultValues.equals("YES")) {
    return "<input type=\"text\" name=\"value\" value=\"${defaultName}\" />"
}
return "<input name=\"value\" type=\"text\">"

Check "Omit value field" fixed comma problem.(comma issue)

Seven
  • 177
  • 1
  • 5