In Jenkins’s declarative pipeline, you can add parameters as part of Jenkinsfile. There are many supported parameters types that you can use with a declarative pipeline.
In this blog, you have answers to the following.
- How to use parameters in the declarative pipeline?
- How to use dynamic parameters or active choice parameters in the declarative pipeline?
Generating Pipeline Code for Parameters
You can generate the parameter pipeline code block easily using the Jenkins pipeline generator. You will find the Pipeline syntax generator link under all the pipeline jobs, as shown in the image below.
Navigate to the pipeline generator in Jenkins and under steps, search for properties, as shown below.
Using Parameters in Jenkinsfile
This script given below has the following parameter types.
- Choice parameters
- Boolean parameter
- Multi-line string parameter
- String Parameter
Here is the Github link for this code.
Access Parameters Inside Pipeline Stages
You can access a parameter at any stage of a pipeline. Accessing parameters in stages is pretty straightforward. You just have to use params. in places where you need to substitute the parameter.
Here is an example of a stage that will be executed based on the condition that we get from the choice parameter.
The parameter name is , and we access it in the stage as . So when the choice parameter matches PROD, it will execute the steps mentioned in the stage.
Using Active Choice Parameter in Declarative Pipeline for Dynamic Parameters
Unlike default parameter types, the Active choice parameter type gives you more control over the parameters using a groovy script. You can have dynamic parameters based on user parameter selection.
To use the active choice parameter, you need to have an Active Choices plugin installed in Jenkins.
Here is a small use case for an active choice parameter.
- A job should have three parameters
- Environment (dev, stage & prod)
- AMI List (Should list the AMIs based on environment)
- AMI information (Show information about the AMIs related to a specific environment)
- If the user selects dev, the AMI list should dynamically change the values related to dev and show information related to the AMIs.
Here is the image which shows the above use case. It shows how the AMI list and AMI information changes when you select different environments.
There are three types of active choice parameters.
Active Choices Parameter
Thi parameter type returns a set of parameters returned by the groovy script. For example, an environment parameter that lists dev, stage, and prod values.
You can also return values from third-party APIs as parameters.
One such example is dynamically showing folders from a Github repo in the Jenkins parameters. To make this work you just need to write a groovy script that calls Github APIs and query the folders of the specific repository.
Active Choices Reactive Parameter
Returns parameters based on conditions based on another referenced parameter. You can refer to an active choice parameter and return a parameter based on a condition. For example, if the environment parameter is selected as a dev, the reactive parameter will return AMI ids for dev based on groovy conditions.
In the following example, is the reference parameter.
Active Choices Reactive Reference Parameter
The reactive reference parameter is similar to a reactive parameter except for the fact that it mostly will not be used in the build environment. Meaning, it is often used to display information to the user dynamically to select the correct values from the other parameter input fields, as shown in the above use case image.
Using Active Choice Parameters With Declarative Pipeline
If you are wondering how to use active choice parameters in a declarative pipeline, here is the Jenkinsfile with all Active Choice parameter types. If you execute this, you will get parameters like the demo I have shown with the use case.
If you have trouble copying the code, use this Github link
Jenkinsfile Parameter Best Practices
The following are some of the best practices you can follow while using parameters in a Jenkinsfile.
- Never pass passwords in the String or Multi-line parameter block. Instead, use the password parameter of access Jenkins credentials with credential id as the parameter.
- Try to use parameters only if required. Alternatively, you can use a config management tool to read configs or parameters in the runtime.
- Handle the wrong parameter execution in the stages with a proper exception handling. It avoids unwanted step execution when a wrong parameter is provided. It happens typically in multi-line and string parameters.
Jenkinsfile Parameter FAQs How to dynamically populate the choice parameter in the declarative pipeline?
Dynamic parameters can be achieved by using an active choice parameter. It uses a groovy script to dynamically populate choice parameter values.
How are the parameters used in the declarative pipeline?
In the declarative pipeline, parameters can be incorporated using the properties block. It supports all types of Jenkins parameters.
How to generate pipeline code for parameters?
You can use the native Jenkins pipeline syntax generator to generate the code block for any type of pipeline parameter.