Sub topic 9
Required Status Checks and Branch Protection
This chapter assumes you know how a CI workflow reports a status to a pull request, and that you can read a GitHub Actions workflow file. It covers how to make a check mandatory and how to avoid blocking merges by accident.
What "required" means
A status check is a named result (success, failure, pending) that CI attaches to a commit. A check is required when branch protection (or a repository ruleset) says a pull request cannot merge into a protected branch until that named check succeeds.
Requiring a check turns it from advice into a gate. That is why placement matters. A required check on every pull request must be fast, stable, and relevant to every change. A slow or flaky required check slows down the whole team.
Choosing what to require
Use the placement decisions from earlier chapters as a guide:
- Require the fast, deterministic checks that run on pull requests, such as lint and unit tests.
- Require the merge-time checks through the merge queue, so integration tests gate the candidate commit and not each pull request.
- Do not require nightly checks. They run on a schedule, not on a pull request, so no pull request can ever report them.
Matching names to workflows
Branch protection matches checks by name. For GitHub Actions, the name is the job name (or workflow / job in some views). If you rename a job, the old required name never reports again, and the pull request waits forever on a check that does not exist.
Give jobs stable, explicit names:
name: pr
on:
pull_request:
branches: [main]
jobs:
fast-checks:
name: fast-checks
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
- run: npm testTreat the job name as a public contract. Change it only together with the protection settings.
Required checks and merge queues
When you enable a merge queue, GitHub builds a temporary candidate commit and runs the workflows that listen to the merge_group event. The required checks must report on that event too. If the integration job is required but its workflow has no merge_group trigger, the queue never gets a result and the entry times out.
The merge workflow from the previous chapters already has the trigger:
name: merge
on:
merge_group:
push:
branches: [main]
jobs:
integration:
name: integration
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 test:integrationCommon ways to block merges by accident
- Path filters on required checks. If a workflow uses
paths:and a pull request touches none of those paths, the workflow never starts. The required check stays pending, and the pull request cannot merge. Fix this by running the workflow always and skipping work inside it, or by adding a small always-running job that you require instead. - Skipped jobs. A job skipped through a job-level
if:reports as passed. A whole workflow skipped by trigger filters reports nothing. These two behave differently. - Renamed jobs. See above. The old name stays required.
- Missing triggers. A check required on
mainthat has nopull_requestormerge_grouptrigger can never pass. - Flaky required checks. People start rerunning or bypassing. Quarantine the unstable test instead of weakening the rule.
Other protection settings worth knowing
- Require branches to be up to date forces a re-run against the latest
main. A merge queue gives you the same guarantee without manual rebasing. - Required reviews and dismiss stale approvals are separate from CI but interact with it.
- Restrict who can push so that nothing reaches
mainwithout going through the gates.
Exercise
Your repository requires a check named docs-lint. The workflow that produces it has paths: ['docs/**']. A pull request that only changes src/app.js cannot merge. Why, and what are two ways to fix it?
Show answer
The workflow never starts because no changed file matches docs/**, so docs-lint never reports and stays pending. One fix is to remove the paths: filter and skip the expensive steps inside the job, for example with a changed-files check. Another is to require a small always-running job that depends on the filtered work and reports success when the work was not needed.
Exercise
Why should the nightly end-to-end workflow not be a required status check?
Show answer
It runs on a schedule on the default branch, not on a pull request or merge queue candidate, so it never reports a status to a pull request. Requiring it would block every merge indefinitely.