Scripted vs Declarative Jenkins Pipeline Comparison
📖 Scenario: You are working as a DevOps engineer. Your team wants to understand the difference between scripted and declarative Jenkins pipelines. You will create two simple Jenkins pipelines: one scripted and one declarative, both doing the same task of printing a message.
🎯 Goal: Build two Jenkins pipelines: a scripted pipeline and a declarative pipeline. Both pipelines will print the message "Hello from Jenkins Pipeline!" to the console. This will help you see how the two styles differ in syntax and structure.
📋 What You'll Learn
Create a scripted Jenkins pipeline that prints "Hello from Jenkins Pipeline!"
Create a declarative Jenkins pipeline that prints "Hello from Jenkins Pipeline!"
Use the exact variable and stage names as instructed
Print the output in the final step
💡 Why This Matters
🌍 Real World
Jenkins pipelines automate software build, test, and deployment tasks. Understanding scripted and declarative pipelines helps teams choose the best style for their projects.
💼 Career
DevOps engineers often write Jenkins pipelines. Knowing both scripted and declarative styles improves flexibility and collaboration in CI/CD workflows.
Progress0 / 4 steps
1
Create a scripted Jenkins pipeline
Write a scripted Jenkins pipeline using node block that prints the message "Hello from Jenkins Pipeline!" using echo.
Jenkins
Hint
Use node { ... } to define the scripted pipeline and echo to print the message.
2
Add a declarative Jenkins pipeline
Add a declarative Jenkins pipeline using pipeline and stages that has one stage named PrintMessage which prints "Hello from Jenkins Pipeline!" using echo.
Jenkins
Hint
Use pipeline { agent any stages { stage('PrintMessage') { steps { echo ... } } } } structure for declarative pipeline.
3
Compare the two pipelines in one script
Combine both the scripted pipeline and the declarative pipeline in one Jenkinsfile script. Keep the scripted pipeline first, then the declarative pipeline. Use exact code from previous steps.
Jenkins
Hint
Just place the scripted pipeline code first, then the declarative pipeline code below it.
4
Print a summary message
Add a final echo statement outside both pipelines that prints "Scripted and Declarative pipelines completed."
Jenkins
Hint
Use echo "Scripted and Declarative pipelines completed." outside the pipeline blocks.
Practice
(1/5)
1. Which statement best describes the main difference between scripted and declarative Jenkins pipelines?
easy
A. Scripted pipelines use Groovy code and are flexible; declarative pipelines have a fixed structure and are easier to read.
B. Declarative pipelines use Groovy code and are flexible; scripted pipelines have a fixed structure and are easier to read.
C. Both scripted and declarative pipelines have the same syntax and flexibility.
D. Scripted pipelines are only for building Java projects; declarative pipelines are for all other projects.
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 A
Each stage must contain a steps block that holds the commands to run.
Step 2: Check the snippet
The 'Build' stage directly contains echo without wrapping it inside steps {}, which is invalid.
Final Answer:
Missing 'steps' block inside the 'Build' stage -> Option C
Quick Check:
Declarative stages require steps block [OK]
Hint: Declarative stages always need a 'steps' block [OK]
Common Mistakes:
Omitting the 'steps' block inside stages
Confusing scripted and declarative syntax
Thinking 'echo' must be replaced by 'sh'
5. You want to create a Jenkins pipeline that runs a complex Groovy script for dynamic branching but also wants to keep some stages simple and readable. Which approach is best?
hard
A. Use a fully scripted pipeline for all stages to keep consistency.
B. Use a declarative pipeline with a scripted block inside for complex parts.
C. Use only declarative pipeline and avoid any Groovy scripting.
D. Write two separate pipelines and run them manually.