Skip to content
Latchkey

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 in if if 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.sh

Gotchas

  • Once any step fails, later steps are skipped unless they use if: always() or failure().
  • String comparisons are case-sensitive; normalize values first.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →