Sub topic 11
Blocking merges with required status checks
This chapter assumes you have a working lint and format workflow, such as the one from the earlier chapters. It also assumes you can change repository settings on GitHub. A workflow that fails does not stop anyone from merging by default. GitHub shows a red mark, but the merge button still works. A required status check turns that mark into a gate.
What a required check is
A status check is a result that GitHub attaches to a commit. Each job in a workflow reports one. A required status check is a check that must pass before a pull request can merge into a protected branch.
Two settings work together:
- A branch protection rule or ruleset defines which branch is protected, usually
main. - The "Require status checks to pass before merging" option lists the check names that must succeed.
Names matter
GitHub matches required checks by name. For a GitHub Actions job, the check name is the job's name. It is the job key (lint) unless you set a name: field on the job.
With the parallel jobs from the previous chapter, you get two separate checks:
jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run lint
format:
name: Format
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run format:checkSetting name: explicitly is useful. If you later rename the job key, the check name stays the same, so your protection rule keeps working.
Setting up the rule
- Run the workflow at least once. GitHub only offers check names it has seen recently.
- Open the repository, then go to Settings, then Branches (or Rules for rulesets).
- Add a rule for
main. - Enable "Require status checks to pass before merging".
- Search for
LintandFormatand add both. - Optionally enable "Require branches to be up to date before merging". This makes GitHub re-run checks against the latest
main, so a pull request cannot pass on stale code. - Save the rule.
Common errors
The most frequent problem is a check that stays "Expected, waiting for status" forever. It has three usual causes.
- The workflow does not run for pull requests. If the workflow only triggers on
push, no check ever reports on the pull request. Keeppull_requestinon:. - The name does not match. A renamed job or workflow produces a new check. The old required name never reports.
- Path filters skip the workflow. If the workflow uses
paths:and a pull request touches none of them, the workflow is skipped and the required check never reports. Either remove the filter or add a lightweight always-running job.
Also check who the rule applies to. Repository admins can bypass protection unless you enable "Do not allow bypassing the above settings" (or the equivalent in rulesets).
Why this pairs well with check-only commands
Required checks only work if a failure is real and repeatable. Because lint and format:check never modify files, the same commit always gives the same result. That makes the gate predictable for contributors.
Exercise
A pull request shows "Expected, waiting for status" for the required check Lint, and the workflow never ran. Your workflow starts with on: push: branches: [main]. What is wrong, and how do you fix it?
Show answer
The workflow does not listen for pull_request events, so no check is reported on the pull request branch. Add pull_request: under on: (as in on: { pull_request: , push: { branches: [main] } }) and push a new commit. The Lint check will then report and can pass or fail.