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

AKS picture AKS · May 28, 2015 · Viewed 36.4k times · Source

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).

Answer

Dirk Koenig picture Dirk Koenig · Feb 20, 2017

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