0
0
JenkinsHow-ToBeginner · 3 min read

How to Use When Condition in Jenkins Pipeline

In Jenkins Pipeline, use the when block inside a stage to run that stage only if a specified condition is true. The when block supports conditions like branch name, environment variables, or custom expressions to control pipeline flow.
📐

Syntax

The when block is placed inside a stage to define conditions for running that stage. It supports multiple condition types like branch, environment, expression, and more.

Basic structure:

stage('Stage Name') {
  when {
    conditionType 'value'
  }
  steps {
    // steps to run if condition is true
  }
}

Explanation:

  • stage('Stage Name'): Defines a pipeline stage.
  • when: Starts the condition block.
  • conditionType 'value': Condition to check (e.g., branch 'main').
  • steps: Commands to run if condition passes.
groovy
stage('Example Stage') {
  when {
    branch 'main'
  }
  steps {
    echo 'This runs only on main branch'
  }
}
💻

Example

This example shows a Jenkins Declarative Pipeline with two stages. The second stage runs only if the branch is main.

groovy
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
    stage('Deploy') {
      when {
        branch 'main'
      }
      steps {
        echo 'Deploying to production'
      }
    }
  }
}
Output
[Pipeline] stage [Pipeline] { (Build) [Pipeline] echo Building... [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Deploy) [Pipeline] echo Deploying to production [Pipeline] } [Pipeline] // stage
⚠️

Common Pitfalls

Common mistakes when using when conditions include:

  • Using when outside of a stage block, which is invalid.
  • Incorrect syntax like missing quotes or wrong condition names.
  • Expecting when to control steps directly instead of entire stages.
  • Not handling multiple conditions properly (use allOf or anyOf for combining).

Example of wrong and right usage:

groovy
stage('Wrong Usage') {
  steps {
    when {
      branch 'main'
    }
    echo 'This will fail'
  }
}

stage('Right Usage') {
  when {
    branch 'main'
  }
  steps {
    echo 'This runs only on main branch'
  }
}
📊

Quick Reference

Common when condition types:

Condition TypeDescriptionExample
branchRuns stage on specific branchwhen { branch 'main' }
environmentRuns stage if env var matcheswhen { environment name: 'DEPLOY', value: 'true' }
expressionRuns stage if Groovy expression is truewhen { expression { return params.RUN_STAGE } }
notNegates a conditionwhen { not { branch 'develop' } }
allOfAll conditions must be truewhen { allOf { branch 'main'; environment name: 'DEPLOY', value: 'true' } }
anyOfAny condition is truewhen { anyOf { branch 'main'; branch 'release' } }

Key Takeaways

Use the when block inside a stage to control if it runs based on conditions.
Common conditions include branch, environment variables, and custom expressions.
The when block applies to entire stages, not individual steps.
Combine multiple conditions with allOf or anyOf for complex logic.
Always place when inside a stage block to avoid syntax errors.