Dynamic and reactive parameterization in Jenkins pipelines using HTML, Groovy, and Bash.
By: Esteban Echavarria Collazos
Tags: Jenkins, Pipeline As Code, Globant, DevOps, Pipeline

A few years ago DevOps culture boomed and started to become a must-have in most microservices architectures thanks to the ease of automating the applications life-cycle for hundreds of services simultaneously. This increase in demand also brought new challenges and possible ways to develop CI/CD workflows and multiple technologies to do it.

In our particular case, we will be talking about Jenkins and how you could extend its possibilities in terms of pipeline parameterization using a plugin called “Active Choices”

Content:

1- What is this plugin called “Active Choices” and why is it so useful

2- How does the plugin “Active Choices” works?

3- A practical example of the Active Choices plugin in AWS: Preview and upload an image to an existing S3 bucket.

4- Creating pipeline parameters

5- Conclusions

1- What is this plugin called “Active Choices” and why is it so useful?

As described in its docs: “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.”

Usually, when there are hundreds of services to deploy, the pipeline is parameterized to allow you to select from only one pipeline, multiple kinds of deployment, (i.e. which environment should be deployed, with what name, if it should execute test…) Jenkins currently allows you to use some predefined parameters like choice, checkbox or string parameter. When you’ve to manage pipelines that are not the same, you will see that there are certain cases when the parameters provided by Jenkins are not enough because of its static nature, in these cases, the plugin “Active Choices” comes to enrich your parameterization options allowing you to pass reactive references between parameters and render custom HTML in your parameterization options.

2- How does the plugin “Active Choices” works?

It provides 3 new kinds of parameters when you’re configuring your pipeline:

Active Choices Parameter : allows you to use a Groovy or Scriplet script to determine whether your input will be computed or predefined and return your results depending on the computations made.

: allows you to use a Groovy or Scriplet script to determine whether your input will be computed or predefined and return your results depending on the computations made. Active Choices Reactive Parameter : Same as the active choices, allows you to use a Groovy or Scriplet script, and additionally, these parameters will be recalculated when a referenced parameter changes (i.e. ENV is the referenced parameter when ENV is DEV, the parameters will react and disable the “Execute test” choice parameter)

: Same as the active choices, allows you to use a Groovy or Scriplet script, and additionally, these parameters will be recalculated when a referenced parameter changes (i.e. ENV is the referenced parameter when ENV is DEV, the parameters will react and disable the “Execute test” choice parameter) Active Choices Reactive Reference Parameter: Contains Active Choice Parameter and Active Choice Reactive Parameter options, and additionally enables new parameter options such as HTML widgets, bulleted or numbered lists, and input boxes.

3- A practical example of the Active Choices plugin in AWS: Preview and upload an image to an existing S3 bucket.

Suppose you do now want your team to have direct access to AWS management console to upload your assets to S3, since it will incur operational overheat (Users creation, permissions, policies…), instead you would prefer to centralize the image uploading for your assets in Jenkins avoiding giving individual credentials with S3-specific permissions to users. You also want to be able to list the S3 directories and determine in which directory will be the asset uploaded.

To accomplish that, you will need the following things:

An input string parameter for the image URL with an HTML Reactive Reference Parameter to preview the image.

An input text box for your image name. (Tunned with HTML)

A scripted Active Choices Parameter that pulls all S3 buckets to select where to upload the image.

And as a pre-requisite you will need the following things:

Jenkins Plugin: Active Choices

AWS Credentials are already configured in your Jenkins agent.

AWS S3 Permission for Read and Put.

4- Creating pipeline parameters:

To create the parameters, we are going to enter into the “Configure” tab in our pipeline, click on “This project is parameterized”, once enabled, click on “Add parameter” and select “String Parameter” to create our image input box.