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 themain branch:
2. Environment Variable Check
Execute the stage ifDEPLOY 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.