CI deploys every push, so a half-finished branch can reach staging. if: runs a job only when its condition is true:
yaml
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latestgithub.ref is the branch a push ran for: refs/heads/main for main (a pull request's run has its own, refs/pull/<number>/merge). When the condition is false, the job is skipped, - in gh run view. A step takes if: the same way.
An if: also means "and everything before me passed": here, test has to pass too.
Test it from both sides: push to main, then make a branch and push that:
bash
git switch -c check-if
git push -u origin check-ifObjectives
- Main still deploys.
- Branches don't.