When to Use Scripted Over Declarative in Jenkins Pipelines
📖 Scenario: You are a DevOps engineer working on automating software builds and deployments using Jenkins. Jenkins supports two main ways to write pipelines: scripted and declarative. Understanding when to use scripted pipelines instead of declarative ones helps you handle complex automation tasks effectively.
🎯 Goal: Learn to create a simple Jenkins scripted pipeline that includes a conditional step, showing when scripted pipelines are preferred over declarative pipelines.
📋 What You'll Learn
Create a Jenkins scripted pipeline with a map of build stages
Add a configuration variable to control which stages run
Use a for loop to iterate over stages and run selected ones
Print the names of the stages that are executed
💡 Why This Matters
🌍 Real World
Jenkins pipelines automate software build, test, and deployment tasks. Scripted pipelines are used when automation needs complex logic beyond simple linear steps.
💼 Career
DevOps engineers and automation specialists use scripted pipelines to create flexible CI/CD workflows that adapt to changing project requirements.
Progress0 / 4 steps
1
Create a map of build stages
Create a variable called buildStages as a map with these exact entries: 'Build': 'Compile code', 'Test': 'Run unit tests', 'Deploy': 'Deploy to staging'
Jenkins
Hint
Use Groovy map syntax with keys and values inside square brackets.
2
Add a configuration variable to select stages
Create a variable called stagesToRun as a list containing the exact strings 'Build' and 'Test'
Jenkins
Hint
Use Groovy list syntax with square brackets and comma-separated strings.
3
Use a for loop to run selected stages
Write a for loop with variable stageName to iterate over buildStages.keySet(). Inside the loop, use an if statement to check if stageName is in stagesToRun. If yes, print "Running stage: " + stageName
Jenkins
Hint
Use Groovy for loop and contains() method on list.
4
Print the executed stages
Add a print statement that outputs exactly "Running stage: Build" and "Running stage: Test" when the script runs
Jenkins
Hint
Run the script and check the console output matches the expected lines.
Practice
(1/5)
1. Which situation is best suited for using a scripted Jenkins pipeline instead of a declarative one?
easy
A. When you want to use built-in syntax for parallel stages only
B. When you want a simple, easy-to-read pipeline with fixed stages
C. When you need complex loops and conditional logic that declarative can't handle
D. When you want to avoid writing any Groovy code
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 C
Quick Check:
Complex logic = scripted [OK]
Hint: Choose scripted for complex logic, declarative for simple flow [OK]
Common Mistakes:
Thinking declarative supports all Groovy features
Using scripted for simple pipelines unnecessarily
Confusing parallel stage support as exclusive to scripted
2. Which of the following is the correct way to start a scripted pipeline in Jenkins?
A. stage keyword cannot be used in scripted pipelines
B. Incorrect use of echo command inside node
C. node block must be inside stage blocks
D. Missing braces {} around the 'Test' stage block
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 D
Quick Check:
All stage blocks need braces in scripted [OK]
Hint: Always use braces {} for stage blocks in scripted pipelines [OK]
Common Mistakes:
Omitting braces for single commands in stages
Thinking stage keyword is invalid in scripted
Misplacing node and stage blocks
5. You want to create a Jenkins pipeline that dynamically creates stages based on a list of environments (dev, test, prod). Which pipeline type should you use and why?
hard
A. Declarative pipeline, because it supports dynamic stage creation easily
B. Scripted pipeline, because it allows loops and dynamic stage generation with Groovy
C. Declarative pipeline, because scripted pipelines cannot handle multiple stages
D. Neither, Jenkins does not support dynamic stages
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 B
Quick Check:
Dynamic stages = scripted pipeline [OK]
Hint: Use scripted pipelines for dynamic or complex stage creation [OK]