CI/CD and automation · Beginner · 17 min
Block merges until checks pass with GitHub rulesets
By Padding · Updated 18 Nov 2026
GitHub (web) · GitHub CLI 2.106.0 · November 2026
You will have a GitHub ruleset on main that only accepts reviewed pull requests whose named checks have passed, with linear history, no force pushes, protected v* release tags and an optional merge queue, all kept as JSON files you apply with gh api.
On this page
A test workflow only protects main if a failing run stops the merge. Without a rule, the red cross on a pull request is advice: anyone with write access can click Merge anyway, push straight to main, or force-push over yesterday's history. A ruleset turns that advice into a gate that GitHub enforces for everyone, including you.
In this tutorial you write one ruleset for main and one for release tags, as JSON files you keep in the repository and apply with gh api. The branch ruleset requires a pull request, one approving review, named status checks that have passed, a linear history and no force pushes or deletion. You try it out on a throwaway branch before it touches main, and you learn the one mistake that leaves pull requests waiting forever: requiring a check from a workflow that does not always run. At the end you add a merge queue, for repositories where many pull requests land each day.
Rulesets are newer than classic branch protection rules and do the same job with more control. Several rulesets can apply to the same branch, and GitHub combines them, using the strictest version of any rule they share. If your repository already has a classic branch protection rule on main, it keeps working alongside the ruleset; once the ruleset covers everything, you can delete the old rule.
Plan availability (checked 26 November 2026): rulesets work in public repositories on every plan, including GitHub Free. For private repositories you need GitHub Pro, GitHub Team or GitHub Enterprise Cloud. The Evaluate mode and the merge queue have narrower limits, covered in the steps that use them.
Step 1 · Find the exact names of your checks
A required status check is matched by name, letter for letter. Guessing the name is the most common reason a ruleset blocks every pull request, so read the names off a real run first.
Open any recent pull request in your repository, or create one, and list its checks from the repository folder:
gh pr checks <number>Replace <number> with the pull request number. Each line looks something like ✓ test (24) 1m2s https://...: a status mark, then the check's name, then how long it took and a link. Copy the name column. For a GitHub Actions job, the name is the job's name: if it has one, otherwise its key under jobs:. A job with a matrix reports one check per leg, named after the job and its matrix values, so the test job from the Node tests tutorial shows up as something like test (24) and test (26).
Write down the checks that must pass before anything merges. Leave out checks that only report information, such as a coverage comment, and checks from workflows that do not run on every pull request (Step 3 explains why).
Check it worked
You have a short list of check names copied from the gh pr checks output, not typed from memory, and each one appears on every recent pull request you look at.
Step 2 · Write the branch ruleset as JSON
Keep rulesets in the repository so a change to the rules is reviewed like any other change. Create a folder for them and write the branch ruleset. Replace the two context values with the check names from Step 1:
mkdir -p .github/rulesets
cat > .github/rulesets/main.json <<'JSON'
{
"name": "main: reviewed and green",
"target": "branch",
"enforcement": "active",
"bypass_actors": [],
"conditions": {
"ref_name": {
"include": ["refs/heads/ruleset-trial"],
"exclude": []
}
},
"rules": [
{ "type": "deletion" },
{ "type": "non_fast_forward" },
{ "type": "required_linear_history" },
{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 1,
"dismiss_stale_reviews_on_push": true,
"require_code_owner_review": false,
"require_last_push_approval": true,
"required_review_thread_resolution": false,
"allowed_merge_methods": ["squash", "rebase"]
}
},
{
"type": "required_status_checks",
"parameters": {
"strict_required_status_checks_policy": true,
"do_not_enforce_on_create": false,
"required_status_checks": [
{ "context": "test (24)" },
{ "context": "test (26)" }
]
}
}
]
}
JSONThe ruleset targets a branch called ruleset-trial for now, not main. You switch it over in Step 5, once you have seen it work. Here is what each part does:
enforcement: activemeans the rules are enforced as soon as the ruleset exists. The other values aredisabledandevaluate(Step 4).bypass_actors: []means nobody can skip the rules, not even repository administrators. Adding yourself here is tempting; it also means your own mistakes skip review.deletionstops anyone deleting the branch.non_fast_forwardblocks force pushes, so published history cannot be rewritten.required_linear_historyrefuses merge commits. Pull requests land by squash or rebase, which keepsgit logonmaina straight line and makesgit bisecteasier. Your repository settings must allow at least one of those merge methods, andallowed_merge_methodslimits the Merge button to the same two.pull_requestmeans every change reaches the branch through a pull request.required_approving_review_count: 1needs one approval from someone with write access.dismiss_stale_reviews_on_pushwithdraws that approval when new commits change the diff, andrequire_last_push_approvalmeans the approval must come from someone other than the last person to push, so nobody can sneak in a change after review.required_status_checkslists the checks that must pass.strict_required_status_checks_policy: truealso requires the pull request branch to be up to date with the target branch, so the checks ran against the code that will actually exist after the merge.
If you work on the repository alone, set required_approving_review_count to 0 and require_last_push_approval to false. GitHub does not let you approve your own pull request, so a required review with no second person blocks every merge. The checks, history and force-push rules still protect you.
Check it worked
Run python3 -m json.tool .github/rulesets/main.json. It prints the file back with no error, and the context values match your list from Step 1 exactly, including spaces and brackets.
Step 3 · Fix the path-filter trap before you require a check
A required check has to report a result. If its workflow never starts, GitHub has nothing to compare and the pull request waits, showing that it is waiting for a status to be reported. It never times out.
The usual cause is a trigger filter. This workflow skips documentation-only changes:
on:
pull_request:
paths:
- 'src/**'
- 'package.json'A pull request that only edits README.md does not start the workflow, so test (24) and test (26) never report, and that pull request can never merge. The same happens with branches: filters and with commit messages containing [skip ci].
You have two safe choices:
- Remove the
paths:filter from any workflow whose jobs are required. The tests run on every pull request, which costs some minutes but never blocks. - Keep the workflow starting on every pull request, and skip the expensive steps inside it with a job-level
if:condition. A job skipped by its ownif:reports as passed, so the required check is satisfied. The tutorial on path filters and concurrency builds this pattern step by step.
There is a related trap with needs:. If a required job lists another job under needs: and that job fails, the required job is skipped, and GitHub reports a skipped job as Success, so the check can look green without the tests having run. Make sure each required check is a job that runs the real test, or one that uses if: always() and fails when the jobs it needs failed.
Check it worked
For each check on your list, open the workflow file that produces it. Its on.pull_request trigger has no paths, paths-ignore or branches filter that could leave a pull request into main without a run, and the required job does not sit behind a needs: on a job that could fail.
Step 4 · Try the ruleset before it guards main
A wrong ruleset on main can stop the whole team merging, so prove it on something that does not matter first.
If your organisation uses GitHub Enterprise Cloud, you can set "enforcement": "evaluate" and point the ruleset straight at main. Evaluate mode applies no restrictions; it records what would have been blocked. You read the results on the Rule Insights page (Settings, then Rules, then Insights) or with gh api repos/{owner}/{repo}/rulesets/rule-suites. Evaluate is only available with GitHub Enterprise, so on Free, Pro and Team use a trial branch instead, as below. Team plans still get the Rule Insights page for active rulesets.
Create the trial branch from main and push it:
git switch main
git pull
git switch -c ruleset-trial
git push -u origin ruleset-trialNow create the ruleset. gh api fills in {owner} and {repo} from the repository in your current folder:
gh api --method POST repos/{owner}/{repo}/rulesets --input .github/rulesets/main.jsonThe reply is the ruleset as GitHub stored it, including its numeric id. Then try to break the rules. Make an empty commit on the trial branch and push it directly:
git commit --allow-empty -m "Try a direct push"
git push origin ruleset-trialThe push is refused with a message saying the changes must be made through a pull request, and the commit stays only on your computer. Undo it with git reset --soft HEAD~1, which removes that empty commit and nothing else. Avoid git reset --hard here: it would also throw away any uncommitted edits, such as the workflow change from Step 3 if you have not committed it yet.
Finally, open a pull request into ruleset-trial from a small feature branch. The merge box lists the review and each required check as outstanding until the checks finish and someone approves. Your workflow must also run for pull requests into ruleset-trial: if its trigger says branches: [main], add ruleset-trial to that list for the trial, or use no branch filter at all, as in the Node tests tutorial.
Check it worked
gh api repos/{owner}/{repo}/rulesets --jq '.[] | "\(.id) \(.name) \(.enforcement)"' lists your ruleset as active, the direct push was rejected, and the test pull request into ruleset-trial shows the merge as blocked until its checks pass and it is approved. If the box still waits for a status, first confirm the workflow actually ran on the trial pull request (its branches: filter must include ruleset-trial). If it ran and passed, the check names do not match; go back to Step 1.
Step 5 · Point the ruleset at main
Change the target in .github/rulesets/main.json. Replace "refs/heads/ruleset-trial" with "~DEFAULT_BRANCH", the special value for the repository's default branch, so the ruleset follows main even if the default branch is renamed later:
"include": ["~DEFAULT_BRANCH"],Update the existing ruleset rather than creating a second one. Replace <ruleset-id> with the id from the listing in Step 4:
gh api --method PUT repos/{owner}/{repo}/rulesets/<ruleset-id> --input .github/rulesets/main.jsonThen tidy up the trial: close the test pull request and delete the branch. GitHub refuses to delete ruleset-trial while the ruleset protected it, but now that the ruleset targets only main, it can go:
git switch main
git branch -D ruleset-trial
git push origin --delete ruleset-trialCommit .github/rulesets/main.json through a pull request, which is now the only way in. Your colleague's approval of that pull request is the first review the new rule enforces.
On slow or prepaid connections, reviewers can approve from the terminal with gh pr review <number> --approve, which uses far less data than loading the pull request page.
Check it worked
Open any pull request into main. The merge box lists the required review and your named checks, the Merge button only offers squash and rebase, and it stays unavailable until every listed check is green and one other person has approved.
Step 6 · Protect release tags with a tag ruleset
Release tags such as v1.4.0 are what deploy workflows and package users rely on. A tag that is quietly moved to another commit, or deleted and recreated, means two people can download "v1.4.0" and get different code. A tag ruleset stops that.
cat > .github/rulesets/release-tags.json <<'JSON'
{
"name": "release tags: never move or delete",
"target": "tag",
"enforcement": "active",
"bypass_actors": [],
"conditions": {
"ref_name": {
"include": ["refs/tags/v*"],
"exclude": []
}
},
"rules": [
{ "type": "deletion" },
{ "type": "non_fast_forward" },
{ "type": "update", "parameters": { "update_allows_fetch_and_merge": false } }
]
}
JSON
gh api --method POST repos/{owner}/{repo}/rulesets --input .github/rulesets/release-tags.jsonrefs/tags/v* matches every tag starting with v. Patterns use fnmatch rules, where * does not match a slash, which is fine for flat tag names. deletion blocks deleting a matching tag, and update with non_fast_forward blocks pointing it at a different commit. Anyone with write access can still create a new tag.
To limit who creates release tags as well, add { "type": "creation" } to the rules. That rule blocks everyone not on the bypass list, so add your release managers or the repository admin role to the bypass list first. The web form for the ruleset (Settings, then Rules, then Rulesets) lets you pick them from a list, which is easier than looking up role IDs for the JSON.
Check it worked
Push a test tag, then try to move it. A warning first: the test tag starts with v, so any workflow with on: push: tags: ['v*'] (a release or deploy workflow) starts when you push it. Check .github/workflows/ for such a trigger, and if one exists, cancel that run straight away in the Actions tab or run this check in a repository without one.
git tag v0.0.0-ruleset-check
git push origin v0.0.0-ruleset-check
git tag -f v0.0.0-ruleset-check HEAD~1
git push --force origin v0.0.0-ruleset-checkThe first push succeeds and the forced push is rejected with an error that points to a repository rule violation.
Now clean up. The tag is protected, so switch the ruleset off, delete the tag on GitHub and on your computer, then switch the ruleset back on. Find the tag ruleset's id with the listing command from Step 4, and in .github/rulesets/release-tags.json change "enforcement": "active" to "enforcement": "disabled":
gh api --method PUT repos/{owner}/{repo}/rulesets/<tag-ruleset-id> --input .github/rulesets/release-tags.json
git push origin --delete v0.0.0-ruleset-check
git tag -d v0.0.0-ruleset-checkChange the file back to "enforcement": "active" and apply it again:
gh api --method PUT repos/{owner}/{repo}/rulesets/<tag-ruleset-id> --input .github/rulesets/release-tags.jsonThe listing from Step 4 shows the tag ruleset as active again, and git ls-remote --tags origin v0.0.0-ruleset-check prints nothing.
Step 7 · Add a merge queue for a busy repository
Strict status checks have a cost. When five pull requests are approved at once, the first one merges and the other four are now behind main, so each author updates their branch, waits for the checks again, and races the others. A merge queue takes over that work. Approved pull requests join a queue, GitHub tests each one combined with main and the pull requests ahead of it, and merges them in order once their checks pass.
Merge queues are available in public repositories owned by an organisation, and in private repositories owned by organisations on GitHub Enterprise Cloud (checked 26 November 2026). Personal repositories cannot use one.
First, make your required workflows run for the queue. The queue reports its builds through the merge_group event, and a workflow that does not listen for it never reports, so every queued pull request fails. Add the trigger beside pull_request:
on:
pull_request:
merge_group:Merge that change before turning on the queue. Then add this rule to the rules array in .github/rulesets/main.json:
{
"type": "merge_queue",
"parameters": {
"merge_method": "SQUASH",
"grouping_strategy": "ALLGREEN",
"max_entries_to_build": 5,
"min_entries_to_merge": 1,
"max_entries_to_merge": 5,
"min_entries_to_merge_wait_minutes": 5,
"check_response_timeout_minutes": 60
}
}merge_method: SQUASHkeeps the linear history rule happy.grouping_strategy: ALLGREENmerges a group only when every pull request in it passes its checks.HEADGREENonly needs the last entry's combined build to pass, which is faster and less strict.max_entries_to_buildcaps how many queue builds run at once, which also caps your Actions minutes.min_entries_to_merge,max_entries_to_mergeandmin_entries_to_merge_wait_minutescontrol batching: with these values a lone pull request waits at most five minutes for company, and no more than five merge together.check_response_timeout_minutesis how long the queue waits for checks before treating them as failed. Set it above your slowest test run.
Because the queue always tests against the latest main, you can set strict_required_status_checks_policy to false in the same file, so authors no longer have to keep updating their branches by hand. Apply the file with the same gh api --method PUT command as in Step 5.
Check it worked
On an approved pull request with passing checks, the green button now reads Merge when ready, and gh pr merge <number> adds it to the queue instead of merging. The pull request's timeline shows it joining the queue, a check run triggered by merge_group appears, and the pull request merges once that run passes.
Next steps
Your rules now live in .github/rulesets/, so copying them to the next repository is one gh api call per file. Some places to go from here:
- Make the required review worth having, in review code well as an author and a reviewer.
- Revisit the test workflow behind your required checks, in run Node.js tests in GitHub Actions.
Related tutorials
- How to Push to GitHub over SSH and Open a Pull RequestYou will connect a local repository to a new GitHub repository over SSH, push your work, push a branch, open a pull request from the website and from the GitHub CLI, merge it, and bring the result back to your computer.
- How to Run Node.js Tests Automatically with GitHub ActionsYou will write tests with Node.js's built-in test runner, run them on every push and pull request with a GitHub Actions workflow that caches npm downloads and tries two Node.js versions, and check the workflow file locally before you push it.
- Review code well, as the author and as the reviewerYou will have prepared a small pull request for review as its author, then reviewed it as a colleague would, ending with a set of labelled comments and a clear approve or request-changes decision you can reuse on real work.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials