Scripted vs declarative comparison in Jenkins - Performance Comparison
We want to understand how the time it takes to run Jenkins pipelines changes when using scripted versus declarative styles.
How does the pipeline style affect the work Jenkins does as the pipeline grows?
Analyze the time complexity of these two Jenkins pipeline snippets.
// Scripted pipeline example
node {
for (int i = 0; i < stages.size(); i++) {
stage(stages[i]) {
echo "Running stage ${stages[i]}"
}
}
}
// Declarative pipeline example
pipeline {
stages {
stage('Build') {
steps { echo 'Building...' }
}
stage('Test') {
steps { echo 'Testing...' }
}
}
}
The scripted pipeline uses a loop to run stages dynamically, while the declarative pipeline defines stages explicitly.
Look at what repeats in each pipeline style.
- Primary operation: Running each stage one by one.
- How many times: Once per stage, either in a loop (scripted) or defined stages (declarative).
As the number of stages increases, the work Jenkins does grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 stage executions |
| 100 | 100 stage executions |
| 1000 | 1000 stage executions |
Pattern observation: The number of operations grows directly with the number of stages.
Time Complexity: O(n)
This means the time to run the pipeline grows linearly with the number of stages, regardless of scripted or declarative style.
[X] Wrong: "Declarative pipelines are always faster because they are simpler."
[OK] Correct: Both styles run each stage once, so time depends on stage count, not style simplicity.
Understanding how pipeline style affects execution helps you explain trade-offs clearly and shows you grasp how Jenkins works under the hood.
"What if we added parallel stages in the declarative pipeline? How would the time complexity change?"
Practice
Solution
Step 1: Understand scripted pipeline nature
Scripted pipelines use Groovy code, allowing more flexibility but requiring more coding skill.Step 2: Understand declarative pipeline nature
Declarative pipelines have a fixed, simpler structure designed for easier writing and reading.Final Answer:
Scripted pipelines use Groovy code and are flexible; declarative pipelines have a fixed structure and are easier to read. -> Option AQuick Check:
Scripted = flexible Groovy, Declarative = fixed structure [OK]
- Confusing which pipeline uses Groovy code
- Thinking both pipelines have the same syntax
- Believing scripted pipelines are only for Java
Solution
Step 1: Identify declarative pipeline syntax
Declarative pipelines start with thepipeline {}block and includeagentandstages.Step 2: Compare options
pipeline { agent any; stages { stage('Build') { steps { echo 'Building...' } } } } correctly usespipeline { agent any; stages { stage('Build') { steps { echo 'Building...' } } } }which matches declarative syntax.Final Answer:
pipeline { agent any; stages { stage('Build') { steps { echo 'Building...' } } } } -> Option DQuick Check:
Declarative starts with pipeline block [OK]
- Using scripted syntax for declarative pipelines
- Omitting the 'pipeline' block
- Missing 'stages' or 'steps' keywords
node {
stage('Test') {
echo 'Running tests'
}
}Solution
Step 1: Analyze scripted pipeline structure
The scripted pipeline usesnodeandstageblocks correctly with anechostep inside.Step 2: Determine output
The pipeline will run the 'Test' stage and print 'Running tests' to the console.Final Answer:
Prints 'Running tests' during the 'Test' stage -> Option AQuick Check:
Scripted pipelines allow stage and echo inside node [OK]
- Thinking 'stage' is only for declarative pipelines
- Expecting a syntax error without 'pipeline' block
- Assuming no output if node block is present
pipeline {
agent any
stages {
stage('Build') {
echo 'Building...'
}
}
}Solution
Step 1: Review declarative pipeline stage structure
Each stage must contain astepsblock that holds the commands to run.Step 2: Check the snippet
The 'Build' stage directly containsechowithout wrapping it insidesteps {}, which is invalid.Final Answer:
Missing 'steps' block inside the 'Build' stage -> Option CQuick Check:
Declarative stages require steps block [OK]
- Omitting the 'steps' block inside stages
- Confusing scripted and declarative syntax
- Thinking 'echo' must be replaced by 'sh'
Solution
Step 1: Understand pipeline flexibility needs
Complex dynamic branching requires Groovy scripting flexibility, which scripted pipelines provide.Step 2: Combine declarative simplicity with scripted flexibility
Declarative pipelines allow embedding scripted blocks for advanced logic, keeping most stages simple and readable.Final Answer:
Use a declarative pipeline with a scripted block inside for complex parts. -> Option BQuick Check:
Declarative + scripted block = best of both worlds [OK]
- Using only scripted pipeline losing readability
- Avoiding scripted blocks in declarative pipelines
- Running separate pipelines manually instead of combining
Ready to prove it?
Pass the test and this pattern moves unknown
