Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized Jenkins job with dependent parameter

Tags:

jenkins

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?

like image 461
Anastazja Bondarenko Avatar asked Jan 28 '23 14:01

Anastazja Bondarenko


1 Answers

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.

like image 193
bto Avatar answered Jun 03 '23 19:06

bto