0
0
Jenkinsdevops~30 mins

When to use scripted over declarative in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Run the script and check the console output matches the expected lines.