The Active Choices plugin is used in parametrized freestyle Jenkins jobs to create scripted, dynamic and interactive job parameters. Active Choice parameters can be dynamically updated and can be rendered as combo-boxes, check-boxes, radio-buttons or rich HTML UI widgets.
Active Choice parameters are scripted using Groovy, or (optionally) Scriptler Groovy scripts. These custom scripts support the use of the Jenkins Java API, system environment variables, global node properties, and potentially external Java and Javascript libraries.
Once the plugin is installed three new parameter types become available:
- Active Choices Parameter
- Active Choices Reactive Parameter
- Active Choices Reactive Reference Parameter
Active Choices parameters allow users to select value(s) for a job parameter. Parameter values can be:
- dynamically generated (using Groovy or a Scriptler script)
- dynamically updated based on other UI parameters
- multi-valued (can have more than one value)
- rendered with a variety of UI controls, including dynamic HTML
We will introduce the Active Choices based UI controls by briefly describing their behavior and rendering characteristics. We will then provide a guide to their configuration
- An Active Choices parameter dynamically generates a list of value options for a build parameter using a Groovy script or a script from the Scriptler catalog.
- Active Choices parameters can be rendered as standard selection lists, check boxes and radio buttons.
- A text box filter can be optionally shown to aid in filtering the value options.
Both of these parameters have additional useful behaviors and Reactive Reference has some unique rendering options.
Similarly to Active Choices Parameter above:
- Active Choices Reactive and Reactive Reference parameters dynamically generate value options for a build parameter using a Groovy script or a Scriptler script
In addition:
- Active Choices Reactive and Reactive Reference parameters can be dynamically updated (cascade update) when the value of other job UI control(s) change(s)
- Active Choices Reactive parameters can be rendered as standard selection lists, check boxes and radio buttons.
- A text box filter can be optionally shown to aid in filtering the value options.
Active Choices Reactive Reference parameters are used to enhance a Jenkins job form UI with reference information.
With this use case in mind, a Reactive Reference UI control can be rendered as:
- An HTML list (bulleted or numbered)
- An HTML input text box
- Dynamically generated HTML (image, iframe, etc.);
The dynamically generated HTML option, works with any well-formatted HTML returned by the Groovy script.It enables rendering of a variety of HTML elements, including pictures, inline-frames, hyperlinks, richly formatted text etc.
In addition, Reactive Reference parameters can be hidden from the UI and thus provide the option of dynamically generating hidden build parameters. These options are further discussed in the Reactive Reference configuration section.
In the example above the value options for the 'Professions' parameter get updated when the 'Gender' parameter changes.
In addition, the Reactive Reference parameter 'Gender_Balance' rendered as a picture is also dynamically updated when the 'Gender' parameter is updated.
The following table summarizes the behavior and rendering characteristics of the three Active Choices parameter types.
The following parameters are included in the plug-in:
- Active Choices Parameter
- Active Choices Reactive Parameter
- Active Choices Reactive Reference Parameter
We now describe the details of their configuration.
An Active Choices Parameter is configured by setting the following options in the parameter configuration
These are the typical parameter Name and Description that are common to all Jenkins parameters
The 'Script' is the Groovy code or Scriptlet script that will dynamically generate the parameter value options
-
By selecting either of the two radio button options you can either type a Groovy script directly or use a Scriptler script
-
The script must return a java.util.List, an Array or a java.util.Map, as in the example below:
return ['Option 1', 'Option 2', 'Option 3']
The 'Fallback Script' configuration option provides alternate parameter value options in case the main Script fails either by throwing an Exception, or by not return a java.util.List, Array or java.util.Map.
The 'Choice Type' option provides four different rendering options for the option values:
- A list box where a single selection is allowed
- A list box where multiple selections are allowed
- A list of check boxes (multiple selections allowed)
- A list of radio buttons (a single selection is allowed)
The 'Enable Filter' option will provide a text box filter in the UI control where a text filter can be typed. Only value options that contain the text are then listed.
This filer is case independent.
The Example 01 Active Choice parameter configuration generates the following build form UI control. The user can select a single 'State' option from a filterable drop down list.
It is possible to have some of the options displayed in an Active Choices UI control selected by default when the control is initialized.
You can define the default value selections by adding the suffix;
:selected to the element you want to be the default selection from
those returned by the script.
In the example below, we will make the State of 'Parana' the default
selection when the parameter UI control is rendered.
We credit the developers of the Dynamic Parameter plugin with some of the initial concepts and code on which Active Choices was implemented. However, there are several important differences and improvements between the Active Choices plugin and the original Dynamic Parameter plugin:
- An Active Choices parameter can be rendered as a multi-select control (combo-box or check-box) allowing users to select more than one value for the parameter
- The parameter options value list can be filtered. If the "Enable Filters" option is checked, an extra input box will be displayed allowing users to filter the options.
- You can define a 'fallback' behavior if the value generator script raises an exception.
- You can define default value selections in the dynamically generated value list
An Active Choices Reactive parameter is configured with a set of similar options as those shown above for the Active Choices parameter. However, a Reactive parameter provides the additional 'Referenced parameters' configuration option.
- This option, takes a list of job parameters that trigger an auto-refresh of the Reactive Parameter when any of the 'Referenced parameters' change
The 'Referenced parameters' text field contains a list of comma separated parameter Names (from the current job) that will trigger a refresh of the Reactive Parameter when their values change. The values of these parameters are passed to the script binding context before the script is re-executed to generate a new set of option values for the Active Choices control.
Let's examine a Jenkins build form rendered with Active Choice parameters that satisfies the following requirements. The form:
- Allows users to select one of several Brazilian States
- Provides an additional control where a set of Cities belonging to the selected State is dynamically displayed
- Allows the user to select one or more of the displayed Cities
The job UI is composed of two parameters:
The first parameter is the States Active Choice Parameter from
Example 01. It allows the user to select one of several Brazilian
States.
We could have just as easily used a Jenkins Choice Parameter, but we use
an Active Choice parameter (as shown from Example 01). The Groovy script
for this is:
return[
'Sao Paulo',
'Rio de Janeiro',
'Parana',
'Acre'
]
The second parameter is the Cities Active Choices Reactive Parameter that dynamically displays a set of cities belonging to the selected State and allows users to select multiple values. The Cities parameter configuration is shown above in Example 02.
Note that:
- The Cities Reactive parameter references the previously defined States parameter ('Referenced parameters'=States);
- The 'Choice Type' is set to Check Boxes.This will allow the user to select one or more Cities by selecting multiple check boxes.
- A custom 'Groovy Script' will be used to generate the Cities value options as shown below (the last list value returned by the script)
if (States.equals("Sao Paulo")) {
return ["Barretos", "Sao Paulo", "Itu"]
} else if (States.equals("Rio de Janeiro")) {
return ["Rio de Janeiro", "Mangaratiba"]
} else if (States.equals("Parana")) {
return ["Curitiba", "Ponta Grossa"]
} else if (States.equals("Acre")) {
return ["Rio Branco", "Acrelandia"]
} else {
return ["Unknown state"]
}
Whenever the user changes the option of the States parameter, the Cities parameter will get dynamically updated. Note how that the States referenced parameter is in the script binding and can be used directly.
You can use a Reactive parameter type for things like displaying the list of Maven artifacts, given a group ID.
A Reactive Reference parameter is configured with a set of similar options as those shown above for the Active Choices Reactive parameter.
However, a Reactive Reference parameter provides a unique set of rendering options (see 'Choice Type').
- Input text box
- Numbered list
- Bullet items list
- Formatted HTML
- Formatted Hidden HTML
Given the wide variety of rendering options the Active Choices Groovy script must return the following types of variables for each option:
Choice Type | Groovy Returns | Comment |
---|---|---|
Input text box | String | The return String appears in a simple text box |
Numbered list | List | The return List displays as a numbered list |
Bullet items list | List | The return List displays as a bulleted list |
Formatted HTML | String | The return String must be well formatted HTML to display correctly. You can include any HTML tags here, e.g.: some <table>, or a <form> to another web site. |
Formatted Hidden HTM | String | The parameter won't be displayed in the UI |
A typical application of a Reactive Reference parameter is to dynamically display reference information that can be used to guide the user in making an appropriate value selection of another job parameter.
By design, the values of Reactive Reference parameters are NOT passed to the build environment with one important exception. When the choice type is set to Formatted HTML or Formatted Hidden HTML and the HTML is an 'input' element the value can be passed to the build. See the 'Advanced Usage' section for additional instructions.
Below we present 3 examples of Reactive References with different Choice Types and their corresponding renderings in the Jenkins job UI.
Consider an example where the user needs to make a meal selection that complements the available wine selection. The food selection would be easier if some useful reference info could be offered when users considered a particular wine. We call this reference information the WINE_RULE and we can easily implement it using a Reactive Reference parameter.
The WINE_RULE gets automatically updated when a user makes a new selection from the WINE_MENU (Note Referenced parameters=WINE_MENU). As a result, when we make a WINE_MENU selection we also get a WINE_RULE that can guide the FOOD_MENU selection.
The complete configuration of the WINE_RULE parameter is shown below.
The groovy script that generates the WINE_RULE formatted HTML for each of the choices is shown below.
switch(WINE_MENU){
case~/.*Champagne.*/:
winerec='Champagne is perfect with anything salty'
return "<b>${winerec}</b>"
break
case ~/.*Sauvignon Blanc.*/:
winerec='Sauvignon Blanc goes with tart dressings and sauces'
return "<b>${winerec}</b>"
break
case~/.*Grüner Veltliner.*/:
winerec='Choose Grüner Veltliner when a dish has lots of fresh herbs'
return "<b>${winerec}</b>"
break
case~/.*Pinot Grigio.*/:
winerec='Pinot Grigio pairs well with light fish dishes'
return "<b>${winerec}</b>"
break
case ~/.*Chardonnay.*/:
winerec='Choose Chardonnay for fatty fish or fish in a rich sauce'
return "<b>${winerec}</b>"
break
case~/.*Off-Dry Riesling.*/:
winerec='Off-Dry Riesling pairs with sweet & spicy dishes'
return "<b>${winerec}</b>"
break
case~/.*Moscato dAsti.*/:
winerec='Moscato dAsti loves fruit desserts'
return "<b>${winerec}</b>"
break
case ~/.*dry Rosé.*/:
winerec='Pair a dry Rosé with rich, cheesy dishes'
return "<b>${winerec}</b>"
break
case~/.*Pinot Noir.*/:
winerec='Pinot Noir is great for dishes with earthy flavors'
return "<b>${winerec}</b>"
break
}
Your Groovy script binding has access to two additional variables for use:
- jenkinsProject -> The Jenkins Project object
- jenkinsBuild -> The Jenkins Build object
As was mentioned earlier, in general the values of reactive reference parameters are not passed to the build. However, there are some scenarios where the ability to pass these values would be of interest. For a more extensive discussion of this feature you can read here.
In this scenario, we want to provide the user a dynamic default value that is also editable. This can be accomplished with the following reactive reference configuration:
- Choice Type: Formatted HTML
- Groovy Script returning an HTML input element with the dynamic default value
- Advanced Option set to
Scenario 2: Pass a dynamically created value that is hidden (can't be edited by the user)
In this scenario, we want the build to have access to a dynamic parameter generated from user input/option selections in the UI. The parameter is created programmatically, and is not user-editable. This can be accomplished with the following reactive reference configuration:
-
Choice Type: Formatted Hidden HTML
-
Groovy Script returning an HTML input element with the dynamic default value
-
A 'Formatted Hidden HTML' Choice type is useful when you want to calculate values to use in the build, but these values should not be modified by the user(e.g. to compute the deploy URL).
In both scenarios the groovy script must return an HTML element formatted as follows:
return "<input name=\"value\" value=\"${ReactiveRefParam}\" class=\"setting-input\" type=\"text\">"
ReactiveRefParam is the Reactive Reference value that will be passed to the build
This is an interesting application of the Reactive Reference parameter. It allows you to create custom UI parameter controls with improved interactivity. See example
By default Reactive References pass to the build a hidden <input name="value" value="">. It means that your Reactive Reference parameter will always be empty, but you can use a Formatted HTML parameter and instruct the plug-in to not include this hidden value parameter.
You can click the Advanced button and there you will find an option to omit the value field. This will you let you define a value for the hidden parameter.
We assume users that need to use Scriptler generated parameters are already familiar with the Scriptler Plug-in. If you need further information on how to use the Scriptler Plug-in, please refer to its Wiki page first.
Similarly to a Groovy script, a Scriptler script is also written in Groovy and used to render the parameter. Your Scriptler script is also expected to return a java.util.List, Array or java.util.Map for Active Choices and Reactive parameters, or custom HTML elements for the Reactive Reference parameter. Note that the value of other build parameters (when using Scriptler in combination with Active Choices) will be available in the Scriptler script context. You do not need to define such parameters in the Scriptler interface, or during the job definition.
However, the main advantage that the Scriptler Plug-in provides is the creation of a reusable Groovy script catalog that can be used across multiple jobs or even for automation.
To make your Scriptler scripts reusable across multiple projects you should parameterize them and assign script parameters using build parameters.
TODO: Provide an example
Note that although the text 'Filter' box available for Active Choices parameters provides easy, case insensitive filtering by simply typing some text, it also support more sophisticated filtering using regular expressions.
The following example shows such an example where a complex options list
is filtered using a regular expression.
Active Choice versions before v2.0 may not be safe to use. Please review the following warnings before using an older version:
**Starting with Active Choices v2.0, sandboxed **Groovy scripts
for Active Choices Reactive Reference Parameter will no longer emit
HTML that is considered unsafe, such as <script>
tags. This may result
in behavior changes on Build With Parameters forms, such as missing
elements. To resolve this issue, Groovy scripts emitting HTML will need
to be configured to run outside the script security sandbox, possibly
requiring separate administrator approval in In-Process Script
Approval.
Active Choices will load two extra Javascript files, JQuery and unochoice.js.
- English
- Portuguese (Brazil)) Work in Progress
- If you want to include yours, send us a pull request with the messages.properties files or get in touch!
- - The parameters are supposed to be handled only by humans, and at the moment do not work when the job is triggered by plug-ins, API or scripts. Please see this issue for more.
- - Before filing issues, please take a look at the Troubleshooting Page
This plug-in is not developed for Hudson, and we don't fix bugs happening in any version of Hudson.
Plugin | 1.0 | 1.1 | 1.2 |
Jenkins | 1.580.1+ | 1.580.1+ | 1.580.1+ |
For commercial support, please get contact us via @tupilabs
URL | Description |
---|---|
http://biouno.org/publications.html | Contains a list of resources for several plug-ins created in the BioUno project, including Active Choices, R, and Image Gallery. |
https://www.cloudbees.com/event/topic/jenkins-ci-open-source-continuous-integration-system-scientific-data-and-image | In Ioannis Moutsatsos' talk, you can find slides about the Active Choices (née Uno-Choice) plug-in, as well as watch it being used too. |
- Updated minimum version of Jenkins to 2.60.3
- Updated dependency ssh-cli-auth to 1.4,
- Updated dependency script-security to 1.39
- Updated stapler-adjunct-jquery to 1.12.4-0
- JENKINS-51296: Project renaming is not propagated to Active Choices projectName (thanks to sebcworks)
- JENKINS-39665: Unnecessary Scrollbars on build with parameters screen when using Active Choices Plugin (thanks to Lubomir Stanko)
- JENKINS-53356: Scriptlet 'ViewScript' link and Required Parameters not displayed in configuration
- Fixed javadocs issues, missing license headers, simplified if expressions, and other minor code improvements
- Pull Request #25: Initial version. Allow to disable any select option with :disable (thanks to Ivan Aragonés Muniesa)
- JENKINS-49260: this.binding.jenkinsProject not returning project of current build
- JENKINS-51296: Project renaming is not propagated to Active Choices projectName
- Updates to dependencies from Jenkins and plugins, in pom.xml
- JENKINS-48230: Discover parameters on jobs' wrappers
- JENKINS-47908: After upgrade to v2.0, the active choices updates randomly in the browser
- JENKINS-48448: Add a variable with the parameter name
- JENKINS-43380: Input parameter HTML description not working
-
- Important: Sandboxed Groovy scripts for Active Choices Reactive Reference Parameter will no longer emit HTML that is considered unsafe, such as <script> tags. This may result in behavior changes on Build With Parameters forms, such as missing elements. To resolve this issue, Groovy scripts emitting HTML will need to be configured to run outside the script security sandbox, possibly requiring separate administrator approval in In-Process Script Approval.
-
JENKINS-42685: Remove custom stapler proxy as we can now use assync requests
-
JENKINS-31625: Add configuration parameter, which defines, when filter must started work
-
JENKINS-36158: Active choice reactive reference parameter not working on checkbox reference
-
JENKINS-43380: Input parameter HTML description not working
-
Pull request #15: Make Scriptler dependency optional (thanks to Daniel Beck)
- JENKINS-34487: Do not hang the browser when parameter re-evaluated (thanks to Vladimir Fedorov)
- JENKINS-38532: Active Choices Reactive Reference Parameters don't parse equals character ('=') properly.
- JENKINS-34188: jenkins.log noise: "script parameter ... is not an instance of Java.util.Map."
- JENKINS-39620: Saving a job with Active Choice 1.4 parameters after upgrade to v1.5 resets scriptlet parameters
- JENKINS-39760: Active Choices Parameters lost of Job Config save
- JENKINS-36590: Active-Choice jenkinsProject variable is not available under Folder or Multibranch-Multiconfiguration job
- JENKINS-37027: 'View selected script option' in build configuration displays wrong scriptler script
- JENKINS-34988: this.binding.jenkinsProject not returning project of current build
- Upgraded build plug-ins
- Fixed Findbugs issues
- Upgraded parent in order to be able to release to Jenkins plug-in repositories
- JENKINS-39620: Saving a job with Active Choice 1.4 parameters after upgrade to v1.5 resets scriptlet parameters
- JENKINS-39534: When Jenkins restarts, the groovy scripts are lost
- JENKINS-34818: Active Choice reactive parameter cannot access global parameters
- Removed from the update center. Read more about it here
- JENKINS-32405: Groovy script failed to run if build started by timer
- JENKINS-32461: jenkinsProject variable is not available in Multi-configuration project
- JENKINS-32566: Render an AC Reactive reference as a functional Jenkins File Parameter
- pull-request/6: Let "PhantomJS Unit Testing" be able to run on both linux and windows...
- JENKINS-30824: Active Choice Reactive Parameter - radios button value not passed to build (thanks to lyen liang for his PR showing how to fix it
- JENKINS-32044: Fail to evaluate Boolean parameter to "on" when checked
- JENKINS-30592: An AC Reactive Reference is always displayed unless it references a parameter
- JENKINS-32149: Create random parameter name only once
- JENKINS-29476: The 'jenkinsProject' variable is not set in the binding after restarting Jenkins (thanks to @perfhector for PR/5)
- pull-request/1: Use value of Reactive Reference dynamic input controls as build parameters. See example
- pull-request/2: Update unochoice.js
- JENKINS-29055: Problem with Active Choices 1.0 and jQuery
- JENKINS-28764: Environment variables are not expanded and thus cannot be used as parameter values to Scriptler scripts
- JENKINS-28785: Referenced Parameters Not Passed to Scriptler Scripts
- Initial release to Jenkins update center, with Choice, Cascade Dynamic Choice and Dynamic Reference parameter types.
The first commit happened on 2014/03/13, and the initial release on 2014/03/21. There were 24 releases to the BioUno update center, the last one being on 2015/03/26, when the project decided to publish it via Jenkins update center.