In Jenkins, the “Choice Parameter” is a parameter that allows you to select a single value from a predefined list of values. This can be useful when you want to allow users to select from a list of options when building a Jenkins job: selecting a specific version of a software component to build or test.
This post will teach you how to create a choice parameter within a Jenkins job.
Jenkins Create Choice Parameter
To create a Choice parameter in Jenkins, follow these steps:
Start by logging into the Jenkins dashboard.
Click on the New Item option to create a new Jenkins job.
Next, give a name for your job and select the job type as “Pipeline.”
Scroll down to the “Pipeline” section and select the Definition “Pipeline Syntax” from the dropdown.
To add a choice parameter, select the “Pipeline Syntax” link to open the Pipeline Generator.
Select the “Declarative Directive Generator” from the left-hand menu.
Select “parameters: Parameters” from the provided options in the sample directive option.
Click on the “Add” option to create a new parameter type. Choose the parameter type as choice.
Give the parameter a name and provide various choices to select the values. Once you are satisfied with the option, select generate syntax to generate the target pipeline syntax.
This should give you a valid pipeline sample to add to your code. For example:
choice choices: ['debian_amd64', 'ubuntu_amd64', 'centos_amd64'], description: 'Choose your favorite distribution', name: 'dist'
}
Once satisfied, we can copy the code and paste it into the pipeline section of our pipeline. An example full script is as shown:
agent any
parameters {
choice choices: ['debian_amd64', 'ubuntu_amd64', 'centos_amd64'], description: 'Choose your favorite distribution', name: 'dist'
}
stages {
stage('Build') {
steps {
echo "Building for distribution: ${params.dist}"
}
}
}
}
This pipeline defines a single stage called “Build,” which contains a single step that prints a message to the console indicating the value of the dist parameter.
When we run this pipeline, we will be presented with a dropdown list containing the values debian_amd64, ubuntu_amd64, and centos_amd64. We then select one of these values and then run the pipeline.
Conclusion
In this, you learned how to use the Jenkins declarative generator and the Jenkins parameter block to create a parameter from a list of values.
Scroll down to the “Post-build Actions” section and click the “Add post-build action” dropdown. Select “Build Other Projects” from the options.
- In the “Projects to build” field, enter the name of the job that you want to trigger.
- Scroll down to the “Parameters” section and click the “Add parameter” dropdown. Select “Choice parameter” from the options.
- Enter a name for your parameter (e.g. “MY_CHOICE”) and a description (optional).
- In the “Choices” field, enter the list of values that you want to be available for selection. Each value should be on a new line.
- Click the “Save” button to save your job.
When you build the job, you will be presented with a dropdown list containing the values that you specified. You can select one of the values and then build the job. The value that you selected will be passed to the command that you entered in the “Command” field, and can be used in your build script.