How does Jenkins read dynamic parameters?
Jenkins can read dynamic parameters using plugins.
- Firstly, make sure you have installed the “Parameterized Build” plugin. You can find and install it in “Manage Jenkins” -> “Manage Plugins” on the Jenkins homepage.
- Open the configuration page in your Jenkins project.
- Under the “Build” section in the “General” category, click on the drop-down menu for “Add Parameter” and select “Choice Parameter”.
- In the “Name” field, enter the name of the parameter, for example, “dynamic parameter”.
- In the “Choices” field, enter the different options you wish to provide, separating them with commas, for example “option1, option2, option3”.
- Optionally, enter the default value in the “Default Value” field.
- Click on the “Advanced” button, then in the “Script” section under the “Advanced” section, enter a Groovy script to dynamically generate options. For example, you can use the following script to generate date options:
def today = new Date()
def tomorrow = today + 1
def twoDaysLater = today + 2
return [today, tomorrow, twoDaysLater].collect { date ->
date.format("yyyy-MM-dd")
}
- Click “Save” to save the configuration.
Your Jenkins project will now be able to read dynamic parameters during the build, allowing users to choose a value from the provided options. If you use a Groovy script to generate the options, a new set of options will be generated every time the build is run.