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. A push ruleset limits what content a push may contain. It does not limit which branch receives it.
How push rulesets differ
Branch and tag rulesets act on named refs. A push ruleset has "target": "push" and inspects the commits in each push. It applies to every branch in the repository and in its fork network, so it has no ref_name condition. An organisation push ruleset selects repositories only, with repository_name or repository properties.
A push is rejected if any commit in it breaks a rule. The rules only check new pushes. Files already in the history are not removed.
The four push rules
| Rule type | Parameter | What it blocks |
|---|---|---|
| `file_path_restriction` | `restricted_file_paths` | Changes to files at the listed paths or patterns |
| `max_file_path_length` | `max_file_path_length` | Paths longer than the given number of characters |
| `file_extension_restriction` | `restricted_file_extensions` | Files with the listed extensions |
| `max_file_size` | `max_file_size` | Files larger than the given size in MB (1 to 100) |
Path entries use fnmatch-style patterns, so .github/workflows/* matches files directly in that directory. Extensions are written with the dot, such as .exe.
Example: protect workflows and block binaries
This ruleset covers all repositories except sandbox-*. It blocks edits to workflow files, blocks common binary extensions, and rejects files over 50 MB. Organisation admins can bypass it.
{
"name": "push-content-limits",
"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_path_restriction",
"parameters": {
"restricted_file_paths": [".github/workflows/*"]
}
},
{
"type": "file_extension_restriction",
"parameters": {
"restricted_file_extensions": [".exe", ".dll", ".jar"]
}
},
{
"type": "max_file_size",
"parameters": {
"max_file_size": 50
}
}
]
}Create it the same way as any other organisation ruleset:
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/orgs/YOUR_ORG/rulesets \
--input push-content-limits.jsonCommon errors
- Adding a
ref_namecondition to a push ruleset. The API rejects it because push rulesets are not tied to branches. - Setting
max_file_sizeabove 100. The value must be between 1 and 100. - Expecting the rule to clean up existing files. A file already in history stays there. A later commit that does not touch it passes.
- Forgetting that the rule travels with forks. A contributor pushing to a fork of a targeted repository is checked too.
- Using bypass mode
pull_request. Pushes are not pull requests, so give push bypass actorsalways.
Exercise
Exercise
A push ruleset sets maxfilesize to 10 and targets all repositories. A developer pushes a branch whose first commit adds a 15 MB file and whose second commit deletes it. Is the push accepted? What would you change to let a release engineering team push large files?
Show answer
The push is rejected. Every commit in the push is checked, and the first commit contains the 15 MB file, so the later deletion does not help. To let release engineering through, add their team as a bypass actor with bypassmode "always". You could also exclude their repositories in the repositoryname condition.