8

I want to add a choice parameter to a Jenkins job. The list is fixed, but I want the dropbox to display custom value, and not the actual ones (analogous to name of a web page and not its URL).

In the certain case this is the path of the pom.xml file, however, I want to display the module name instead of the full path. An example:

Actual value                         | What I want to be displayed
-------------------------------------|----------------------------
full/path/to/my/modules/pom.xml      | All modules
full/path/to/my/modules/util/pom.xml | Utilities
full/path/to/my/modules/data/pom.xml | Data handling

Thanks for the help in advance!

Csaba Faragó
  • 443
  • 1
  • 4
  • 14

3 Answers3

5

With the Active Choices plugin, you can set this up with a groovy map:

Set a "Active Choices Parameter" with the following groovy script:

return['full/path/to/my/modules/pom.xml' : 'All modules',
'full/path/to/my/modules/util/pom.xml' : 'Utilities',
'full/path/to/my/modules/data/pom.xml' : 'Data handling']

enter image description here

The "value" of the map will be displayed in the build parameters choices: enter image description here

And "key" of the map will be set in the variable:

> echo "$URL"
full/path/to/my/modules/pom.xml
A21z
  • 1,007
  • 11
  • 18
  • How can we do it for only one parameter only? – Sumith08 Dec 16 '20 at 13:37
  • It works with a single element in the list: return['full/path/to/my/modules/pom.xml' : 'All modules'] – A21z Dec 17 '20 at 14:14
  • I get groovy error as I dont want the other parameters display names to be changed. I want something like this return['full/path/to/my/modules/pom.xml' : 'All modules', 'full/path/to/my/modules/util/pom.xml' , 'full/path/to/my/modules/data/pom.xml'] – Sumith08 Dec 17 '20 at 14:34
  • return['full/path/to/my/modules/pom.xml' : 'All modules', 'full/path/to/my/modules/util/pom.xml' : 'full/path/to/my/modules/util/pom.xml', 'full/path/to/my/modules/data/pom.xml' : 'full/path/to/my/modules/data/pom.xml'] should work. – A21z Dec 18 '20 at 20:29
  • I know this way but it will be a lengthy one. – Sumith08 Dec 21 '20 at 08:46
4

You can do this with the Extended Choice Parameter plugin.

To set it the way you want, under "This build is parameterized", choose Extended Choice Parameter, and set it up like this:

enter image description here

Note it may look a little different depending on what version of Jenkins you have but it shouldn't be too different (this screenshot was on 2.0-beta-2).

user1072692
  • 687
  • 1
  • 8
  • 19
  • Thanks for your suggestion! We have an outdated version of this plugin, and seems it does not have this feature yet. Indeed, the Jenkins itself is a bit outdated, and a warning says the latest version of the extended choice plugin may not work with our Jenkins. – Csaba Faragó Sep 16 '16 at 06:31
0

I found it painful that there were no easy solution to this problem for so many years, like writing a display|value line with the choice plugin, so I finally wrote my own plugin: https://github.com/Avalancs/ChoiceWithDisplay You can get the .hpi file from the release tab, and if you don't know how to install .hpi files check this SO post: https://stackoverflow.com/a/19588907/7216760

AvaLanCS
  • 89
  • 3