Dynamic parameter values in Jenkins - Time & Space Complexity
We want to understand how the time to get dynamic parameter values changes as the number of options grows.
How does the system handle more data when filling parameters dynamically?
Analyze the time complexity of the following Jenkins pipeline snippet.
properties([
parameters([
choice(name: 'ENV', choices: getEnvironments(), description: 'Select environment')
])
])
def getEnvironments() {
return ['dev', 'test', 'stage', 'prod']
}
This code dynamically sets a choice parameter by calling a function that returns a list of environment names.
Look for repeated actions that take time as input grows.
- Primary operation: Iterating over the list of environments to build the choices.
- How many times: Once per environment in the list.
As the number of environments increases, the time to prepare choices grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations to list choices |
| 100 | 100 operations to list choices |
| 1000 | 1000 operations to list choices |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to get dynamic parameter values grows linearly with the number of options.
[X] Wrong: "Dynamic parameters always load instantly no matter how many options there are."
[OK] Correct: The system must process each option, so more options mean more time to prepare the list.
Understanding how dynamic parameters scale helps you explain performance in real Jenkins pipelines.
"What if the getEnvironments() function fetched data from an external API instead of a static list? How would the time complexity change?"
Practice
dynamic parameter values in Jenkins 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 AQuick Check:
Dynamic parameters update options dynamically [OK]
- Thinking dynamic parameters speed up job execution
- Confusing dynamic parameters with job restart features
- Assuming dynamic parameters disable inputs
Solution
Step 1: Identify plugins for dynamic parameters
The Active Choices Plugin allows parameters to be dynamically generated using Groovy scripts.Step 2: Exclude unrelated plugins
Git Plugin manages source control, Pipeline Plugin manages job pipelines, and Mailer Plugin sends emails, none create dynamic parameters.Final Answer:
Active Choices Plugin -> Option CQuick Check:
Active Choices Plugin enables dynamic parameters [OK]
- Choosing Git Plugin for dynamic parameters
- Confusing Pipeline Plugin with parameter plugins
- Selecting Mailer Plugin unrelated to parameters
return ['dev', 'test', 'prod'].findAll { it.startsWith(params.ENV_PREFIX ?: '') }What will be the parameter options if
ENV_PREFIX is set to t?Solution
Step 1: Understand the script filtering
The script returns list items starting with the value ofENV_PREFIX. Here,ENV_PREFIXis 't'.Step 2: Filter the list
From ['dev', 'test', 'prod'], only 'test' starts with 't'.Final Answer:
['test'] -> Option BQuick Check:
Filter by prefix 't' gives ['test'] [OK]
- Ignoring the prefix filter and returning full list
- Choosing empty list when prefix matches an item
- Selecting wrong matching item
return ['one', 'two', 'three'].filter { it.contains(params.SEARCH) }But Jenkins shows a syntax error. What is the problem?
Solution
Step 1: Check Groovy list filtering methods
Groovy usesfindAllto filter lists, notfilterwhich 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 befindAllinstead offilter-> Option DQuick Check:
Use findAll for filtering lists in Groovy [OK]
- Using filter instead of findAll causes syntax errors
- Thinking list must be a map for filtering
- Believing quotes type causes syntax error
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 runsgit ls-remote --heads <repo_url>and parses branch names -> Option AQuick Check:
Active Choices + git command fetches dynamic branches [OK]
- Using static parameters instead of dynamic scripts
- Choosing Boolean parameter for listing options
- Forgetting to parse git command output
Ready to prove it?
Pass the test and this pattern moves unknown
