0
0
Jenkinsdevops~30 mins

Dynamic parameter values in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Dynamic Parameter Values in Jenkins Pipeline
📖 Scenario: You are setting up a Jenkins pipeline for a software project. You want to create a build parameter that changes its available options based on another parameter's value. This helps the team pick the right build options easily.
🎯 Goal: Build a Jenkins pipeline script that defines two parameters: ENVIRONMENT and VERSION. The VERSION parameter's options should change dynamically depending on the selected ENVIRONMENT value.
📋 What You'll Learn
Create a string parameter called ENVIRONMENT with options: dev, test, prod
Create a dynamic choice parameter called VERSION whose options depend on the ENVIRONMENT value
Use a Groovy script inside the pipeline to set the VERSION options dynamically
Print the selected ENVIRONMENT and VERSION values in the pipeline output
💡 Why This Matters
🌍 Real World
Dynamic parameters help teams avoid mistakes by showing only relevant options based on previous choices, making builds faster and less error-prone.
💼 Career
Understanding dynamic parameters is useful for Jenkins administrators and DevOps engineers to create flexible and user-friendly pipelines.
Progress0 / 4 steps
1
Create the ENVIRONMENT parameter
Create a Jenkins pipeline parameter called ENVIRONMENT with the choices dev, test, and prod using the parameters block.
Jenkins
Need a hint?

Use the choice parameter type inside the parameters block.

2
Add a dynamic VERSION parameter
Add a VERSION parameter using the extendedChoice plugin or a Groovy script that will later be set dynamically based on ENVIRONMENT. For now, add the parameter with an empty placeholder list.
Jenkins
Need a hint?

Add a choice parameter named VERSION with an empty list for now.

3
Set VERSION options dynamically based on ENVIRONMENT
Inside the stages block, add a script step that sets the VERSION parameter choices dynamically using a Groovy map. Use dev -> ["1.0-dev", "1.1-dev"], test -> ["1.0-test", "1.1-test"], and prod -> ["1.0", "1.1"]. Assign the correct list to a variable called versionOptions based on params.ENVIRONMENT.
Jenkins
Need a hint?

Use a Groovy map to store version lists and select based on params.ENVIRONMENT.

4
Print the selected ENVIRONMENT and VERSION
Add echo statements inside the steps block to print the selected ENVIRONMENT and VERSION values using params.ENVIRONMENT and params.VERSION.
Jenkins
Need a hint?

Use echo with params.ENVIRONMENT and params.VERSION to show selections.