When to use scripted over declarative in Jenkins - Time & Space Complexity
We want to understand how the choice between scripted and declarative Jenkins pipelines affects the time it takes to run jobs as they grow in size and complexity.
How does the pipeline style impact the work Jenkins does as we add more steps or logic?
Analyze the time complexity of this scripted pipeline snippet.
node {
stage('Build') {
for (int i = 0; i < stepsCount; i++) {
echo "Running build step ${i}"
// simulate build step
}
}
stage('Test') {
echo 'Running tests'
}
}
This scripted pipeline runs a loop of build steps followed by a test stage.
Look for loops or repeated actions that affect execution time.
- Primary operation: The for-loop running build steps repeatedly.
- How many times: The loop runs
stepsCounttimes, which can vary.
As stepsCount grows, the number of build steps increases linearly.
| Input Size (stepsCount) | Approx. Operations |
|---|---|
| 10 | 10 build steps + 1 test stage |
| 100 | 100 build steps + 1 test stage |
| 1000 | 1000 build steps + 1 test stage |
Pattern observation: The total work grows directly with the number of build steps.
Time Complexity: O(n)
This means the time to run the pipeline grows in a straight line as you add more build steps.
[X] Wrong: "Using scripted pipelines always makes execution slower than declarative ones."
[OK] Correct: The execution time depends on the number of steps and logic, not just the pipeline style. Scripted pipelines can be just as fast if they avoid unnecessary loops or complex logic.
Understanding how pipeline style affects execution helps you explain trade-offs clearly and shows you can think about scaling Jenkins jobs efficiently.
What if we replaced the for-loop with parallel steps? How would the time complexity change?
Practice
Solution
Step 1: Understand pipeline types
Declarative pipelines are simpler and use a fixed syntax, while scripted pipelines allow full Groovy code control.Step 2: Match use case to pipeline type
Complex loops and dynamic conditions require scripted pipelines because declarative syntax is limited.Final Answer:
When you need complex loops and conditional logic that declarative can't handle -> Option CQuick Check:
Complex logic = scripted [OK]
- Thinking declarative supports all Groovy features
- Using scripted for simple pipelines unnecessarily
- Confusing parallel stage support as exclusive to scripted
Solution
Step 1: Identify scripted pipeline syntax
Scripted pipelines use Groovy code inside anodeblock, not thepipelineblock.Step 2: Check each option
node { stage('Build') { echo 'Hello' } } usesnode { stage('Build') { echo 'Hello' } }which is correct scripted syntax.Final Answer:
node { stage('Build') { echo 'Hello' } } -> Option AQuick Check:
Scripted pipelines start with node block [OK]
- Using declarative
pipelinesyntax for scripted pipelines - Omitting the node block in scripted pipelines
- Confusing
stagesblock as scripted syntax
node {
for (int i = 1; i <= 3; i++) {
echo "Count: ${i}"
}
}Solution
Step 1: Understand scripted pipeline loop
The for loop runs from 1 to 3, printing each count with echo.Step 2: Predict output
Each iteration prints "Count: 1", "Count: 2", and "Count: 3" on separate lines.Final Answer:
Count: 1\nCount: 2\nCount: 3 -> Option AQuick Check:
Loop prints each count line [OK]
- Assuming only last iteration prints
- Thinking for loop syntax is invalid in scripted
- Believing echo cannot be used inside node
node {
stage('Build') {
echo 'Building...'
}
stage('Test')
echo 'Testing...'
}Solution
Step 1: Check stage syntax in scripted pipeline
Each stage block must have braces {} to group its commands.Step 2: Identify missing braces
The 'Test' stage lacks braces around echo, causing syntax error.Final Answer:
Missing braces {} around the 'Test' stage block -> Option DQuick Check:
All stage blocks need braces in scripted [OK]
- Omitting braces for single commands in stages
- Thinking stage keyword is invalid in scripted
- Misplacing node and stage blocks
Solution
Step 1: Understand dynamic stage requirements
Creating stages dynamically requires looping and conditional logic.Step 2: Match pipeline type to dynamic behavior
Scripted pipelines allow Groovy loops to generate stages dynamically; declarative pipelines are static.Final Answer:
Scripted pipeline, because it allows loops and dynamic stage generation with Groovy -> Option BQuick Check:
Dynamic stages = scripted pipeline [OK]
- Assuming declarative supports dynamic stages easily
- Believing scripted pipelines cannot handle multiple stages
- Thinking Jenkins disallows dynamic pipeline structures
Ready to prove it?
Pass the test and this pattern moves unknown
