How to Run a Step Only if a Condition Is True in GitHub Actions
A step-level if: evaluates an expression and runs the step only when the result is truthy.
Attach if: to the step with a boolean expression. GitHub evaluates it, and any non-empty, non-false result runs the step. The ${{ }} wrapper is optional inside if.
Steps
- Add
if:to the step you want to guard. - Write a boolean expression referencing
github,env, orstepscontexts. - Leave other steps unconditional so they always run.
Workflow
.github/workflows/ci.yml
steps:
- id: check
run: echo "deploy=true" >> "$GITHUB_OUTPUT"
- name: Deploy
if: steps.check.outputs.deploy == 'true'
run: ./deploy.shGotchas
- Do not wrap the whole
ifin extra quotes unless it starts with!, or YAML may mis-parse it. - A false condition skips the step; it does not fail it.
Related guides
How to Run a Job Only if a Condition Is True in GitHub ActionsGate an entire GitHub Actions job with a job-level if so every step is skipped when the condition is false, u…
How to Combine Conditions With && and || in an if in GitHub ActionsCombine multiple GitHub Actions if conditions with && and ||, and preserve status behavior by leading with al…