Use jobs.<job_id>.steps[*].if
to require a condition be met before a step is run.
You can use any supported context and expression to create a conditional.
When you use expressions in an if
conditional, you may omit the ${{ }}
expression syntax because CloudBees platform automatically evaluates the if
conditional as an expression.
However, this rule does not apply everywhere.
You must use the ${{ }}
expression syntax or escape with ''
, ""
, or ()
when the expression starts with !
, since !
is reserved notation in YAML format.
Using the ${{ }}
expression syntax turns the contents into a string, and strings are truthy.
For example, if: true && ${{ false }}
will evaluate to true
.
Example usage: Contexts
A conditional at the job level cannot reference any step outputs. However, a conditional within a step can reference previous steps in the same job.
In the following example, the step only runs when the event type is a push
and the branch is main
.
steps: - name: My first step uses: docker://alpine:3.18 if: ${{ cloudbees.event_name == 'push' && cloudbees.scm.ref == 'refs/heads/main' }} run: echo This event is a push on the main branch.
In the following example, the first step is referenced in the second step.
Example usage: Status checks
In the following example, my backup step
only runs when the previous step of a job fails.
steps: - name: My first step uses: cloudbees/checkout@v1 - name: My backup step if: ${{ failure() }} uses: cloudbees/kaniko@v1
Example usage: Using secrets
You cannot directly reference a secret in jobs.<job_id>.steps[*].if
.
CloudBees recommends setting secrets as environment variables at the job level, then referencing these environment variables in the conditional, as in the following example:
If a secret is not set, the return value of an expression referencing the secret (such as ${{ secrets.my_secret }}
is an empty string.