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.
pipeline {
agent any
stages {
stage('Deploy') {
when { branch 'main' }
steps { echo 'Deploying...' }
}
}
}| Step | Condition Checked | Condition Result | Action Taken | Output |
|---|---|---|---|---|
| 1 | Is branch 'main'? | True | Execute deploy stage | Deploying... |
| 2 | Pipeline ends | - | Finish | Pipeline completed successfully |
| Variable | Start | After Step 1 | After Step 2 |
|---|---|---|---|
| branch | main | main | main |
| deploy_stage_executed | false | true | true |
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.
when block in a Jenkins pipeline?when in Jenkinswhen block controls whether a stage runs based on conditions.when usagewhen functionality.when controls stage execution = A [OK]when means "only if" for stages [OK]when with environment setupwhen sets agentswhen declares parameterswhen syntax for branch conditionbranch 'main' inside when block.branch 'name' exactly inside when [OK]whenstage('Deploy') {
when {
branch 'main'
}
steps {
echo 'Deploying to production'
}
}when conditionwhen condition fails, the stage is skipped and no steps run.when, stage skips silently [OK]stage('Deploy') {
when {
environment DEPLOY == 'true'
}
steps {
echo 'Deploying application'
}
}when syntax for environment variableenvironment name: 'DEPLOY', value: 'true' or expression blocks, not direct comparison.environment DEPLOY == 'true' is invalid and causes syntax error.environment condition is invalid syntax -> Option Denvironment condition syntax = D [OK]when { environment name: 'VAR', value: 'val' } syntax [OK]whenwhen inside stepswhen block correctly implements this in a declarative Jenkins pipeline?whenallOf to require both conditions true simultaneously.allOf with branch 'release' and environment name: 'DEPLOY', value: 'yes'.anyOf which means either condition, not both.allOf for AND conditions = B [OK]allOf to combine multiple when conditions [OK]anyOf instead of allOfPass the test and this pattern moves unknown