Sub topic 8
Blocking merges when tests fail
This chapter assumes you can write a workflow that runs tests on pull_request and that you have admin access to a GitHub repository. A failing workflow only warns people. To stop a broken change from reaching main, you must tell GitHub that the check is required.
From status check to merge gate
Each job in a workflow reports a result on the commit as a status check. A check is advisory by default. Authors can still click "Merge" while it shows a red cross.
A branch protection rule, or a ruleset, turns selected checks into requirements. GitHub then disables the merge button until those checks pass on the pull request's latest commit.
Prepare the workflow
The workflow must run on pull_request. Otherwise the check never appears on the pull request and the merge waits forever.
name: Tests
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testThe name of the required check is the job name, here test. If you set name: on the job, that value is used instead. Keep job names stable, because renaming a job makes the old required check unreachable.
Create the rule
- Open the repository and go to Settings, then Branches.
- Under branch protection rules, choose Add branch protection rule.
- Enter
mainas the branch name pattern. - Enable Require status checks to pass before merging.
- Enable Require branches to be up to date before merging. This makes the tests run against the combined result of your branch and the current
main. - In the search box, select the
testcheck. - Save the rule.
Repositories that use rulesets (Settings, then Rules) have an equivalent option named "Require status checks to pass".
Search only finds checks that have run
The check list only offers names that have run in the repository within the last week. Run the workflow at least once on a pull request first. If you cannot find the check, this is the usual reason.
Enforce the rule for everyone
By default, administrators can bypass the rule. Enable Do not allow bypassing the above settings (or Include administrators) if you want it to apply to them too.
A required check that never reports blocks the pull request. The status stays "Expected - Waiting for status to be reported".
The most common cause is a path filter on the workflow:
on:
pull_request:
paths:
- "src/**"If a pull request changes only the README, the workflow does not run, so the required check never reports. Either remove the filter, or add a job that always runs and make that job the required check.
Matrix jobs and required checks
A matrix creates one check per combination, named like test (ubuntu-latest, 20). Each name must be required separately. If you change the matrix versions later, update the rule too.
A simpler approach is a final job that depends on the matrix and is the only required check:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
all-tests-passed:
needs: test
if: ${{ always() }}
runs-on: ubuntu-latest
steps:
- run: |
if [ "${{ needs.test.result }}" != "success" ]; then
echo "Tests did not succeed: ${{ needs.test.result }}"
exit 1
fiThe always() condition makes the gate job run even when test fails. Without it, the gate job is skipped, and GitHub treats a skipped required check as passing.
Exercise
A pull request changes only docs/guide.md. Your workflow has paths: ["src/**"] under pull_request, and test is a required check. What happens, and how do you fix it?
Show answer
The workflow never runs, so test never reports and the merge stays blocked at "Expected - Waiting for status to be reported". Fix it by removing the paths filter, or by making a job that always runs the required check.