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
}
Can anyone help me with this please?
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()
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)
Related
For Jenkins pipeline, I've a parameter say Repository and second parameter say Branch.
Based on Repository value, I want to auto-populate, value for Branch.
Let's say -
if Repository is BobRepo then for Branch value BobBranch is auto populated
if Repository is AdamRepo then for Branch value AdamBranch is auto populated
This can be achieved using Active Choice Reactive Reference Parameter.
But if user provides, some unknown value to Repository like UnknownRepo then he should be allowed to type in value in Branch parameter which is not possible with Active Choice Reactive Reference Parameter
Can you please help how to achieve editable parameter when conditions don't match?
Actually you can achieve that using the Active Choice Reactive Reference Parameter.
All you need is to set the Branch parameter to be of Choice Type Formatted HTML and use the following template that is provided in the official documentation:
return "<input name=\"value\" value=\"${ReactiveRefParam}\" class=\"setting-input\" type=\"text\">"
This code template will create an input parameter with the propagated value that was defined in the value attribute ,and it enables the user to edit the propagated value.
In your case it will look something like:
def branch = ''
if (Repository == 'BobRepo') {
branch = 'BobBranch'
} else if (Repository == 'AdamRepo') {
branch = 'AdamBranch'
}
return "<input name='value' value='${branch}' class='setting-input' type='text'>"
If you want the input field to be read only on certain values, then it is also possible using the readonly attribute of the HTML input with some logic.
In addition you can also add HTML styles to input as you wish.
Here is an example that prevents edits when a branch is populated and the input width is changed to a custom value:
def branch = ''
if (Repository == 'BobRepo') {
branch = 'BobBranch'
} else if (Repository == 'AdamRepo') {
branch = 'AdamBranch'
}
return "<input name='value' value='${Repository}' style='width: 200px;' class='setting-input' type='text' ${branch ? 'readonly' : ''}>"
Does Jenkins support (out of the box plugin) to have a way of defining dependent parameters. For ex: If I have three fields in a choice parameter for user to input, and if I select option A (considering it gives you a list of options) in the first field, then only other dependent fields should be shown to user to fill out(while doing the build), Similarly if user selects B , it should show rest of the other relevant options
Regards
You can do a basic if/then on a source parameter using an Active Choice Reactive Reference parameter. This does not solve the problem of not wanting parameters to display or not though.
How to do an if / then on a build parameter, setting another build parameter
============================================================================
You need the "Active Choices" plugin for starters.
1) The source parameter
Create a typical choice parameter - We'll call it "Choose", and give it the choices "val1", "val2", "val3", "val4"
2) The derived parameter
Create an "Active Choices Reactive Reference Parameter".
Name: What you want the parameter to be called / referenced as
Check "Groovy script"
The script:
if (Choose.equals("val1")) {
return "<input name=\"value\" value=\"Something because we chose val1\" class=\"setting-input\" type=\"text\">"
} else if (Choose.equals("val2")) {
return "<input name=\"value\" value=\"Something because we chose val2\" class=\"setting-input\" type=\"text\">"
} else if (Choose.equals("val3")) {
return "<input name=\"value\" value=\"Something because we chose val3\" class=\"setting-input\" type=\"text\">"
} else if (Choose.equals("val4")) {
return "<input name=\"value\" value=\"Something because we chose val4\" class=\"setting-input\" type=\"text\">"
}
Choice type: Formatted hidden HTML
-- or --
Formatted HTML - For testing so you can see the parameter being set
Referenced parameters: The name of the source parameter - "Choose"
I use the jenkins with active choices parameter
I need that groovy depends on my choice return area where I can write something
I try this
if (test_bench_UID.equals('user_spec')) { inputBox = "<input name='value' class='setting-input' type='text'>" return inputBox }
Where test_bench_UID is name active choices parameter, user_spec is parameter after choosing whom groovy should return input box
You have to add Active Reactive Reference Parameters, and select the choice type of the "Formatted HTML" field
,
don't forget to select the reference field!), and write such a script (
if(refParameter.equals("value")){
inputBox = "<input name='value' class='setting-input' type='text'>"
return inputBox
} else return "<b>else value will be show as text</b>"
You can use the variable by using ${value}.
In your case may be you can try ${user_spec} where ever you want to access it.
Hope it helps.
I'm trying to use groovy to update Jenkins job config.xml by the following code
def updateParameter(String key, String value){
println "changing defult value as $value for key $key"
def xml = new XmlSlurper().parseText(jobConfig)
xml.properties.'hudson.model.ParametersDefinitionProperty'.'parameterDefinitions'.'hudson.model.StringParameterDefinition'.each {
println 'found parameter: ' + it.name
if(it.name.text() == key){
println('default value changed')
it.defaultValue=value
}
}
jobConfig = XmlUtil.serialize(xml)
}
When running jobConfig = XmlUtil.serialize(xml), it changes the format, which is pretty, but I lost link break in pipeline plugin, so pipeline script doesn't work anymore. Is there a way to convert GPathResult to String without format changing?
Best Regards,
Eric
It is all my fault, the line breaks were removed when I read the xml. It seems XmlUtil.serialize(xml) doen't format the text of a xml tag, which is good :)
Best Regards,
Eric
I want little help which I suspect is due to my lack of understanding for Groovy syntax. So, here's the thing:
On the GSP page I want to set a field's value from the params map which is
["id":"107901", "Field_10.value":"2", "Field_10":["value":"2"],"Field_11.value":"", "Field_11":["value":""],action:'abc']
On the gsp page, I want to find the value against the key Field_{some-id}.value
So I am calling a tag like, g.testTag(id:field.id) with its implementation as
def testTag = { attrs,body->
println "params are ${params}"
def result = ""
def keyRequired = "Field_${attrs.id}.value"
println "keyRequired >>>>> ${keyRequired.toString()}"
params.each { key,value->
println "key is ${key}"
println "Value is ${value}"
if (key.equals(keyRequired.toString())) {
result = params.value
}
}
println "Final result is >>>>>> ${result}"
}
The value passed in id is 10 and with my params printed as above, I was expecting a value
of 2 which is corresponding to the key in the params to show up. But apparently I see the
result as null..
What am I doing wrong ? Can anyone help please...
Thanks
Not result = params.value, but result = value.
You have to change the line:
result = params.value
to:
result = value
At the each loop, you're basically saying that inside the params iteration, you're naming every key "key" and every value "value". So, params.value will actually look for the key value inside your params map, which is null.
Funny that you do that right with key but not with value. Probably just got distracted.
it is likely what you want to do, the groovy way (no need to loop over the keys of the map) to access "Field_10.value":"2"
result=params["Field_${attrs.id}.value"]
Alternatively, this also works because you have "Field_10":["value":"2"] in your map
result=params["Field_${attrs.id}"].value