How to Conditionally Run a Step in GitHub Actions
A step-level if expression decides whether that single step executes.
Put an if: on the step. It can reference contexts (github, env, steps) and status functions (success(), failure(), always()).
Steps
- Add
if:to the step with a boolean expression. - Use status functions to run on failure or always.
- Omit the
${{ }}wrapper inifif you prefer; it is optional there.
Workflow
.github/workflows/ci.yml
steps:
- run: npm test
- name: Upload logs on failure
if: failure()
uses: actions/upload-artifact@v4
with:
name: logs
path: logs/
- name: Notify on main only
if: github.ref == 'refs/heads/main'
run: ./notify.shGotchas
- Once any step fails, later steps are skipped unless they use
if: always()orfailure(). - String comparisons are case-sensitive; normalize values first.
Related guides
How to Use continue-on-error in GitHub ActionsLet a GitHub Actions step or job fail without failing the run using continue-on-error, ideal for non-blocking…
How to Run a Job Only on Main in GitHub ActionsRestrict a GitHub Actions job to the main branch with an if condition on github.ref, so deploys and releases…