0
0
Jenkinsdevops~10 mins

Conditional deployment logic in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Conditional deployment logic
Start Pipeline
Check Condition
Deploy
End Pipeline
The pipeline starts, checks a condition, then either deploys if true or skips deployment if false, then ends.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Deploy') {
      when { branch 'main' }
      steps { echo 'Deploying...' }
    }
  }
}
This Jenkins pipeline deploys only if the branch is 'main'.
Process Table
StepCondition CheckedCondition ResultAction TakenOutput
1Is branch 'main'?TrueExecute deploy stageDeploying...
2Pipeline ends-FinishPipeline completed successfully
💡 Pipeline ends after deployment stage completes because condition was true.
Status Tracker
VariableStartAfter Step 1After Step 2
branchmainmainmain
deploy_stage_executedfalsetruetrue
Key Moments - 2 Insights
Why does the deploy stage run only when the branch is 'main'?
Because the 'when' condition checks the branch name. In the execution table row 1, the condition is true only if branch equals 'main', triggering deployment.
What happens if the branch is not 'main'?
The deploy stage is skipped. The condition in row 1 would be false, so no deploy action occurs and pipeline ends without deployment.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 1?
ASkipping deployment
BPipeline failed
CDeploying...
DNo output
💡 Hint
Check the 'Output' column in execution_table row 1.
At which step does the pipeline finish?
AStep 1
BStep 2
CBefore Step 1
DAfter Step 3
💡 Hint
Look at the 'Action Taken' column for pipeline completion.
If the branch was 'feature', what would happen to 'deploy_stage_executed' variable?
AIt would remain false
BIt would be undefined
CIt would be true
DIt would cause an error
💡 Hint
Refer to variable_tracker and key_moments about condition false case.
Concept Snapshot
Jenkins conditional deployment uses 'when' to check conditions.
If condition true, deploy stage runs.
If false, stage is skipped.
Common use: deploy only on 'main' branch.
Helps avoid unwanted deployments.
Full Transcript
This Jenkins pipeline example shows conditional deployment logic. The pipeline starts and checks if the current branch is 'main'. If yes, it runs the deploy stage and prints 'Deploying...'. If not, it skips deployment. The execution table traces these steps clearly. Variables track the branch name and whether deployment ran. Key moments clarify why deployment depends on the branch and what happens if the condition is false. The visual quiz tests understanding of outputs and variable states. The snapshot summarizes how Jenkins uses 'when' conditions to control deployment stages.