Stage conditions with when directive
📖 Scenario: You are setting up a Jenkins pipeline for a simple project. You want to control which stages run based on the branch name. This helps avoid running unnecessary steps on branches where they are not needed.
🎯 Goal: Build a Jenkins pipeline script with two stages. The first stage always runs. The second stage runs only when the branch is main. You will use the when directive to add this condition.
📋 What You'll Learn
Create a Jenkins pipeline with two stages named
Build and DeployThe
Build stage should always runThe
Deploy stage should run only when the branch is mainUse the
when directive with branch 'main' conditionPrint simple messages in each stage to show they ran
💡 Why This Matters
🌍 Real World
In real projects, controlling which stages run based on branch names helps save time and resources by skipping unnecessary steps on feature or development branches.
💼 Career
Understanding Jenkins pipeline conditions is essential for DevOps roles to automate builds and deployments efficiently and safely.
Progress0 / 4 steps