📖 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
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
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
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
Hint
Use echo with params.ENVIRONMENT and params.VERSION to show selections.
Practice
(1/5)
1. What is the main purpose of using dynamic parameter values in Jenkins jobs?
easy
A. To update input options based on other inputs or external data
B. To make Jenkins jobs run faster
C. To disable user inputs during job execution
D. To automatically restart failed jobs
Solution
Step 1: Understand dynamic parameters
Dynamic parameters change their available options based on other inputs or external data sources.
Step 2: Identify the purpose
This makes Jenkins jobs flexible and user-friendly by showing relevant choices dynamically.
Final Answer:
To update input options based on other inputs or external data -> Option A
But Jenkins shows a syntax error. What is the problem?
medium
A. The parameter SEARCH must be declared as a string
B. The list should be a map, not a list
C. The script must use double quotes for strings
D. The method should be findAll instead of filter
Solution
Step 1: Check Groovy list filtering methods
Groovy uses findAll to filter lists, not filter which causes syntax error.
Step 2: Confirm other parts are correct
The list syntax and parameter usage are valid; string quotes can be single or double.
Final Answer:
The method should be findAll instead of filter -> Option D
Quick Check:
Use findAll for filtering lists in Groovy [OK]
Hint: Use findAll, not filter, to filter lists in Groovy [OK]
Common Mistakes:
Using filter instead of findAll causes syntax errors
Thinking list must be a map for filtering
Believing quotes type causes syntax error
5. You want a Jenkins job parameter that lists all Git branches dynamically from a repository URL. Which approach correctly implements this dynamic parameter?
hard
A. Use Active Choices Plugin with a Groovy script that runs git ls-remote --heads <repo_url> and parses branch names
B. Use a String parameter and ask users to type branch names manually
C. Use a Boolean parameter to select if branches should be listed
D. Use a fixed Choice parameter with hardcoded branch names
Solution
Step 1: Identify dynamic branch listing method
Active Choices Plugin with Groovy script can run shell commands to fetch live branch names from Git.
Step 2: Exclude static or unrelated options
String parameter requires manual input, Boolean parameter is not for listing, fixed Choice is static and not dynamic.
Final Answer:
Use Active Choices Plugin with a Groovy script that runs git ls-remote --heads <repo_url> and parses branch names -> Option A
Quick Check:
Active Choices + git command fetches dynamic branches [OK]
Hint: Use Active Choices with git command to list branches dynamically [OK]
Common Mistakes:
Using static parameters instead of dynamic scripts
Choosing Boolean parameter for listing options
Forgetting to parse git command output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.