Skip to content
Latchkey

How to Run a Step Only on the Main Branch in GitHub Actions

Check github.ref == 'refs/heads/main' in a step if to scope it to the default branch.

The github.ref context holds the full ref (e.g. refs/heads/main). Compare it in a step-level if to run only on main.

Steps

  • Add if: github.ref == 'refs/heads/main' to the step.
  • Use the full ref form, not the short branch name.
  • Keep test steps unconditional so pull requests still run them.

Workflow

.github/workflows/ci.yml
steps:
  - run: npm test
  - name: Publish (main only)
    if: github.ref == 'refs/heads/main'
    run: npm publish

Gotchas

  • On pull_request events github.ref is the merge ref, so prefer github.head_ref there.
  • Use github.ref_name if you want just main instead of the full ref.

Related guides

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