Sub topic 5
Requiring Status Checks Before Merging
This chapter assumes you know Git branches and pull requests, and that you have a CI workflow that reports a result on each pull request. It also assumes admin access to a repository where you can experiment.
What a status check is
A status check is a pass or fail result that a tool reports against a specific commit. GitHub Actions jobs report check runs. Other CI systems report commit statuses. A required status check is one that must succeed before a pull request can merge into a protected branch.
Setting it up in a classic rule
The classic branch protection endpoint takes a required_status_checks object. This body requires a check named build:
{
"required_status_checks": {
"strict": true,
"checks": [{ "context": "build" }]
},
"enforce_admins": true,
"required_pull_request_reviews": {
"required_approving_review_count": 1,
"dismiss_stale_reviews": true,
"require_code_owner_reviews": true
},
"restrictions": null,
"allow_force_pushes": false,
"allow_deletions": false
}Send it with the gh CLI:
gh api --method PUT repos/OWNER/REPO/branches/main/protection --input protection.jsonTwo settings matter most here:
contextis the exact name of the check as it appears on a pull request. For GitHub Actions, this is the job name, not the workflow name.strict: trueturns on "require branches to be up to date before merging". The pull request branch must contain the latest commit of the target branch, so the check result reflects what the merged code will look like.
Strict mode and its cost
Without strict mode, a pull request can pass its checks against an old base and still break the target branch after merging. With strict mode, every merge to the target branch makes other open pull requests out of date. Each must be updated and re-tested.
On busy repositories this creates a queue of manual updates. A merge queue, covered in a later chapter, automates that work.
The same rule in a ruleset
In a ruleset, the equivalent is a required_status_checks rule:
{
"type": "required_status_checks",
"parameters": {
"strict_required_status_checks_policy": true,
"required_status_checks": [{ "context": "build" }]
}
}The setting names differ, but the behavior matches the classic rule.
Common errors
- Check never reports. If a workflow is skipped, for example by a path filter, the required check stays "Expected" and the pull request cannot merge. Keep required workflows running on every pull request, or use a job that always reports.
- Name mismatch. A misspelled or renamed job never matches the required context, so the pull request waits forever.
- Matrix jobs. Each matrix combination reports under its own name. Require the exact names, or add one final job that depends on the others and require only that.
- Check has never run. GitHub lets you pick only checks that ran in the repository recently. Run the workflow once before adding it.
Exercise
A pull request changes only a README. Your CI workflow has a path filter that skips it for README changes, and build is a required check. Why is the pull request stuck, and how do you fix it?
Show answer
The workflow never runs, so build never reports and stays "Expected". Remove the path filter from the required workflow, or add an always-running job that reports a success result, and require that job instead.
Exercise
What does strict: true change, and what is its main cost?
Show answer
It requires the pull request branch to contain the latest target branch commit before merging, so checks test the real merged result. The cost is that each merge makes other open pull requests out of date, forcing updates and reruns.