Script blocks for Groovy in Jenkins - Time & Space Complexity
We want to understand how the time it takes to run Groovy script blocks in Jenkins changes as the amount of work inside the block grows.
Specifically, how does adding more repeated tasks inside a script block affect the total execution time?
Analyze the time complexity of the following Jenkins Groovy script block.
node {
stage('Example') {
script {
for (int i = 0; i < n; i++) {
echo "Processing item ${i}"
}
}
}
}
This code runs a loop inside a Groovy script block that prints a message for each item from 0 to n-1.
Look for repeated actions inside the script block.
- Primary operation: The
forloop that runsechocommands. - How many times: Exactly
ntimes, wherenis the loop limit.
As n grows, the number of echo commands grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 echo commands |
| 100 | 100 echo commands |
| 1000 | 1000 echo commands |
Pattern observation: The total work grows directly in proportion to n. Double n, double the work.
Time Complexity: O(n)
This means the time to run the script block grows linearly with the number of loop iterations.
[X] Wrong: "The script block runs in constant time no matter how many times the loop runs."
[OK] Correct: Each loop iteration runs a command, so more iterations mean more work and more time.
Understanding how loops inside script blocks affect execution time helps you explain and predict Jenkins pipeline performance clearly and confidently.
"What if we nested another loop inside the script block? How would the time complexity change?"
Practice
script blocks in Jenkins pipelines?Solution
Step 1: Understand Jenkins pipeline structure
Jenkins pipelines use declarative syntax but allow Groovy code insidescriptblocks for flexibility.Step 2: Identify the role of
scriptblocksscriptblocks let you write Groovy code for loops, conditions, and other logic not directly supported in declarative syntax.Final Answer:
To run Groovy code for custom logic inside the pipeline -> Option CQuick Check:
Script blocks = Groovy code inside pipeline [OK]
- Confusing script blocks with environment or agent declarations
- Thinking script blocks define stages
- Assuming script blocks are optional for all logic
script block inside a declarative Jenkins pipeline?Solution
Step 1: Recall correct syntax for script blocks
In declarative pipelines,scriptis a block keyword followed by curly braces containing Groovy code.Step 2: Compare options
Onlyscript { ... }is valid Groovy block syntax. Others use invalid punctuation or keywords.Final Answer:
script { echo 'Hello' } -> Option AQuick Check:
script block syntax = script { ... } [OK]
- Using colons or arrows instead of braces
- Adding parentheses after script keyword
- Misplacing quotes or braces
pipeline {
agent any
stages {
stage('Example') {
steps {
script {
def sum = 0
for (i in 1..3) {
sum += i
}
echo "Sum is ${sum}"
}
}
}
}
}Solution
Step 1: Analyze the for loop inside script block
The loop runs from 1 to 3 inclusive, adding each number to sum: 1 + 2 + 3 = 6.Step 2: Understand echo output
The echo prints the string with sum value interpolated, so output is "Sum is 6".Final Answer:
Sum is 6 -> Option AQuick Check:
1+2+3=6 output = Sum is 6 [OK]
- Thinking loop ends at 2
- Confusing sum initialization
- Assuming syntax error due to script block
pipeline {
agent any
stages {
stage('Test') {
steps {
script {
for i in 1..3 {
echo "Number: ${i}"
}
}
}
}
}
}Solution
Step 1: Check for loop syntax in Groovy
Groovy requires parentheses around the for loop variable and range:for (i in 1..3).Step 2: Identify the error in given code
The code usesfor i in 1..3without parentheses, causing syntax error.Final Answer:
Missing parentheses around for loop variable and range -> Option DQuick Check:
Groovy for loop needs ( ) [OK]
- Omitting parentheses in for loops
- Misplacing script block outside steps
- Confusing echo usage inside script
script block?Solution
Step 1: Check map creation and loop syntax
def squares = [:] for (i in 1..4) { squares[i] = i * i } squares.each { k, v -> echo "${k} squared is ${v}" } correctly creates an empty map with[:]and uses parentheses in the for loop.Step 2: Verify square calculation and iteration
def squares = [:] for (i in 1..4) { squares[i] = i * i } squares.each { k, v -> echo "${k} squared is ${v}" } usesi * ifor squares and iterates map witheach { k, v -> ... }to echo key-value pairs.Final Answer:
def squares = [:] for (i in 1..4) { squares[i] = i * i } squares.each { k, v -> echo "${k} squared is ${v}" } -> Option BQuick Check:
Map with for (i in 1..4) and i*i is correct [OK]
- Using list [] instead of map [:]
- Omitting parentheses in for loop
- Using ^ for power instead of *
- Incorrect map iteration syntax
Ready to prove it?
Pass the test and this pattern moves unknown
