Sub topic 11
Push rulesets for paths, file sizes and extensions
This chapter assumes you can read ruleset JSON and know how an organisation ruleset selects repositories, as covered in earlier chapters. You should also know what bypass actors and enforcement statuses do. Here you will use a different ruleset target, push, to control the content of what is pushed, not the branch it lands on.
How push rulesets differ
Branch and tag rulesets decide what may happen to a named ref: deleting it, force pushing to it, or merging into it without review. A push ruleset ignores ref names. It inspects the commits in each push and rejects the push if they contain files that break a rule.
Three differences matter in practice:
- No ref targeting. A push ruleset has no
ref_namecondition. It applies to pushes to any branch or tag in a matching repository. - Fork network coverage. A push ruleset on a repository also applies to every fork of that repository. A branch ruleset does not.
- Pushes are rejected whole. If one file in a push violates a rule, GitHub rejects the entire push. The developer must fix the commits locally and push again.
Push rulesets only look at what a push adds or changes. They do not scan the existing repository, so files that were committed earlier stay in place.
The four push rules
| Rule type | Parameter | What it blocks |
|---|---|---|
| `file_path_restriction` | `restricted_file_paths` | Pushes that change files at matching paths |
| `max_file_path_length` | `max_file_path_length` | Pushes with a file path longer than the limit |
| `file_extension_restriction` | `restricted_file_extensions` | Pushes that add files with listed extensions |
| `max_file_size` | `max_file_size` | Pushes that contain a file larger than the limit, in MB |
Restricting paths
restricted_file_paths takes a list of patterns in fnmatch syntax. Use ** to match nested directories.
.github/workflows/org-ci.ymlmatches exactly that file.config/secrets/**/*matches every file underconfig/secrets, at any depth.*.envmatches files with that name ending in any directory the pattern reaches. Test your patterns before relying on them.
The path rule blocks any change to a matching file, including edits and deletions. It is not limited to new files. That makes it a good fit for files that only a platform team should touch, such as workflow files or a CODEOWNERS file.
Restricting extensions
restricted_file_extensions lists extensions with the leading dot, for example .exe, .dll and .pem. Use it for binary artefacts and key material that should never live in source control.
An extension rule only looks at the file name. A developer can rename tool.exe to tool.bin and the rule will not catch it. Treat it as a guard against accidents, not a security boundary.
Limiting file size and path length
max_file_size is measured in megabytes. GitHub already rejects single files over 100 MB, so useful limits are below that. A limit of 5 or 10 MB keeps large datasets and build outputs out of Git history. max_file_path_length caps the number of characters in a full path. It protects teams that check out on Windows, where long paths cause errors.
Setting up an organisation push ruleset
An organisation push ruleset uses "target": "push". Its conditions object holds only a repository_name entry, because no ref applies. The example below blocks binaries and large files everywhere, and it protects the shared workflow file.
{
"name": "block-binaries-and-protect-ci",
"target": "push",
"enforcement": "active",
"bypass_actors": [
{
"actor_id": 1,
"actor_type": "OrganizationAdmin",
"bypass_mode": "always"
}
],
"conditions": {
"repository_name": {
"include": ["~ALL"],
"exclude": ["sandbox-*"]
}
},
"rules": [
{
"type": "file_extension_restriction",
"parameters": {
"restricted_file_extensions": [".exe", ".dll", ".pem"]
}
},
{
"type": "max_file_size",
"parameters": {
"max_file_size": 10
}
},
{
"type": "max_file_path_length",
"parameters": {
"max_file_path_length": 255
}
},
{
"type": "file_path_restriction",
"parameters": {
"restricted_file_paths": [".github/workflows/org-ci.yml"]
}
}
]
}Save this as block-binaries-and-protect-ci.json and create it with the same endpoint you used for branch rulesets.
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/orgs/YOUR_ORG/rulesets \
--input block-binaries-and-protect-ci.jsonTo change the ruleset later, send a PUT request to /orgs/YOUR_ORG/rulesets/RULESET_ID with the full body. List existing rulesets with gh api /orgs/YOUR_ORG/rulesets to find the ID.
Bypass for push rulesets
Push rulesets accept bypass actors, but the useful mode is always. The pull_request mode lets an actor bypass only through a pull request, and a push ruleset checks pushes, not merges. Keep the bypass list short, because a bypassing actor skips every rule in the ruleset, not just the path rule. If only one team needs to edit workflow files, put the path rule in its own ruleset and give that team the bypass. The other rules then stay in force for everyone.
Layering with other rulesets
Push rulesets combine with each other the same way branch rulesets do. All matching rulesets apply together, and the strictest value wins. If one ruleset sets max_file_size to 10 and another sets it to 5, a 7 MB file is rejected. Restricted path and extension lists are added together, so a later ruleset cannot remove an entry that an earlier one blocks.
Repository rulesets and organisation rulesets also stack. A repository admin cannot loosen an organisation push ruleset. They can only add stricter rules at the repository level.
Rolling out and common errors
Push rules stop developers in the middle of their work, so roll them out in steps:
- Create the ruleset with
"enforcement": "evaluate"if your plan supports it, and review rule insights for would-be violations. - Fix noisy patterns, such as a path rule that catches generated files.
- Switch to
activeand announce the change with the exact rule list.
Common problems:
- A developer cannot push even after deleting the file. The offending file remains in an earlier local commit. They must rewrite those commits, for example with
git rebase -i, orgit commit --amendfor the latest commit. - A path pattern matches nothing. Check the pattern against the repository-relative path, with no leading
/, and use**for nested directories. - A bot cannot push generated files. Add the app as an
Integrationbypass actor in a dedicated ruleset, not in the main one. - A fork is blocked unexpectedly. The push ruleset covers the fork network, so contributors pushing to forks see the same rejections.
- Rules lock out an emergency fix. Always keep an
OrganizationAdminor a platform team in the bypass list.
Exercise
Exercise
Your organisation wants to stop anyone from pushing files larger than 5 MB or any .zip file to every repository except docs-archive. Platform engineers in the team with ID 4821 must be able to push anything. Write the push ruleset conditions, bypass actors and rules as JSON.
Show answer
{ "name": "limit-large-files", "target": "push", "enforcement": "active", "bypassactors": [ { "actorid": 4821, "actortype": "Team", "bypassmode": "always" } ], "conditions": { "repositoryname": { "include": ["~ALL"], "exclude": ["docs-archive"] } }, "rules": [ { "type": "maxfilesize", "parameters": { "maxfilesize": 5 } }, { "type": "fileextensionrestriction", "parameters": { "restrictedfile_extensions": [".zip"] } } ] }
Exercise
A developer pushes three commits. The first adds a 20 MB file that breaks the size rule, and the second deletes that file again. The third is a normal code change. Is the push accepted, and why?
Show answer
No. Push rulesets check every commit in the push, not only the final state of the tree. The first commit still contains the oversized file, so GitHub rejects the whole push. The developer must squash or rewrite the commits so that no commit adds the file, and then push again.