Sub topic 1
What repository rulesets are and why they exist
Repository rulesets let you define rules for branches, tags and pushes in one place and apply them to many repositories. This chapter explains the problem they solve and the parts every ruleset has. Later chapters cover each part in detail.
Prerequisites
This chapter assumes you already know:
- Basic Git branching and pull requests.
- GitHub organisations, teams and repository roles.
- What a GitHub Actions workflow and a status check are.
- How to read JSON.
The problem with per-repository settings
Classic branch protection rules are configured one repository at a time. In an organisation with 200 repositories, that means 200 sets of settings. Over time they drift. One repository requires two reviews, another requires none, and a new repository may have no protection at all until someone remembers to add it.
Classic rules also have limits:
- They apply only to branches. Tags need a separate, older mechanism.
- Each branch name pattern has its own rule, and only one rule applies to a matching branch.
- You cannot test a rule without turning it on.
- Seeing which rules apply to a branch means opening each rule and reading it.
Rulesets address these gaps. They are a newer GitHub feature and can be defined for a single repository or for an entire organisation.
The four parts of a ruleset
Every ruleset is made of the same four parts.
| Part | What it answers | Example |
|---|---|---|
| Target | Which kind of ref does it govern? | Branches, tags, or pushes |
| Conditions | Which refs and, in an organisation, which repositories? | The default branch of every repository |
| Rules | What is required or blocked? | One approving review, no force pushes |
| Bypass list | Who may skip the rules, and how? | Repository admins, through a pull request |
A ruleset also has an enforcement status: active, evaluate or disabled. That setting controls whether the rules are applied, only logged, or switched off.
A first look at a ruleset
The JSON below is a small organisation ruleset. It is trimmed from the full example later in this tutorial, and you can read each part in it.
{
"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" }
]
}Reading it from the top:
targetisbranch, so the ruleset governs branches.conditionsselect the default branch (~DEFAULT_BRANCH) in all repositories (~ALL) except those whose names matchsandbox-*.rulesblock deleting the branch and block non-fast-forward updates, which are force pushes.bypass_actorslets the repository admin role skip the rules, but only by opening a pull request.enforcementisactive, so the rules are applied now.
What rulesets give you over classic rules
- Central control. One organisation ruleset replaces many identical per-repository settings.
- Layering. Several rulesets can apply to the same branch at once. GitHub combines them, and the most restrictive result wins. A later chapter covers this.
- Safe testing. The evaluate status records what would have been blocked without blocking anyone.
- Visibility. Anyone with read access to a repository can see which rulesets apply to it. Rule insights show the history of allowed and blocked actions.
- Controlled bypass. Bypass is granted to named roles, teams or apps, and can be limited to pull requests.
- API access. A ruleset is a JSON document, so you can store it, review it and create it with the REST API.
Where this tutorial goes next
The next chapters build on the four parts. You will choose a scope, pick refs and repositories, add pull request and status check rules, and then require a shared workflow to pass before merging. Later chapters cover bypass, enforcement, overlap, push rulesets, the API and rollout.
Exercise
An organisation has 40 repositories. You want the default branch in each one to reject force pushes, except in repositories named sandbox-*. Which parts of a ruleset do you set, and which part do you leave out?
Show answer
Set the target to branch, and set conditions with refname including ~DEFAULTBRANCH and repositoryname including ~ALL and excluding sandbox-*. Add the nonfastforward rule, which blocks force pushes. Create it as an organisation ruleset, because repositoryname conditions exist only there. You can leave out the bypass list if nobody needs to skip the rule. Set enforcement to evaluate first if you want to check the effect before blocking anyone.
Exercise
A push to a default branch is rejected, and the repository has no classic branch protection rule. Where else should you look for the cause?
Show answer
Look at the rulesets that apply to the repository, both repository-level and organisation-level ones. Several rulesets can apply at once, and the most restrictive combination applies.