Sub topic 9
Required Status Checks and Branch Protection
This chapter assumes you can read a GitHub Actions workflow, you know how pull requests and branches work in Git, and you have seen the three triggers from the previous chapter: pull_request, merge_group or push to main, and schedule. Examples use GitHub. GitLab and others have equivalents, noted at the end.
What a required check is
A status check is a result (pending, success, failure) that a CI job reports against a commit. By default, checks are advisory. A pull request with a red check can still be merged.
A required status check is a check that branch protection says must pass before a pull request can merge into a protected branch. Required checks are the enforcement layer. The workflows decide what runs. The branch protection rule decides what blocks.
This separation matters for placement. A check on the pull request stage that is not required only gives advice. A check that is required gates every merge. So choosing the stage and choosing whether to require it are two decisions.
Configuring protection
In GitHub, you can use a branch protection rule or a ruleset. Rulesets are the newer mechanism and can be layered and applied to many repositories. The relevant settings are the same in both.
- Require status checks to pass before merging. You pick check names from a list. Only checks that have run recently in the repository appear in the list.
- Require branches to be up to date before merging. The pull request branch must contain the latest commit of the base branch. This ensures checks ran against the code that will actually land, at the price of more reruns.
- Require a merge queue. The queue builds a candidate commit (the pull request merged onto the current base plus any queued changes ahead of it) and fires
merge_group. - Require pull request reviews. This is separate from checks but combines with them.
- Do not allow bypassing. Administrators can bypass rules unless you include them.
You can also set these through the API, which is useful for keeping settings in version control. This example uses the GitHub CLI to set required checks on main.
gh api -X PUT repos/OWNER/REPO/branches/main/protection \
--input - <<'EOF'
{
"required_status_checks": {
"strict": true,
"contexts": ["fast-checks"]
},
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1
},
"restrictions": null
}
EOFHere strict: true is the "up to date" setting. The contexts entry fast-checks is the job name from the pull request workflow in the earlier chapters. Replace OWNER/REPO with your own repository.
Check names are the contract
Branch protection matches checks by name, not by workflow file. For GitHub Actions, the name is the job name (or the job name: value, if set). If you rename a job, the old required name never reports again, and branch protection waits forever.
A required check that never reports shows as "Expected, waiting for status" and the pull request cannot merge. This is the most common way to block merges by accident. Rules that avoid it:
- Treat job names as a public interface. Change them only together with the protection settings.
- Give matrix jobs predictable names. A matrix produces one check per combination, such as
test (18)andtest (20). If you change the matrix, the names change. - Prefer one stable aggregate job (see below) when the set of underlying jobs changes often.
Required checks need the right trigger
A check can only gate a merge if it runs for the event the merge uses. This links the trigger chapter to this one.
| Required check runs on | Works for PR merge? | Works with merge queue? |
|---|---|---|
| `pull_request` only | Yes | No. The queue never gets a result for its candidate commit, so the queue stalls or times out. |
| `merge_group` only | No. The pull request has no result. | Yes |
| Both `pull_request` and `merge_group` | Yes | Yes |
| `push` to main only | No. It runs after the merge. | No |
| `schedule` only | No | No |
Two consequences follow.
- If you require a check with a merge queue, the workflow that produces it must list
merge_groupas a trigger. Otherwise the queue waits for a result that is never sent. - Checks that run only after merge (push to main) or on a schedule cannot be required. They can detect problems, but they cannot prevent them. This is the core reason to move a check earlier, and the reason to be careful about what you require.
The fast-checks workflow from earlier would need a small change to be required with a queue.
name: pr
on:
pull_request:
branches: [main]
merge_group:
jobs:
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 testNote that branches: [main] under pull_request filters on the base branch of the pull request. The merge_group event takes its own branches filter if you need one.
Skipped jobs and path filters
Path filters at the workflow level create a second trap. If a workflow has paths: under pull_request and the pull request changes no matching files, the workflow does not run at all. A required check from that workflow stays in "Expected" and blocks the merge.
There are two safe patterns.
- Job-level filtering. Keep the workflow trigger broad and decide inside the workflow whether to do the work. A job skipped through a job-level
if:reports as "skipped", and GitHub treats a skipped required check as passing. Workflow-levelpaths:filters are different, because the workflow never starts and nothing is reported. - Aggregate gate job. Require one job that depends on the others and reports a single result. Only that job is in the branch protection list.
name: pr
on:
pull_request:
branches: [main]
merge_group:
jobs:
changes:
runs-on: ubuntu-latest
outputs:
src: ${{ steps.filter.outputs.src }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
src:
- 'src/**'
- 'package*.json'
test:
needs: changes
if: needs.changes.outputs.src == 'true'
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 test
gate:
if: always()
needs: [changes, test]
runs-on: ubuntu-latest
steps:
- name: Fail if any needed job failed or was cancelled
run: |
if [ "${{ contains(needs.*.result, 'failure') }}" = "true" ] || \
[ "${{ contains(needs.*.result, 'cancelled') }}" = "true" ]; then
echo "A required job did not succeed."
exit 1
fiRequire only gate in branch protection. The if: always() is essential. Without it, the gate is skipped when a needed job fails, and a skipped required check counts as passing. That would let a failing pull request merge. Checking for failure and cancelled while accepting skipped is what lets path-filtered jobs be skipped legitimately.
Choosing what to require
Requiring a check is a promise that it is fast enough, stable enough, and relevant enough to block everyone. Use these criteria.
- Fast. Required checks sit on the critical path of every merge. A required check that takes 40 minutes slows every change.
- Deterministic. A flaky required check blocks unrelated work and teaches people to click "re-run" without reading. Quarantine flaky tests before requiring the job that contains them.
- Covers a real risk. The failure should predict a real defect in the change, not an environment problem.
- Owned. Someone must be able to fix the check when it breaks.
Checks that fail these criteria belong elsewhere: non-required on the pull request, or in the merge queue or nightly stages. Nightly failures never block a merge, so they need an owner and triage, covered in a later chapter.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Pull request waits on "Expected" forever | Required job renamed, or workflow did not trigger because of `paths:` or `branches:` | Restore the name or update protection. Move filtering inside the workflow. |
| Merge queue entry times out | Required check lacks the `merge_group` trigger | Add `merge_group:` to the workflow. |
| Failing pull request merged | Gate job was skipped, or the check was not required | Add `if: always()` and verify the required list. |
| Check missing from the picker | It has not run in the repository yet | Run the workflow once, then select it. |
| Admins merge red pull requests | Admin bypass is allowed | Enforce rules for administrators, and use a documented emergency path. |
| Constant reruns after every merge | "Up to date" is required without a queue | Use a merge queue so the queue handles ordering. |
Other systems
GitLab calls the equivalent settings "pipelines must succeed" and merge trains for the queue. Jenkins reports statuses through a source-control plugin, and the hosting platform enforces them. The ideas are the same: the platform matches status names, and a missing status blocks merging.
Exercise
Your team has a required check named unit-tests. A developer renames the job to test-unit in the workflow file. Every pull request now shows "Expected, waiting for status" and cannot merge. Explain why this happens and give two ways to fix it.
Show answer
Branch protection matches checks by name. After the rename, no job reports unit-tests, so the required status never arrives. Fix 1: rename the job back to unit-tests. Fix 2: update the branch protection rule to require test-unit instead, ideally in the same change that renames the job. A more durable fix is to require a stable aggregate gate job whose name never changes, so internal job names can change freely.
Exercise
You use a merge queue and require fast-checks, whose workflow triggers only on pull_request. What happens when a pull request enters the queue, and what is the fix?
Show answer
The queue builds a candidate commit and waits for the required check on it. The workflow does not run for the merge_group event, so no result is ever reported, and the queue entry stalls and then times out. The fix is to add merge_group: to the workflow's on: block, so the same job reports on the candidate commit.