How to use the Active Choice plugin for parameterized builds in Jenkins

How to use the Active Choice plugin for parameterized builds in Jenkins

Overview:

  1. Active choices plugin
  2. The Active Choices plugin is used in parametrized freestyle Jenkins jobs to create scripted, dynamic, and interactive job parameters. Active Choices parameters can be dynamically updated and can be rendered as combo-boxes, check-boxes, radio buttons, or rich HTML UI widgets.
  3. There are three types of active parameters:
    • Active Choices Parameter (main idea: set parameter values to select for your build jobs)
    • Active Choices Reactive Parameter (main idea: parameters selected according to the specified value of the active choice parameter)
    • Active Choices Reactive Reference Parameter
  4. This post shows the usage of the first two parameters

Plugin installation:

To install the plugin go to Manage Jenkins --> Manage Plugins --> Select Available Tab --> Type Active Choices --> Click Install without restart:

Config of active choice parameters:

  1. Create freestyle project with the following name: demo-active-reactive-parameter:
  2. Select Active Choice Parameter:
  3. Specify environment parameter:
  4. Specify the Fallback script:
  5. Specify Choice Type --> Single Select and enable filters (optional):
  6. Save the project
  7. Now go to Build with parameters to see all your configs in action:

Config of active reactive parameters:

  1. Now to configure reactive parameters go to Configure again and select Active Choices Reactive Parameter
  2. Set the following configurations:
    if (environment.equals("dev")) {
    return ["develop", "feature"]
    }
    else if (environment.equals("test")) {
    return ["feature"]
    }
    else if (environment.equals("prod")) {
    return ["main", "master"]
    }
    else {
    return ["develop"]
    }
    
  3. Select referenced parameter (active choice parameter name) and enable filtering:
  4. Add build step:
    echo "ENV= $environment"
    echo "BRANCH= $git_branch"
    
  5. Apply the changes

Build:

  1. Now you can see that git_branch parameter reactively chooses its values according to the value of active choice environment parameter
  2. Build the project and check the parameters:

Notes:

  1. The 'Fallback Script' configuration option provides alternate parameter value options in case the main Script fails by throwing an Exception
  2. To make the active choice parameter default add selected option:
    "[parameter_name]:selected"
    
  3. To disable the active choice parameter use:
    "[parameter_name]:disabled"
    

Reference:

  1. Parameterized Builds with Active Choices Plugin, Active Reactive Parameters | Jenkins 2022
  2. Active Choices Reactive Reference Parameter in jenkins pipeline
  3. Jenkins Lab setup