Anastazja  Bondarenko
Anastazja Bondarenko

Reputation: 199

Parameterized Jenkins job with dependent parameter

I am trying to create a Jenkins job that has dependent parameters.

Firstly I want to be able to choose a main parameter: enter image description here And then secondly to be able to choose from a set of options that are dependent parameters of the main parameter. enter image description here

If I select a different main parameter: enter image description here

I then want to have a different set of options as the dependencies to the second main parameter. enter image description here

Please, can you help me with how I can achieve this?

Upvotes: 7

Views: 11553

Answers (2)

Sebastian Westemeyer
Sebastian Westemeyer

Reputation: 298

For all of you who stumble upon the same kind of problem (like I did). There is a fairly new Jenkins plugin available that does exactly what is desired here: display a dependent set of drop-downs, updating dependent boxes when a main box changes selection.

Dependent drop-down box example

For your job you just need to follow the following steps:

Install "Multiselect Parameter Plugin"

The "multiselect parameter plugin" can be installed from Jenkins plugin management, the documentation is available on its Jenkins Plugin page.

Add new parameter

  1. Use "Multiselect Parameter" type
  2. Set name to a sensible value
  3. give a short description
  4. configure like shown below:
H,Main option,Dependent option
V,SELECTED_MAIN,SELECTED_DEPENDENT
C,A,Blue
C,A,Green
C,A,Yellow
C,B,Black
C,B,White
C,B,Grey

Use selected values

When you run "build with parameters" in your job, the following boxes are displayed for selection:

Build parameter drop-downs matching the sample project screens

In your build script you can simply use the configured environment variables SELECTED_MAIN and SELECTED_DEPENDENT, which contain the selected values from both select boxes.

Upvotes: 3

BOC
BOC

Reputation: 1699

I would suggest the Active Choices plugin (also known as "uno-choice"). (This question has references to both, though they're not the accepted answer.)

For your specific use case, you'll want to add two parameters to your job:

  1. Active Choices Parameter

    • Name: MainOption
    • Script: Groovy Script

      return ['A','B']
      
  2. Active Choices Reactive Parameter

    • Name: DependentOption
    • Script: Groovy Script

      def choices
      switch(MainOption){
          case 'A':
              choices = ['Blue','Green','Yellow']
              break
          case 'B':
              choices = ['Black','White','Grey']
              break
          default:
              choices = ['N/A']
              break
      }
      return choices
      
    • Fallback Script: Groovy Script

      return ['Option error']
      
    • Referenced parameters:

      MainOption
      

The "Referenced parameters" setting is the key - when that value is changed, the plugin will re-evaluate the Groovy script, giving you the dependent parameter effect.

Upvotes: 12

Related Questions