Sub topic 2
Repository rulesets and organisation rulesets
This chapter assumes you know Git branching and pull requests, GitHub organisations, teams and repository roles, and how to read JSON. You should also know that a GitHub Actions workflow reports a status check on a commit or pull request.
Two places to define a ruleset
A ruleset is a named set of rules, targets, bypass actors and an enforcement status. You can create one in two places:
- A repository ruleset lives in one repository. Anyone with admin access to that repository can manage it.
- An organisation ruleset lives in the organisation. It can apply to many repositories at once. Only organisation owners, or members with a custom role that allows managing organisation rulesets, can manage it.
Both kinds use the same building blocks. The difference is scope: which repositories the ruleset can reach, and who is allowed to change it.
| Property | Repository ruleset | Organisation ruleset |
|---|---|---|
| Defined in | One repository | The organisation |
| Applies to | That repository only | Repositories you select by name pattern, property or all |
| Managed by | Repository admins | Organisation owners |
| Visible in repositories | Yes, editable by admins | Yes, but read-only |
| REST API path | `/repos/OWNER/REPO/rulesets` | `/orgs/ORG/rulesets` |
Repository admins can see an organisation ruleset that affects their repository, but they cannot edit or delete it. This is what makes organisation rulesets useful for enforcement. A team cannot remove a rule that the organisation set.
What an organisation ruleset adds
A repository ruleset only needs to say which refs it targets, because the repository is already known. An organisation ruleset must also say which repositories it covers. It does this with a repository_name condition, as in the example below, and the later chapter on repository targeting covers the other options.
{
"name": "protect-default-branch",
"target": "branch",
"enforcement": "active",
"conditions": {
"ref_name": {
"include": ["~DEFAULT_BRANCH"],
"exclude": []
},
"repository_name": {
"include": ["~ALL"],
"exclude": ["sandbox-*"]
}
},
"rules": [
{ "type": "deletion" },
{ "type": "non_fast_forward" }
]
}The ref_name condition picks branches or tags inside each repository. ~DEFAULT_BRANCH means whatever branch is the default in that repository, so one ruleset works even when some repositories use main and others use master. The repository_name condition picks the repositories. Here, ~ALL includes every repository and sandbox-* removes the sandbox ones.
A repository ruleset has no repository_name condition. If you send it to a repository endpoint, GitHub rejects the request.
How the pieces fit
Evaluation goes from the outside in. GitHub first checks whether the repository matches, then whether the branch or tag matches, and only then applies the rules. Bypass actors are allowed to skip the rules that result.
Choosing between them
Use an organisation ruleset when a rule should be the same everywhere, such as blocking force pushes to default branches. Use a repository ruleset when a rule belongs to one project, such as requiring a specific test job that only that repository has.
You can use both together. The rules from every ruleset that applies are combined, and the strictest result wins. A later chapter covers this layering in detail.
Exercise
A repository admin opens a repository and sees an organisation ruleset that blocks force pushes to the default branch. They want to remove it for their repository. What can they do, and what is the correct way to exempt that repository?
Show answer
They cannot edit or delete it from the repository, because organisation rulesets are read-only there. An organisation owner must change the ruleset, for example by adding the repository name to the repository_name exclude list or by adding a suitable bypass actor.