Dynamic or Conditional display of Jenkins job's parameters (rather than their value population) - jenkins

Let's say I have two(or more) types of projects: app(Application) and svc (Service) and I have created a Jenkins job (common job) which have bunch of parameters. This common job might call another downstream/individual project type jobs (Trigger other project builds etc and pass respective parameters) but it's out of scope of this question.
For ex:
PROJ_TYPE (a choice parameter type with values: app, svc)
Param2 (of some type)
Param3 (of Cascading type i.e. it depends upon the value of parent parameter PROJ_TYPE).
Param4 (Lets say I want to show this parameter only when PROJ_TYPE is selected as "app")
Param5 (of some type)
Param6 (Lets say I want to show this parameter only when PROJ_TYPE is selected as "svc". This parameter can be of any type i.e. choice, dynamic, extended choice, etc )
If I have the above parameters in a Jenkins job, then Jenkins job will show / prompt all of the parameters when a user will try to build (i.e. Build with Parameters).
Is it possible in Jenkins to show parameter (Param4) only if PROJ_TYPE parameter was selected as app otherwise, I don't want to show this parameter at all -or somehow if it's possible to grey it out? i.e. in this case, the job will show only PROJ_TYPE, Param2, Param3, Param4 and Param5 (and will not show Param6 or it's disabled/greyed out).
Similarly, I want to show parameter (Param6) only if PROJ_TYPE parameter was selected as svc otherwise, I don't want to show this parameter at all -or somehow if it's possible to grey it out? i.e. in this case, the job will show only PROJ_TYPE, Param2, Param3, Param5 and Param6 (and will not show Param4 or it's disabled/greyed out).

I know this is an oldie, but I had been searching for something similar until I found Active Choices Plugin. It doesn't hide the parameters, but it's possible to write a Groovy script (either directly in the Active Choices Parameter or in Scriptler) to return different values. For example:
Groovy
if (MY_PARAM.equals("Foo")) {return ['NOT APPLICABLE']}
else if (MY_PARAM.equals("Bar")) {return ['This is the only choice']}
else if (MY_PARAM.equals("Baz")) {return ['Bazoo', 'Bazar', 'Bazinga']}
/Groovy
In this example MY_PARAM is a parameter in the Jenkins job. As long as you put 'MY_PARAM' in the Active Choices 'Referenced Parameters' field the script will re-evaluate the parameter any time it is changed and display the return value (or list of values) which match.
In this way, you can return a different list of choices (including a list of one or even zero choices) depending on the previous selections, but I haven't found a way to prevent the Parameter from appearing on the parameters page. It's possible for multiple Active Choice Parameters to reference the same Parameter, so the instant someone selects "App" or "Svc" all the irrelevant parameters will switch to 'Not Applicable' or whatever suits you. I have played with some HTML text color as well, but don't have code samples at hand to share.
Dirk

According to the description you may do this with Dynamic-Jenkins-Parameter plugin:
A Jenkins parameter plugin that allows for two select elements. The second select populates values depending upon the selection made for the first select.
Example provided on the wiki does exactly what you need (at least for one conditional case). I didn't try it by myself.

#derik It worked! for me
the second list is populating based on the choice of the first element.
I used Active Choice reactive parameter plugin,
the requirement was the first param will list my servers,
based on the fist selection, the second parm is to connect to selected server and list the backup.
so the list of available backup will the shown here to restore.
enabled parameterized.
Select Choice Parameter
Name: Server
Choices : Choose..
qa
staging
master
Description : Select the server from the list
Add new parameter "Active Choice Reactive Parameter"
Name: Backup
Script: Groovy Script
def getbackupsqa = ("sshpass -f /opt/installer/pass.txt /usr/bin/ssh -p 22 -o StrictHostKeyChecking=no myuser#111.22.33.44 ls /opt/jenkins/backup").execute()
if (Server.equals("Choose..")) {return ['Choose..'] }
else if (Server.equals("qa")) {return getbackupsqa.text.readLines()}
else if (Server.equals("staging")) {return ['Staging server not yet configured']}
else if (Server.equals("master")) {return ['Master server not yet configured']}
Description : Select the backup from the list
Referenced parameters : Server
The result as here

Related

Display the active selected parameters as comma separated values with in a box in Jenkins

I have certain build parameters followed by active choices(radio button, drop-down etc)
Once after selecting each option I want to display all the selected items as comma-separated value with a box (It's just a confirmation box having all the selected values after which I must proceed to build)
[Build parameters]
https://i.stack.imgur.com/vJjCz.png
[Expected]
https://i.stack.imgur.com/gkJXd.png
You can use the Active Choices Reactive Reference Parameter to active this.
Add Reactive Reference Parameter to you job, call it Test Data, in the Referenced parameters section reference your existing parameters OperatingSystem,OSVersion,TestSuites,ProbeDeploymentType, in the Choice Type choose Formatted Html so you can customise you display box, and in the script section just create the format you want to print and design the HTML for it.
Here is an example with a textarea:
summeryList =TestSuites.split(',').collect { suite ->
"${OperatingSystem}, ${OSVersion}, ${suite}, ${ProbeDeploymentType}"
}
return "<textarea cols='50' readonly name='value' >${summeryList.join('\n')}</textarea>"
You can of course customize the html layout however you want.
Here is a screenshot of the parameter config:
and the result build page:

Conditional Parameter based on User input in Jenkins

I have a requirement to call a parameter based on user input for another parameter. In short,
I have a parameter for build_type with choice values Daily, Release and Snapshot. Only whenever user selects Snapshot option for build_type, it should prompt user to enter snapshot name.
Do we have any plugin for this ?
Thanks,
Ras.
Give this a try https://wiki.jenkins.io/pages/viewpage.action?pageId=74875956
Its straight forward you can also try this https://wiki.jenkins.io/display/JENKINS/Extended+Choice+Parameter+plugin
EDITED
It does work, you just have to do a bit of groovy scripting :)
First you need to select Active Choice Parameter
Then select Active Choice Reactive reference Parameter
Groovy script
if(States.equals("Snapshot")){
inputBox="<input name='Snapshot' type='text' >"
}
TA-DA
I'll leave the beautification/formatting to you :)

How to define a Jenkins String parameter based on the Choice parameter selected

I am trying to setup a job , which requires the user to enter a input String on the Choice selected
I have seen various plugin, but all of them have the option of Choice parameters to be picked up from but not a string one
Scenario - Product ( abc, def , ghi , jkl)
If the product name is only "def" then the user need to enter the dependent revision number as a string parameter "12356" , if any other product is selected then user shouldnt have any option to enter the string parameter
Any help is appreciated
This is not possible in Jenkins build with parameters page.
Parameters are loaded all at once, and cannot have logic that depends on the value of another one.
You have a few options, some easy ones are:
In JenkinsFile/Pipeline, once job has kicked off, evaluate value of the product param and if 'def' Prompt user for subsequent input
https://jenkins.io/doc/pipeline/steps/pipeline-input-step/
Have the string field present as a param, but in the description note that it is only required when product == 'def' and otherwise ignore it regardless of value

Jenkins : Dynamically make the parameter available in UI while building job

Assume I have a job at jenkins . and am trying to build it with parameters.
Assume I have 2 parameters say para1 and para2.
para1 is of choice type parameter with 'yes' and 'No' value
I want para2 to be available in the UI only when 'yes' is selected in para1. otherwise i dont want this parameter itself displayed in the UI
We have following plugins
https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin
https://wiki.jenkins.io/display/JENKINS/Reactive+Reference+Dynamic+Parameter+Controls
but these will allow to dynamically return values for a parameter based on any reference i.e any prev paramter value,
but not to disable a parameter itself from a job while it builds, like the user should not see the parameter name itself in UI while building the job , in my case para2 should not be seen when 'No' selected in para1 ...
any suggestion or workaround to acheive my scenario ?
Many Thanks
What you are trying to achieve is not possible. A Jenkins job is defined by XML, displaying a new parameter based on a value of another one means that the XML definition of the job should be changed and the job reloaded again.
What you can do is to have both parameters displayed in the job and then just use a simple if statement in your Jenkins pipeline to ignore para2 in case para1 value is no.
if (para1 == 'yes'){
print "Do something that takes into account ${para2}"
} else {
print "${para2} value is ignored"
}

How to send additional parameters to remote script in jQuery Autocomplete

To the best of my knowledge, when a remote script is specified as the source in the set of Autocomplete options, that script receives whatever the user has typed until that point, and then it's up to that script to send relevant data based on that. Is it possible to send any additional parameter to that script? For example, suppose the site is providing the user an option to filter questions by forum. Each forum has plenty of sub-forums. Say abc is a forum, which contains sub-forums bcd ,cde and def. Now if the user has already inputted abc, and then he types b, the script should not return bcd, since abc being present ensures that bcd is present. So the script should return other results that start with b. For that I need to send a parameter to the script (having the value abc) such that it doesn't return results that belong to forum abc, and search other forum or subforum names that start with b. How can I send the additional parameter?

Resources