Sub topic 2
Repository rulesets and organisation rulesets
This chapter assumes you know Git branching and pull requests, and that you understand GitHub organisations, teams and repository roles. It also assumes you can read JSON. Here you will learn where a ruleset lives, who can change it, and how that decides what it can do. Later chapters cover targeting, individual rules and layering in detail.
One ruleset format, three places to create it
A ruleset is a named set of rules with a target, an enforcement status and an optional list of bypass actors. The format is the same wherever the ruleset is created. What changes is the level that owns it:
- A repository ruleset belongs to one repository.
- An organisation ruleset belongs to an organisation and can apply to many of its repositories.
- On GitHub Enterprise Cloud, an enterprise ruleset belongs to an enterprise and can apply across several organisations. This tutorial focuses on the first two.
The owning level decides three things:
- Who can create and edit the ruleset.
- Which repositories the ruleset can reach.
- Which rule types are available.
Plan availability also matters. Rulesets work on public repositories on every plan. Organisation rulesets and rulesets on private repositories need a paid plan. Check your plan before you design a rollout.
Where each kind lives
| Aspect | Repository ruleset | Organisation ruleset |
|---|---|---|
| Created under | Repository **Settings**, then **Rules**, then **Rulesets** | Organisation **Settings**, then **Repository**, then **Rules**, then **Rulesets** |
| Who can edit | Repository admins, or a custom role with the permission to edit repository rules | Organisation owners, or a custom organisation role with the permission to edit repository rules |
| Repositories reached | The one repository | All repositories, or a selection by name pattern or custom property |
| Extra conditions | Ref name only | Ref name plus repository selection |
| Required workflows rule | Not available | Available |
| REST API path | `/repos/{owner}/{repo}/rulesets` | `/orgs/{org}/rulesets` |
| Visible to repository users | Yes | Yes, read-only |
The last row is easy to miss. A developer who opens the Rules page of a repository sees the organisation rulesets that apply to it. They can read each rule and see why a push was rejected, but they cannot change it. A repository admin cannot loosen an organisation ruleset, and that is the main reason to use one.
How the levels relate
A repository does not pick one level. Every active ruleset that targets a ref applies to it, whichever level it comes from. If the repository has its own ruleset, the organisation rulesets still apply next to it.
The combination is additive. Each ruleset contributes rules, and when two rulesets set the same kind of rule with different values, the stricter value applies. For example, if one ruleset asks for one approving review and another asks for two, the pull request needs two. Repository rulesets can add requirements on top of the organisation baseline but cannot remove them. The chapter on overlapping rulesets covers the full behaviour.
When to use a repository ruleset
A repository ruleset fits when the requirement belongs to one team or one project. Typical cases:
- A single repository needs a stricter review count than the organisation baseline.
- A release repository protects tags that match
v*so nobody can move or delete them. - A team without organisation-level access wants to evaluate a rule before proposing it to the whole organisation.
Repository rulesets can be exported and imported as JSON. A team can prototype a ruleset in one repository and hand the file to an organisation owner. Keep in mind that a repository ruleset has no repository selector, because it already belongs to one repository.
When to use an organisation ruleset
An organisation ruleset fits when the requirement is a policy, not a team preference. Typical cases:
- Every default branch must block force pushes and deletion.
- Every production repository must pass a shared security workflow before merging.
- Nobody may push files such as
*.pemor*.p12to any repository.
The ruleset in this tutorial's main example is of this type. It targets ~DEFAULT_BRANCH in every repository except those named sandbox-*:
{
"name": "protect-default-branch",
"target": "branch",
"enforcement": "active",
"bypass_actors": [
{
"actor_id": 5,
"actor_type": "RepositoryRole",
"bypass_mode": "pull_request"
}
],
"conditions": {
"ref_name": {
"include": ["~DEFAULT_BRANCH"],
"exclude": []
},
"repository_name": {
"include": ["~ALL"],
"exclude": ["sandbox-*"]
}
},
"rules": [
{ "type": "deletion" },
{ "type": "non_fast_forward" },
{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 1,
"dismiss_stale_reviews_on_push": true,
"require_code_owner_review": true,
"require_last_push_approval": false,
"required_review_thread_resolution": true
}
}
]
}The repository_name condition in this JSON is what makes the ruleset organisation-wide. A repository ruleset cannot contain it.
The workflows rule type is the other capability that only organisation rulesets have. It lets you name a workflow file in one repository and require it to pass in every targeted repository. A repository admin cannot remove that requirement. Required workflows get their own chapter.
Seeing both levels from the command line
Both levels use the same JSON shape, so the same gh api skills work for each. To list the rulesets that apply at the organisation level:
gh api /orgs/YOUR_ORG/rulesets \
--jq '.[] | {id, name, enforcement, source_type}'To list the rulesets that affect one repository, including those inherited from the organisation:
gh api "/repos/YOUR_ORG/YOUR_REPO/rulesets?includes_parents=true" \
--jq '.[] | {id, name, enforcement, source_type, source}'The source_type field reports Repository, Organization or Enterprise. Use it to tell which level a ruleset came from when a push is blocked and you do not recognise the rule. The includes_parents parameter defaults to true, so the repository endpoint returns inherited rulesets unless you turn it off.
To create a repository-level ruleset, post to the repository path. This example blocks force pushes on main and uses a file named block-force-push.json:
{
"name": "block-force-push-main",
"target": "branch",
"enforcement": "active",
"conditions": {
"ref_name": {
"include": ["refs/heads/main"],
"exclude": []
}
},
"rules": [
{ "type": "non_fast_forward" }
]
}gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/repos/YOUR_ORG/YOUR_REPO/rulesets \
--input block-force-push.jsonCompare this body with the organisation example. The only structural difference is that the repository_name condition is gone. The endpoint path decides the level.
Common mistakes
- Expecting a repository admin to override an organisation ruleset. They cannot edit it. Only organisation owners, or users with the custom role permission, can change it. Repository admins can still be listed as bypass actors, but that is an explicit grant.
- Putting a
repository_namecondition in a repository ruleset. The API rejects conditions that the owning level does not support. Remove the condition, or create the ruleset at the organisation level. - Trying to use the
workflowsrule in a repository ruleset. Create the rule in an organisation ruleset instead. - Debugging the wrong level. When a push is blocked, check
source_typefirst. The blocking rule may come from the organisation and not from the repository settings you are looking at. - Forgetting that old branch protection still applies. A branch can be governed by a ruleset and a classic protection rule at once. Review both when you audit a branch.
Choosing the level
Use a short decision rule. If a rule should hold even when a repository admin disagrees, create it at the organisation level. If it expresses one team's workflow and the team should be able to change it, create it at the repository level. You can use both, since the organisation ruleset sets a floor and repository rulesets can only raise it.
Exercise
Your organisation requires one approving review on every default branch through an organisation ruleset. A team asks for two approving reviews on the default branch of its own repository only. Which level should the team's ruleset live at, and what will the effective review requirement be?
Show answer
The team should create a repository ruleset in its own repository that requires two approving reviews. Both rulesets apply to the branch, and when the same rule has different values the stricter one wins, so the effective requirement is two approving reviews. The organisation ruleset still applies, and the team cannot lower it.
Exercise
A developer sees a push rejected with a message about a required workflow. They open the repository's Settings and find no ruleset that mentions it. Where is the rule coming from, and how can they confirm it with the gh CLI?
Show answer
The rule comes from an organisation ruleset, because the workflows rule type exists only at that level. Organisation rulesets appear read-only on the repository's Rules page. From the command line, run gh api "/repos/YOURORG/YOURREPO/rulesets?includesparents=true" and check the sourcetype field for entries marked Organization.