Sub topic 13
Applying rulesets across an organization
This chapter assumes you can create a repository ruleset, set its enforcement status, and use a bypass list. It also assumes you know how ref name patterns work. Here you apply one ruleset to many repositories at once.
Why use an organization ruleset
A repository ruleset covers one repository. With 40 repositories you would repeat the same setup 40 times, and the copies would drift apart. An organization ruleset is defined once and targets repositories as well as refs. Organization owners manage it under the organization's Settings, in the Rulesets page. Repository admins can see it but cannot edit or delete it.
Which plans can use organization rulesets depends on your GitHub plan, so check your plan before you plan a rollout.
Choosing which repositories to target
An organization ruleset has two targeting layers. The first picks repositories. The second is the ref_name condition you already know, which picks branches or tags inside those repositories. You can pick repositories in these ways:
| Method | Effect |
|---|---|
| All repositories | Includes every repository, including ones created later |
| Name patterns | Includes repositories by name, with optional excludes |
| Custom properties | Includes repositories that have a given property value, such as `tier=production` |
| Selected repositories | Includes a fixed list |
Name patterns and properties are dynamic. A new repository that matches is covered the moment it is created. Properties usually scale better than names, because a repository's name rarely says how critical it is.
Creating one with the API
This body targets every repository except those named sandbox-*. It starts in evaluate mode so you can see what it would block.
{
"name": "org-protect-default-branch",
"target": "branch",
"enforcement": "evaluate",
"bypass_actors": [],
"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": false,
"require_last_push_approval": false,
"required_review_thread_resolution": true
}
}
]
}gh api --method POST orgs/ORG/rulesets --input org-ruleset.json
gh api orgs/ORG/rulesetsThe first command creates the ruleset and the second lists the organization's rulesets. The endpoint is /orgs/{org}/rulesets, not the repos path used earlier.
How it combines with repository rules
Organization rulesets layer with repository rulesets and classic branch protection. The most restrictive result applies, as described in the layering chapter. A repository cannot weaken an organization rule. It can only add stricter ones. To see the combined result for one branch, run gh api repos/OWNER/REPO/rules/branches/main.
Exercise
Your organization has 60 repositories. Only those tagged with the custom property tier=production must require one approving review on the default branch. New production repositories should be covered automatically. How do you target them, and what do you set first?
Show answer
Create an organization ruleset that targets repositories by the tier custom property with the value production, with a ref_name include of ~DEFAULT_BRANCH and a pull_request rule requiring one approval. Because the property is dynamic, new repositories with that value are covered automatically. Set enforcement to evaluate first, check the insights, then switch to active.