Skip to main content
In Jenkins Declarative Pipelines, the when directive controls whether a stage should be executed. A stage with a when block runs only if at least one of its conditions evaluates to true. You can also combine conditions for more advanced logic.
Using when conditions can help you skip unnecessary stages, save build time, and reduce resource consumption.

Base Pipeline Example

Below is a simple pipeline without any conditional logic. It has three sequential stages that always run:

Overview of Built-in when Conditions

Basic when Examples

1. Branch Selection

Run a stage only on the main branch:

2. Environment Variable Check

Execute the stage if DEPLOY equals pre-prod:

3. Groovy Expression

Use a custom Groovy expression to gate a manual test stage:
Avoid placing expensive computations or external calls in expression blocks—they run during pipeline evaluation and can slow down the build.

4. Pull Request Builds

Run validations only for pull requests:

Filter by Author Email

5. Tag Checks

Any Tag Build

Specific Tag Pattern

Advanced Condition Combinations

Evaluate Before Agent Allocation

Avoid checking out the workspace unless needed by evaluating conditions before the agent is allocated:

Negation

Invert a condition to exclude certain branches:

anyOf

Run the stage if at least one nested condition is true:

allOf

Run the stage only if all nested conditions are met:

Further Reading

Watch Video