user1340582
user1340582

Reputation: 19699

Jenkins Parameterized build to use key/value pairs

I have a Jenkins parameterized build. I tick the "this build is parameterized" and I set a "Choice" environment name to be "ENVIRONMENT" and then as choices I define human readable names such as "Test env1", "Test env2", etc. However I want these keys to actually contain different values, for example "Test env1" key would container a file path as its value. How can this be done?

Upvotes: 4

Views: 5805

Answers (2)

ptha
ptha

Reputation: 916

I have managed to get the keys/values with a dropdown select parameter working with the Active Choices Plugin, the answer was actually buried in the comments on the plugin page itself.

You simply need to use a map rather than a list when writing your groovy script to define the dropdown options. The map key is what the parameter will be set to if the user selects this option. The map value is what will be actually displayed to the user (i.e something human readable) in the dropdown list.

Here are the steps.

  1. Ensure you have the Active Choices Plugin installed.
  2. Open the configuration of your Jenkins job, select This project is parameterised.
  3. Click Add Parameter and select Active Choices Parameter.
  4. Name your parameter ENVIRONMENT and click the Groovy Script check box.
  5. In Groovy Script enter content: return ['env1 file path value':'Test env1', 'env2 file path value':'Test env2'] For this example the user will see a dropdown with 2 options: Test env1 and Test env2. The keys: env1 file path value and env2 file path value are what the Jenkins build parameter will be set to if the option is selected. Modify these as necessary.

Upvotes: 2

adnan kamili
adnan kamili

Reputation: 9455

In this case ENVIRONMENT is the key and "Test env1", "Test env2", etc. are the possible values. Choice parameter is to restrict the possible inputs.

Based on the value of %ENVIRONMENT% you can execute multiple pathways in your batch scripts or whichever scripts you are executing

Upvotes: 0

Related Questions