Sub topic 12
Protecting tags and restricting pushes
This chapter assumes you can create a ruleset and set its enforcement status, as covered earlier. It extends rulesets beyond branches to tags and to file-level push restrictions.
Why tags need protection
A tag marks a fixed point in history, usually a release such as v1.4.0. Release automation, package registries, and deployment pipelines often trust that a tag never moves. Classic branch protection can't help here, because it only covers branches. A ruleset with the target tag can.
A tag ruleset
A tag ruleset uses the same structure as a branch ruleset, with two differences. target is tag, and the ref_name patterns match full tag refs, such as refs/tags/v*.
{
"name": "protect-release-tags",
"target": "tag",
"enforcement": "active",
"bypass_actors": [
{ "actor_id": 5, "actor_type": "RepositoryRole", "bypass_mode": "always" }
],
"conditions": {
"ref_name": {
"include": ["refs/tags/v*"],
"exclude": []
}
},
"rules": [
{ "type": "creation" },
{ "type": "update" },
{ "type": "deletion" }
]
}Create it with gh api --method POST repos/OWNER/REPO/rulesets --input tag-ruleset.json. The bypass actor above is the repository admin role (role ID 5). Everyone else can't create, move, or delete a v* tag.
The three rules do different jobs:
| Rule | What it blocks |
|---|---|
| `creation` | Creating a ref that matches the pattern |
| `update` | Moving the ref to a different commit |
| `deletion` | Deleting the ref |
The creation rule also fits branches. For example, it can reserve a release/* namespace for a small group. Rules such as required_signatures also work on tags.
Push rulesets
A push ruleset ("target": "push") works differently. It inspects the content of each push, not the name of the ref. It has no ref_name condition. It applies to every branch in the repository, and to the repository's fork network.
The available rules restrict:
- file paths, such as
.github/workflows/** - file extensions, such as
.exe - the maximum file size
- the maximum path length
A push that contains a matching change is rejected before it reaches the repository, whichever branch it targets. Teams use this to keep binaries and large files out of history, or to guard sensitive paths.
{
"name": "block-large-and-binary",
"target": "push",
"enforcement": "evaluate",
"bypass_actors": [],
"rules": [
{ "type": "max_file_size", "parameters": { "max_file_size": 10 } },
{ "type": "file_extension_restriction", "parameters": { "restricted_file_extensions": [".exe", ".dll"] } }
]
}Here max_file_size is in megabytes. Start in evaluate mode, as with branch rulesets, and review what would have been blocked before you switch to active.
Checking the result
Tags and branches are queried separately. Run gh api repos/OWNER/REPO/rules/branches/main for a branch. For a tag, use the same pattern with rules/tags/v1.0.0. This shows which rules apply to that ref, whatever ruleset they come from.
Exercise
Your team wants nobody except release managers to move or delete tags like v2.0.0, and wants to reject any push containing a .jar file on any branch. Which ruleset targets do you need, and what does each use to decide what it applies to?
Show answer
Two rulesets. A tag ruleset with update and deletion rules, an include pattern of refs/tags/v*, and the release managers in the bypass list. A push ruleset with a file_extension_restriction rule for .jar. The tag ruleset matches by ref name pattern. The push ruleset has no ref condition and inspects the content of every push.