Sub topic 13
Moving from branch protection rules to rulesets
This chapter assumes you know classic branch protection rules, you can read the ruleset JSON from earlier chapters, and you can run gh as an organisation admin.
Why migrate
Classic branch protection and rulesets can both apply to the same branch at once. GitHub evaluates both, and the stricter result wins. That makes a gradual move possible, and it also means a leftover classic rule can block a merge that your new ruleset allows.
Rulesets add things branch protection lacks:
- They cover tags as well as branches.
- They can apply to many repositories from the organisation level.
- They support several layered rulesets instead of one rule per name pattern.
- They have an evaluate mode, so you can test before you enforce.
- Anyone with read access to the repository can see them.
Keep classic rules only where a repository needs something rulesets do not provide. Otherwise, plan to retire them.
Map the settings
There is no automatic converter for existing protection rules. You recreate each setting by hand or in JSON.
| Classic branch protection setting | Ruleset equivalent |
|---|---|
| Require a pull request before merging | `pull_request` rule |
| Require status checks to pass | `required_status_checks` rule |
| Require linear history | `required_linear_history` rule |
| Require signed commits | `required_signatures` rule |
| Allow force pushes: off | `non_fast_forward` rule |
| Allow deletions: off | `deletion` rule |
| Restrict who can push | `update` rule, with bypass actors for the allowed people |
| Do not allow bypassing | An empty `bypass_actors` list |
Classic rules also cannot require a shared workflow. That is the workflows rule from the previous chapters, so it is usually the reason to move.
Migration steps
1. Take an inventory
This script lists the repositories in an organisation whose default branch has a classic protection rule. It needs admin access, because the protection endpoint returns 404 to other users.
ORG=YOUR_ORG
gh repo list "$ORG" --no-archived --limit 500 \
--json name,defaultBranchRef \
--jq '.[] | select(.defaultBranchRef != null) | "\(.name) \(.defaultBranchRef.name)"' |
while read -r repo branch; do
if gh api "repos/$ORG/$repo/branches/$branch/protection" --silent 2>/dev/null; then
echo "$repo: $branch has classic protection"
fi
doneTo see the settings you need to copy, run gh api repos/YOUR_ORG/YOUR_REPO/branches/main/protection on a representative repository. Group repositories that share the same settings. Each group usually becomes one organisation ruleset.
2. Build and test in evaluate mode
Create the ruleset with "enforcement": "evaluate". It records what it would block without blocking anything. Review the rule insights for a week or two. Evaluate mode for organisation rulesets requires GitHub Enterprise.
3. Activate, then remove the old rules
Change enforcement to active. Once the ruleset is enforcing, delete the classic rule so you have one source of truth.
gh api --method DELETE repos/YOUR_ORG/YOUR_REPO/branches/main/protectionCommon errors
- Deleting the classic rule first. The branch is unprotected until the ruleset is active. Always activate first.
- Forgetting that both systems apply. A classic rule requiring two approvals still blocks merges after your ruleset asks for one.
- Copying admin enforcement wrongly. Classic protection may have applied to admins. If your ruleset lists a bypass actor, admins can now skip it. Check
bypass_actorsagainst the old behaviour. - Name patterns that no longer match. Classic rules used patterns such as
release/*. In a ruleset the same pattern goes inref_name.includeand needs the full ref form (refs/heads/release/*) or an alias like~DEFAULT_BRANCH.
Exercise
A repository has a classic rule on main that requires two approving reviews. You add an active ruleset on main that requires one approving review. How many approvals does a pull request need, and what should you do to finish the migration?
Show answer
It needs two approvals, because GitHub applies both and the stricter requirement wins. To finish, change the ruleset to require two approvals if two is the intended policy. Confirm it works in evaluate mode first, then activate it and delete the classic rule.