Why scripted pipelines offer flexibility in Jenkins - Performance Analysis
We want to understand how the time it takes to run a scripted Jenkins pipeline changes as the pipeline grows.
Specifically, how does adding more steps or loops affect execution time?
Analyze the time complexity of the following scripted pipeline snippet.
node {
for (int i = 0; i < stages.size(); i++) {
stage(stages[i]) {
echo "Running stage: ${stages[i]}"
// some build steps here
}
}
}
This code runs a list of stages one by one, executing build steps inside each stage.
Look for loops or repeated actions.
- Primary operation: The for-loop that runs each stage.
- How many times: Once for each stage in the stages list.
As the number of stages increases, the total steps grow proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 stage executions |
| 100 | 100 stage executions |
| 1000 | 1000 stage executions |
Pattern observation: The time grows directly with the number of stages.
Time Complexity: O(n)
This means the total time increases in a straight line as you add more stages.
[X] Wrong: "Adding more stages won't affect the total time much because they run fast."
[OK] Correct: Each stage adds its own work, so more stages mean more total time.
Understanding how pipeline steps add up helps you design efficient builds and explain your choices clearly.
"What if we nested another loop inside each stage? How would the time complexity change?"
Practice
Solution
Step 1: Understand scripted pipeline features
Scripted pipelines allow writing code with loops, conditions, and error handling, giving more control.Step 2: Compare with declarative pipelines
Declarative pipelines have a simpler, fixed structure and less flexibility for complex logic.Final Answer:
Because they allow using loops, conditions, and custom error handling. -> Option CQuick Check:
Scripted pipelines = flexible code control [OK]
- Confusing simplicity with flexibility
- Thinking declarative pipelines allow all custom code
- Assuming scripted pipelines auto-generate code
Solution
Step 1: Identify scripted pipeline block syntax
Scripted pipelines start withnode {to define the workspace and steps.Step 2: Eliminate other options
pipeline {is for declarative pipelines;stage {defines a stage;scripted {is invalid syntax.Final Answer:
node { -> Option AQuick Check:
Scripted pipeline starts with node block [OK]
- Using 'pipeline {' which is declarative syntax
- Confusing 'stage {' as pipeline start
- Assuming 'scripted {' is valid syntax
node {
for (int i = 1; i <= 3; i++) {
echo "Count: ${i}"
}
}Solution
Step 1: Analyze the for loop range
The loop runs from i=1 to i=3 inclusive, so it iterates 3 times.Step 2: Check the echo output each iteration
Each iteration prints "Count: " followed by the current i value, producing lines for 1, 2, and 3.Final Answer:
Count: 1\nCount: 2\nCount: 3 -> Option AQuick Check:
Loop 1 to 3 prints counts 1,2,3 [OK]
- Starting loop at 0 instead of 1
- Thinking echo is invalid in scripted pipeline
- Assuming syntax error in for loop
node {
if (env.BRANCH_NAME == 'main')
echo 'Deploying to production'
else
echo 'Deploying to staging'
}Solution
Step 1: Check if-else syntax in scripted pipeline
In scripted pipelines, if and else blocks must use curly braces {} when spanning multiple lines or statements.Step 2: Identify missing braces
The snippet lacks braces around the echo statements, which can cause syntax errors or unexpected behavior.Final Answer:
Missing curly braces {} around if and else blocks -> Option BQuick Check:
If-else blocks need braces in scripted pipelines [OK]
- Assuming env.BRANCH_NAME syntax is wrong
- Thinking echo can't be inside if
- Believing node block forbids if statements
Solution
Step 1: Understand Jenkins retry step
Theretry(n) { ... }block retries the enclosed steps up to n times on failure automatically.Step 2: Evaluate each option
node { retry(3) { sh 'run-build.sh' } } correctly usesretry(3)around the build command. node { for (int i=0; i<3; i++) { sh 'run-build.sh' } } runs the command 3 times without retry on failure. node { try { sh 'run-build.sh' } catch (err) { retry(3) } } misuses retry inside catch. node { while (true) { sh 'run-build.sh' } } loops infinitely.Final Answer:
node { retry(3) { sh 'run-build.sh' } } -> Option DQuick Check:
Use retry(n) block for automatic retries [OK]
- Using loops instead of retry block
- Placing retry inside catch block incorrectly
- Creating infinite loops without exit
Ready to prove it?
Pass the test and this pattern moves unknown
