Sub topic 4
Choosing which repositories a ruleset applies to
An organisation ruleset is only useful if it reaches the right repositories. If it reaches too few, protection has gaps. If it reaches too many, you block teams that should not be affected. This chapter covers the four ways to select repositories, how include and exclude lists interact, and what to check before you widen a ruleset's scope.
It assumes you know how repository and organisation rulesets differ, and that you can read the ruleset JSON from earlier chapters. You also need permission to administer rulesets in an organisation.
Where repository targeting fits
A ruleset has two kinds of target. Ref targeting answers "which branches or tags?" with the ref_name condition. Repository targeting answers "which repositories in the organisation?" and applies only to organisation rulesets. A repository-level ruleset already belongs to one repository, so it has nothing to select.
A change is checked by a rule only if both conditions match. A repository must be in scope, and the ref being pushed or merged into must match ref_name.
The four selection methods
The organisation ruleset editor offers four ways to choose repositories. The REST API exposes them as three condition keys, and the "all repositories" option is a special value of the name condition.
| Method | API condition | Best for |
|---|---|---|
| All repositories | `repository_name` with `~ALL` | Baseline rules for the whole organisation |
| Name patterns | `repository_name` with patterns | Naming conventions such as `svc-*` |
| Repository properties | `repository_property` | Rules tied to a classification such as `environment=production` |
| Fixed list | `repository_id` | A small, stable set of named repositories |
All repositories and name patterns
repository_name takes an include list and an exclude list. The value ~ALL matches every repository. Other entries are patterns that use fnmatch syntax. * matches any run of characters, ? matches one character, and [abc] matches one character from a set.
The first code example in the spine uses this condition:
"repository_name": {
"include": ["~ALL"],
"exclude": ["sandbox-*"]
}A repository is in scope when it matches at least one include entry and no exclude entry. Exclusion always wins. A repository named sandbox-payments matches ~ALL and sandbox-*, so it is out of scope.
Name patterns are dynamic. A repository created tomorrow as svc-billing is covered by an svc-* ruleset from the moment it exists. You do not need to edit the ruleset. A repository renamed so that it no longer matches drops out of scope, which is a common source of silent gaps.
The repository_name condition also has an optional boolean, protected. When it is true, GitHub prevents targeted repositories from being renamed. This closes the rename gap, but it also stops owners from renaming repositories that the ruleset covers.
Repository properties
Custom properties are key-value metadata that organisation owners define once and set on each repository. They let you target by meaning instead of by name. A repository named ledger can carry environment=production, and a ruleset can say "apply to everything in production" with no naming convention.
First define the property in the organisation. This example creates a single-select property:
gh api \
--method PUT \
-H "Accept: application/vnd.github+json" \
/orgs/YOUR_ORG/properties/schema/environment \
-f value_type=single_select \
-f required=false \
-f 'allowed_values[]=production' \
-f 'allowed_values[]=staging' \
-f 'allowed_values[]=sandbox'Then set the value on repositories. This request sets it on two repositories at once:
gh api \
--method PATCH \
-H "Accept: application/vnd.github+json" \
/orgs/YOUR_ORG/properties/values \
--input - <<'EOF'
{
"repository_names": ["ledger", "checkout"],
"properties": [
{ "property_name": "environment", "value": "production" }
]
}
EOFThe ruleset condition then selects on that value:
"repository_property": {
"include": [
{
"name": "environment",
"property_values": ["production"],
"source": "custom"
}
],
"exclude": []
}property_values is a list, so one entry can match several values, such as ["production", "staging"]. Exclude entries use the same shape.
Properties have an operational advantage. Changing a repository's classification changes which rules apply to it, and that change goes through whoever can edit properties. Decide who that is. In a large organisation, the ability to set a property can become the ability to opt a repository out of a ruleset. Restrict it with the organisation's custom property permissions.
A fixed list of repositories
repository_id takes explicit numeric IDs:
"repository_id": {
"repository_ids": [123456, 234567]
}This option is precise and predictable. New repositories are never included by accident. The cost is that nothing is automatic. You must update the ruleset when the set changes. Because it uses IDs, it is not affected by renames.
To find an ID:
gh api /repos/YOUR_ORG/ledger --jq .idYou need the same kind of ID for the workflows rule, which identifies the repository that stores the shared workflow file.
Choosing between methods
Use one targeting method per ruleset. The editor presents the methods as alternatives, and a ruleset that mixes them is harder to reason about. If you need "all production repositories except legacy-*", prefer a property include plus a separate rule of thumb for exceptions, such as setting a different property value on those repositories. This keeps the rule for scope in one place.
A practical selection guide:
- Start with
~ALLplus a small exclude list for baseline rules such as blocking deletion and force pushes. - Use properties for rules that depend on risk, such as stricter review counts for production.
- Use name patterns when your naming convention is enforced and reliable.
- Use a fixed list for pilots and for the first stage of a rollout.
Targeting for required workflows
The workflows rule from the spine's third example ties repository targeting to a shared workflow. Two repositories are involved, and it is easy to mix them up.
- The targeted repositories are the ones selected by your repository conditions. The workflow runs against changes in these.
- The source repository is the one named by
repository_idinside the rule. It stores.github/workflows/org-ci.yml.
Check three things before you activate such a ruleset:
- The source repository must be accessible to the targeted repositories. For private or internal repositories, adjust the Actions access setting in the source repository so that other repositories in the organisation can use its workflows.
- The workflow must be triggered by the events that matter for the targeted refs. A required workflow that does not run for pull requests will never report a result, and merges will wait for it.
- Broad scope amplifies mistakes. A broken workflow on
~ALLblocks every repository at once. Start with a fixed list or a property such asenvironment=sandbox, then widen.
Edge cases and common errors
- A new repository has no effect from the ruleset. Check whether the targeting is a fixed list. New repositories are only picked up by name patterns,
~ALL, or properties. - A repository is covered by nothing after a rename. Name-pattern scope is evaluated against the current name. Use the
protectedoption or switch to properties or IDs. - A property-based ruleset skips repositories. A repository with no value for the property does not match an include on it. Mark the property as required, with a default, so every repository carries a value.
- An exclude seems ignored. Patterns are case-sensitive in how you write them and anchored to the whole name.
sandboxdoes not matchsandbox-api. Usesandbox*orsandbox-*. - The ruleset appears in the list but nothing is blocked. Check the enforcement status. A ruleset set to evaluate records results without blocking.
- Plan limits. Organisation rulesets apply to private repositories only on paid plans. On the free plan they cover public repositories.
Review
Repository targeting is an include and exclude filter over the organisation. Pick the method whose maintenance cost matches how the set of repositories changes: names for conventions, properties for classification, IDs for fixed sets, and ~ALL for baselines. Exclusions always win, and both repository and ref must match before a rule is evaluated.
Exercise
Your organisation names every service repository svc-<team>-<name>, and a ruleset includes svc-* and excludes *-docs. Which of these repositories are in scope: svc-payments-api, svc-payments-docs, svc-ledger, payments-svc-api? Then state one risk of relying on this pattern and one way to remove it.
Show answer
In scope: svc-payments-api and svc-ledger. svc-payments-docs matches the include but also the exclude *-docs, and exclusion wins. payments-svc-api does not start with svc-, so it does not match the include. The risk is that renaming a repository changes its scope without anyone editing the ruleset. You can remove it by setting protected to true on the repository_name condition so targeted repositories cannot be renamed, or by switching to a custom property such as kind=service, which does not depend on the name.
Exercise
You are rolling out a required workflow to an organisation with 300 repositories. Which targeting would you use for the first stage, and how would you widen it?
Show answer
Start with a small, controlled scope: a fixed repository_id list of a few pilot repositories, or a repository_property include such as environment=sandbox. Pair it with the evaluate enforcement status so failures are recorded without blocking. After the workflow proves stable, widen to a broader property value such as staging, and finally to ~ALL with a short exclude list, switching enforcement to active once the insights show no unexpected failures.