0
0
Jenkinsdevops~30 mins

Why scripted pipelines offer flexibility in Jenkins - See It in Action

Choose your learning style9 modes available
Why scripted pipelines offer flexibility
📖 Scenario: You are a DevOps engineer working with Jenkins to automate software builds and deployments. You want to understand why scripted pipelines in Jenkins provide more flexibility compared to declarative pipelines.
🎯 Goal: Build a simple Jenkins scripted pipeline that demonstrates flexibility by using variables, conditional logic, and loops.
📋 What You'll Learn
Create a variable to hold a list of build stages
Add a configuration variable to control whether to run tests
Use a loop to iterate over the stages and run them
Print the stages that were run
💡 Why This Matters
🌍 Real World
In real projects, scripted pipelines help automate complex workflows that need decisions and loops, like running tests only on certain branches or deploying to different environments.
💼 Career
Understanding scripted pipelines is important for DevOps roles because it allows customizing Jenkins automation beyond simple linear steps, making your automation smarter and more adaptable.
Progress0 / 4 steps
1
Create a list of build stages
Create a variable called stages and assign it the list ["Build", "Test", "Deploy"].
Jenkins
Need a hint?

Use square brackets to create a list and assign it to stages.

2
Add a configuration variable to control testing
Create a variable called runTests and set it to true to control whether the Test stage runs.
Jenkins
Need a hint?

Use true (lowercase) to set the boolean variable runTests.

3
Use a loop and conditional to run stages
Use a for loop with variable stage to iterate over stages. Inside the loop, use an if statement to skip the Test stage if runTests is false. Print "Running stage" for each stage run.
Jenkins
Need a hint?

Use continue to skip the Test stage when runTests is false.

4
Print the stages that were run
Add a print statement after the loop to display "Stages completed: " followed by the list of stages that were run. Use a list called completedStages to collect stages inside the loop.
Jenkins
Need a hint?

Use add() to add stages to the list and print it after the loop.