- Notifications
You must be signed in to change notification settings - Fork116
This plugin provides new scripted, dynamic parameters for freestyle jobs that can be rendered as combo-boxes, check-boxes, radio-buttons or rich HTML UI widgets.
License
jenkinsci/active-choices-plugin
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The Active Choices plugin is used in parametrized freestyle Jenkins jobs to createscripted, dynamic and interactivejob parameters. Active Choicesparameters can bedynamically updated and can berendered as combo-boxes,check-boxes, radio-buttons or rich HTML UI widgets.
Active Choices parameters are scripted using Groovy, or (optionally) Scriptler Groovy scripts. These custom scriptssupport the use of the Jenkins Java API, system environment variables, global node properties, and potentiallyexternal 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
NOTE: The Reactive Reference Parameter allows for parameters to be displayed asformatted HTML. When configuring jobs with this feature, keep in mind how the parameterwill be rendered and whether it could be a security issue.
NOTE: The plug-in was developed in a way that it relies heavily on the HTML/DOMof the Jenkins UI. We navigate the DOM using JavaScript to create the relationship andreactivity between parameters. FollowJENKINS-63284for updates on a version that does not require the UI. When that issue is closed, theplug-in should work fine with Pipelines, DSL, timers, cron, REST-API-triggered jobs, etc.
Active Choices parameters allow users to select value(s) for a jobparameter. Parameter values can be:
- dynamically generated (using Groovy or aScriptlerscript)
- 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 (see NOTE above on the security risks)
We will introduce the Active Choices based UI controls by briefly describing their behavior and renderingcharacteristics. 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 scriptor 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.
Similar to Active Choices Parameter above:
- Active Choices Reactive and Reactive Reference parameters dynamically generate value options for a build parameterusing a Groovy script or a Scriptler script
Furthermore:
- Active Choices Reactive and Reactive Reference parameters can bedynamically updated(cascade update) when thevalue 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 enablesrendering of a variety of HTML elements, includingpictures, inline-frames, hyperlinks, richly formatted text etc.
In addition, Reactive Reference parameters can behidden from the UI and thus provide the option of dynamicallygenerating 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 updatedwhen the 'Gender' parameter is updated.
The following table summarizes the behavior and rendering characteristics of the three Active Choices parameter types.
The plug-in includes the following parameter types:
- 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 theGroovy 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 aScriptler script
- The script must return ajava.util.List, anArray or ajava.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 failseither by throwing an Exception, or by not return ajava.util.List,Array, orjava.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 Choices parameter configuration generates the following build form UI control. The user canselect 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 thecontrol is initialized.
You candefine the default value selections by adding the suffix;:selected to the element you want to bethe 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.
You also candefine disabled selections by adding the suffix;:disabled to the element(s) you want to bedisabled. In the example below, we will make various elements to be disabled and immutable.
As you can see, both:selected and:disabled can be specified at the same time.
We credit the developers of theDynamic Parameter plugin with some of theinitial concepts and code on which Active Choices was implemented. However, there are several important differences andimprovements between the Active Choices plugin and the original Dynamic Parameter plugin:
- An Active Choices parameter can berendered as a multi-select control (combo-box or check-box) allowing userstoselect more than one value for the parameter
- The parameteroptions value list can be filtered. If the "Enable Filters" option is checked, an extra inputbox 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 definedefault value selectionsin the dynamically generated value list
An Active Choices Reactive parameter is configured with a set of similar options as those shown above for the ActiveChoices parameter. However, a Reactive parameter provides the additional 'Referenced parameters' configurationoption.
- This option, takes a list of job parameters that trigger anauto-refresh of the Reactive Parameter when any of the'Referenced parameters' change
The 'Referenced parameters' text field containsa 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 arepassed to the script binding context before the script is re-executed to generate a new set of option values for theActive Choices control.
Let's examine a Jenkins build form rendered with Active Choices 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 has two parameters:
The first parameter is the'States' Active Choices Parameter from 'Example 01'. It allows the user to select one ofseveral Brazilian States. We could have just as easily used a Jenkins Choice Parameter, but we use an Active Choiceparameter (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 thatdynamically displays a set of citiesbelonging to the selected State and allows users to select multiple values. The 'Cities' parameter configuration is shownabove 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 multiplecheck boxes.
- A custom 'Groovy Script' will be used to generate the 'Cities' value options as shown below (the last list valuereturned by the script)
if (States=="Sao Paulo") {return ["Barretos","Sao Paulo","Itu"]}elseif (States=="Rio de Janeiro") {return ["Rio de Janeiro","Mangaratiba"]}elseif (States=="Parana") {return ["Curitiba","Ponta Grossa"]}elseif (States=="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. Notehow 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 ChoicesReactive parameter.
However, aReactive 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 ofvariables 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 beused 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 importantexception. When the choice type is set toFormatted HTML orFormatted 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 inthe 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 particularwine. We call this reference information the 'WINE_RULE' and we can easily implement it using a Reactive Referenceparameter.
The 'WINE_RULE' gets automatically updated when a user makes a new selection from the 'WINE_MENU' (Note Referencedparameters=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>"case ~/.*Sauvignon Blanc.*/: winerec='Sauvignon Blanc goes with tart dressings and sauces'return"<b>${winerec}</b>"case ~/.*Grüner Veltliner.*/: winerec='Choose Grüner Veltliner when a dish has lots of fresh herbs'return"<b>${winerec}</b>"case ~/.*Pinot Grigio.*/: winerec='Pinot Grigio pairs well with light fish dishes'return"<b>${winerec}</b>"case ~/.*Chardonnay.*/: winerec='Choose Chardonnay for fatty fish or fish in a rich sauce'return"<b>${winerec}</b>"case ~/.*Off-Dry Riesling.*/: winerec='Off-Dry Riesling pairs with sweet & spicy dishes'return"<b>${winerec}</b>"case ~/.*Moscato dAsti.*/: winerec='Moscato dAsti loves fruit desserts'return"<b>${winerec}</b>"case ~/.*dry Rosé.*/: winerec='Pair a dry Rosé with rich, cheesy dishes'return"<b>${winerec}</b>"case ~/.*Pinot Noir.*/: winerec='Pinot Noir is great for dishes with earthy flavors'return"<b>${winerec}</b>"}
Your Groovy script binding has access to two additional variables for use:
jenkinsProject-> The Jenkins Project objectjenkinsBuild-> 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 discussionof this feature you can readhere.
In this scenario, we want to provide the user a dynamic default value that is also editable. This can be accomplishedwith the following reactive reference configuration:
- Choice Type:Formatted HTML
- Groovy Script returning anHTML input element with the dynamic default value
- Advanced Option set to
In this scenario, we wantthe build to have access to a dynamic parameter generated from user input/optionselections in the UI. The parameter is created programmatically, and is not user-editable. This can be accomplishedwith the following reactive reference configuration:
- Choice Type:Formatted Hidden HTML
- Groovy Script returning anHTML input element with the dynamic default value
- Advanced Option set to
A 'Formatted Hidden HTML' Choice type is useful when you want to calculate values to use in the build, but these valuesshould 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 parametercontrols with improved interactivity. Seeexample
By default, 'Reactive References' pass to the build a hidden<input name="value" value="">. It means that your 'ReactiveReference' parameter will always be empty, but you can use a 'Formatted HTML' parameter and instruct the plug-in to notinclude 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 youdefine a value for the hidden parameter.
We assume users that need to use Scriptler generated parameters are already familiar with the Scriptler Plug-in. Ifyou need further information on how to use the Scriptler Plug-in, please refer toits Wiki page first.
Similar to a Groovy script, a Scriptler script is also written in Groovy and used to render the parameter. YourScriptler script must return ajava.util.List,Array, orjava.util.Map for Active Choices and Reactiveparameters, 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 notneed 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 areusableGroovy scriptcatalog 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 scriptparameters using build parameters.
Environments.groovy in Scriptler
return ["Select:selected","DEV","TEST","STAGE","PROD"]
HostsInEnv.groovy in Scriptler
// Static content examples. These lists can be generated dynamically as an alternative.List devList= ["Select:selected","dev1","dev2"]List testList= ["Select:selected","test1","test2","test3"]List stageList= ["Select:selected","stage1"]List prodList= ["Select:selected","prod1","prod2","prod3","prod4"]List default_item= ["None"]if (Environment=='DEV') {return devList}elseif (Environment=='TEST') {return testList}elseif (Environment=='STAGE') {return stageList}elseif (Environment=='PROD') {return prodList}else {return default_item}
Pipeline inJenkinsfile
properties([ parameters([ [ $class:'ChoiceParameter',choiceType:'PT_SINGLE_SELECT',name:'Environment',script: [ $class:'ScriptlerScript',scriptlerScriptId:'Environments.groovy' ] ], [ $class:'CascadeChoiceParameter',choiceType:'PT_SINGLE_SELECT',name:'Host',referencedParameters:'Environment',script: [ $class:'ScriptlerScript',scriptlerScriptId:'HostsInEnv.groovy',parameters: [ [name:'Environment',value:'$Environment'] ] ] ] ])])pipeline { agent any stages { stage('Build') { steps { echo"${params.Environment}" echo"${params.Host}" } } }}
Note that although the text 'Filter' box available for Active Choices parameters provides easy, case-insensitivefiltering by simply typing some text, it also supports 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 Choices versions before v2.0 may not be safe to use. Please review the following warnings before using an olderversion:
Starting with Active Choices v2.0, sandboxed Groovy scripts for Active Choices Reactive Reference Parameter willnolonger emit HTML that is considered unsafe, such as<script> tags. This may result in behavior changes onBuild With Parameters forms, such as missing elements. To resolve this issue, Groovy scripts emitting HTML will needto be configured to run outside the script security sandbox, possibly requiring separate administrator approval inIn-Process Script Approval.
Active Choices will load two extra Javascript files, JQuery andunochoice.js.
- English
- Portuguese (Brazil))Work-In-Progress
- If you want to include your language, send us a pull request with the
messages.propertiesfiles 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 triggeredby plug-ins, API or scripts. Please seethis issue for more.
- Before filing issues, please take a look at theTroubleshooting Page
See theCHANGES.md file.
For commercial support, please get contact us via@tupilabs.
For more about where the plug-in came from, check out theBioUno project.
Get in touch if you would like to sponsor the development of the plug-in, or an open issue in JIRA.
- “Dynamic and reactive parameterization in Jenkins pipelines using HTML, Groovy, and Bash” byEsteban Echavarria Collazos -link
Check out a list of articles, tutorials, and publications at theBioUno website
About
This plugin provides new scripted, dynamic parameters for freestyle jobs that can be rendered as combo-boxes, check-boxes, radio-buttons or rich HTML UI widgets.
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- Java63.9%
- JavaScript30.2%
- TypeScript3.3%
- CSS1.1%
- HTML1.0%
- Groovy0.3%
- Other0.2%


















