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

48,630

Solution 1

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

Solution 2

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.

Solution 3

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

  1. enabled parameterized.
  2. Select Choice Parameter Name: Server Choices : Choose.. qa staging master Description : Select the server from the list
  3. 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 [email protected] 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

Share:
48,630

Related videos on Youtube

AKS
Author by

AKS

A Quote on "Quote(s)": For every quote, there exists at least one contradictory quote. - AKS

Updated on September 01, 2020

Comments

  • AKS
    AKS over 3 years

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

    • AKS
      AKS almost 9 years
      @VitaliiElenhaupt actually yes that's possible but the idea is if it's possible to auto-disable/hide a parameter depending upon another one. I gave the above project type example, to just put my case. Thanks.
    • Vitalii Elenhaupt
      Vitalii Elenhaupt almost 9 years
      uno-choice-plugin may help somehow: github.com/biouno/uno-choice-plugin. I didn't try it by myself
    • AKS
      AKS almost 9 years
      @VitaliiElenhaupt Thanks. As I mentioned in my post, Param3 and Param6 are already using these param types. Even Param2 can be of any param type (what you are suggesting). These parameter types (Dynamic Cascase, Choice etc) all they are doing is that you can do things (have values on the fly depending upon a given script/file/code/conditions) but what I'm trying to find out is how to hide/disable one of these parameters when another parameter is set to a given value.
    • Vivere
      Vivere about 3 years
      Any update on this? Is Jenkins capable of doing something more than the Active Choices Plugin? Besides that, @AKS did you consider using inputs? I see that the Input parameter is kinda dope because it allows for conditional printing of different parameters. But it stops the jobs waiting for the input, it doesn't do it on the page before pressing build which is pretty annoying
    • AKS
      AKS about 3 years
      @Vivere I think that type is used, before user is looking at all parameters and provide a final go ahead or in process manual verification. medium.com/@dummybot/… and plugins.jenkins.io/uno-choice/#documentation ... but feel free to use any core parameter type like simple choice and you can run any groovy calling: groovy code, or shell code or cli (aql, jql, etc ..) and after getting/massaging the result data, just 'return aResultList' and it'll work in the second link, i don't see this plugin is deprecated or anything
    • Vivere
      Vivere about 3 years
      @AKS Ahhh so that's the use of the input parameter, I see. I use the Uno Choice plugin in a complex scenario myself. I have a Multipipeline Build Job, and a Deploy job. From the deploy job, I am able to select the specific branch, and when I do that I see all the available artefacts for that particular branch. Even so, I would like to have some hidden parameters in the future that become visible when I click a checkbox or something. Also, I would love an Advanced -like button that shows parameters for a more complex scenario. I wasn't able to do any of these yet and it is kinda frustrating.
    • AKS
      AKS about 3 years
      @Vivere welome to Jenkinsfile, FreeStyle supported HTML customization. What you can possibly do is this, instead of hiding/reviving parameters, play with the values i.e. Parameter1 changes the behavior of Parameter2 with/without any values. Without any values kind of makes Parameter2 (hidden in the real sense) may be not on the screen but user won't be able to see anything if you return 'N/A'
    • AKS
      AKS about 3 years
  • Slav
    Slav almost 9 years
    This won't grey out another parameter, but it is the closest that Jenkins can do.
  • AKS
    AKS almost 9 years
    I think it's a good plugin but not related to what I'm asking. I mentioned in my post that I'm already using these parameters (I have close to 20 different types of parameters including the one you mentioned here and I have tried all of them). What I'm trying to do is to hide the parameter/not show it / grey it out ... when another parameter is set to a given value. The one you mentioned or other parameter types, they just populate another parameter depending upon a parent parameter or using some hardcoded data/script/conditions etc.
  • AKS
    AKS almost 9 years
    In short, if you see the example in Dynamic Jenkins parameter plugin, what I'm trying to get to is: Don't show PARAMTWO at all while doing a build. If someone selects PARAMONE as "Two". In the example, all it's doing is PARAMTWO will have "Red" and "Green" values (as you selected "Two" in PARAMONE. Can we write something using this parameter plugin that don't show PARAMTWO at all if a user selects "Two" in PARAMONE?
  • Arindam Roychowdhury
    Arindam Roychowdhury over 8 years
    @ArunSangal...Did you finally find some solution? I am also trying to enable a drop down list only if a boolean parameter is selected (True)...
  • AKS
    AKS almost 8 years
    Play with Active Choice Plugin in Jenkins and see if that helps.
  • AKS
    AKS about 7 years
    Yep, did the same thing using Active Choice Plugin.
  • AKS
    AKS over 4 years
    One important point with Active Choice Plugin (Uno Plugin) is.. if you ever want to define default values (in case of multiple values of a dependent parameter which we define by putting :selected after the value ex: return ['valueA','valueB:selected','valueC'] -- then take this point: Put 'valueB:selected' as the FIRST entry in the array (that you are returning), this will HELP if you ever use TAB (keyboard) to switch between Jenkins parameters! ... if you don't do it, then TAB will make valueA (by default after TAB is pressed and control goes to next parameter, which is annoying).
  • arielma
    arielma about 4 years
    can it helps me with my scenario: stackoverflow.com/questions/60248264/…
  • arielma
    arielma about 4 years
    can it helps me with my scenario: stackoverflow.com/questions/60248264/…
  • arielma
    arielma about 4 years
    can it helps me with my scenario: stackoverflow.com/questions/60248264/…