Create a repository ruleset: branch protections, checks, and exceptions.
- User
Create a repository ruleset: branch protections, checks, and exceptions.
- Assistant
We are going to design a repository ruleset that includes branch protections, required checks, and exceptions. Let's assume we are using a platform like GitHub, GitLab, or Bitbucket. Since the question is generic, I'll describe common practices.
Steps:
- Identify the main branches (e.g., main, master, develop) that require protection.
- Define branch protection rules for these branches (e.g., require pull request reviews, status checks, etc.)
- List required status checks (CI/CD pipelines, tests, etc.) that must pass before merging.
- Define exceptions (if any) such as specific users or teams that can bypass rules.
Let's break it down:
Branch Protections:
- Require pull request reviews before merging (with a minimum number of approvals, e.g., 1 or 2).
- Require status checks to pass (CI/CD, tests, etc.)
- Require linear history (no merge commits, use squash or rebase).
- Require signed commits (if using GPG).
- Require conversation resolution (all comments must be resolved).
- Restrict pushes (only allow specific users or teams to push directly, if any).
Required Checks (Status Checks):
- We assume we have a CI pipeline that runs on pull requests. We'll require at least:
- build (or equivalent) to pass
- test (or equivalent) to pass
- lint (or equivalent) to pass
- security scan (if applicable)
Exceptions:
- Sometimes, we might have emergency procedures or specific users (like maintainers) who can bypass.
- We might also have exceptions for certain branches (e.g., release branches might have different rules).
However, note that the question asks for a repository ruleset, so we are setting up for the entire repository.
Let's design for a typical GitHub repository:
Branch protection rules for:
- main
- develop (if we use Git Flow)
But note: GitHub branch protection rules are set per branch or pattern.
We can set:
- For main: strict rules (require reviews, status checks, etc.)
- For develop: maybe less strict? But let's assume we want the same for all protected branches.
Alternatively, we can set a pattern:
- Protect all branches that match: main, develop, release/, hotfix/
However, let's keep it simple and protect only main and develop.
But note: the question says "repository ruleset", so we are defining a set of rules that apply to the whole repository.
We'll define:
-
Branch protection for:
- main
- develop
-
For each protected branch, require:
- At least 1 approving review (and no own-approve if the reviewer is the last contributor? We can set "Dismiss stale approvals when new commits are pushed" and "Require review from Code Owners" if we have a CODEOWNERS file).
- All required status checks must pass (we list the CI checks).
- Linear history (no merge commits) and require rebase or squash merging.
- Require signed commits (if we use GPG).
- Require all conversations to be resolved.
-
Restrictions:
- Restrict pushes: only allow specific users or teams (like maintainers) to push directly to these branches. Others must use pull requests.
-
Exceptions:
- We might have an exception for a specific user (e.g., a bot account) that is allowed to bypass the required reviews and status checks? But note: in GitHub, we can specify "Allow specified actors to bypass rules". We can add: - A user (e.g., a bot) that automates merges (like dependabot) if we trust it. - Or a team (e.g., admins) that can bypass.
However, note that the branch protection rules are per branch. So we set the same for main and develop.
Let's write the ruleset in a structured way:
Since the platform is not specified, I'll write in a descriptive way.
Alternatively, if we are using GitHub, we can use the GitHub API or the web interface to set these. But the question is to create a ruleset, so I'll write in a format that can be used in a configuration file if the platform supports (like GitHub's branch protection rules via API or Terraform).
But without a specific platform, let me write in a generic YAML-like format:
repository_ruleset: branch_protections: - branch: main require_pr_reviews: required_approving_review_count: 1 dismiss_stale_reviews_on_push: true require_code_owner_reviews: false # if we don't have CODEOWNERS, set to false require_status_checks: strict: true checks: - name: "build" - name: "test" - name: "lint" - name: "security-scan" require_linear_history: true require_signed_commits: true require_conversation_resolution: true restrict_pushes: users: - "admin1" - "admin2" teams: - "maintainers" # Note: we can also allow by app (like GitHub Apps) but let's stick to users/teams. allow_bypass_by_actors: users: - "dependabot[bot]" # example bot that we trust teams: - "admins" - branch: develop same rules as above? We can set the same, but note: sometimes we might want less strict on develop? But let's assume we want the same. # Alternatively, we can define a pattern for branches to avoid repetition. # But let's keep it explicit.However, note that in GitHub, we cannot set the same rule for multiple branches in one rule. We have to set per branch or by pattern.
But the above is just an example.
Let's adjust: if the platform supports patterns, we can do:
repository_ruleset: branch_protections: - pattern: "main" ... rules ... - pattern: "develop" ... same rules ...But note: the pattern might be a regex or a wildcard.
Alternatively, we can set for all branches that are not temporary (like feature branches) by using a pattern that matches our important branches.
However, the question asks for a repository ruleset, so we are defining the entire set.
Let's assume we are using GitHub and we want to set via the API or a configuration management tool.
Important: The required status checks must exactly match the check names in the repository.
Also, note that the branch protection rules are independent per branch, so we have to set for each.
But if we have many branches, we might use a pattern. However, the pattern in GitHub branch protection is limited (only wildcard for * and ?). We can set:
- For main and develop: separate rules? Or we can set a pattern that matches both? Actually, we can set two rules, one for main and one for develop.
Since they are two separate branches, we set two rules.
Now, about exceptions: we are allowing specific actors to bypass. But note: bypassing what? We can set:
- Bypass required pull request reviews.
- Bypass required status checks.
In GitHub, when you set "Allow specified actors to bypass rules", you can choose which rules to bypass for each actor? Actually, it's a single setting: the actor can bypass all the rules set in that branch protection rule.
So we can have:
For main and develop, we allow: - dependabot[bot] to bypass all rules (so it can push directly without PR, or without reviews? Actually, bypassing rules means it can push directly and the checks are not required? But note: if we restrict pushes, then the bot must be in the allowed list to push. Alternatively, we can let the bot open a PR and then we can set that the bot's PRs don't require reviews? But that's not typical.
Actually, the "Allow specified actors to bypass rules" in GitHub allows the actor to push directly to the branch (bypassing the pull request requirement) and also bypass the required status checks? Let me check:
According to GitHub docs:
- "Allow specified actors to bypass rules" allows the specified users, apps, or teams to bypass the branch protection rules. This includes:
- Pushing to the branch without a pull request.
- Merging a pull request without meeting the branch protection requirements.
So for the bot, we might want it to be able to push directly (for example, for automated version bumps) and we don't want to require reviews for its changes.
But note: if we set "Restrict pushes", then only the specified users and teams can push. So we must include the bot in the restrict_pushes list? Actually, the "Restrict pushes" is a separate setting. And the "Allow specified actors to bypass rules" is about bypassing the requirements (like reviews and status checks) but not necessarily the push restriction.
Actually, the push restriction is about who can push to the branch at all. The bypass is about bypassing the requirements when pushing or merging.
So we have:
- Restrict pushes: only these users/teams can push (without PR) to the branch.
- Then, for these users/teams, do they still need to meet the requirements? By default, yes. But we can set "Allow specified actors to bypass rules" so that the specified users/teams (or apps) can push and merge without meeting the requirements.
So for our bot, we want:
- The bot to be able to push directly (so we add it to the restrict_pushes list? Actually, if we restrict pushes, then only those in the list can push. So we must add the bot to the list if we want it to push directly. But then, if the bot is in the list, it can push but still needs to meet the requirements? Unless we also add it to the bypass list.
Alternatively, we can not restrict pushes and just use the bypass? But then anyone can push if they are in the bypass list? Actually, no: the bypass list is for bypassing the requirements, but without push restriction, anyone can push? Wait, no: the branch protection rule with "Require pull request reviews before merging" does not prevent pushing to the branch? Actually, it does not. It only requires that changes come via PR and get reviews. But if we don't restrict pushes, then anyone with write access can push directly? That's a separate permission.
So we have:
- Repository permissions: who has write access? (That's at the repo level, not branch protection)
- Branch protection:
- Require PR reviews: forces all changes to come via PR and get reviews.
- Restrict pushes: limits who can push directly (even if they have write access). So if we set "Restrict pushes", then only the specified users/teams can push directly (without PR). But note: if you have write access and are not in the restrict list, you cannot push directly, you must use PR.
And then the bypass: allows the specified actors to bypass the requirements (so they can push directly and the PRs they open don't require reviews or status checks).
So for our bot, we want:
- The bot to be able to push directly (so we add it to the restrict_pushes list? Actually, if we restrict pushes, then the bot must be in the list to push directly. But if we don't restrict pushes, then anyone with write access can push directly? That's not what we want.
Let me clarify:
Without "Restrict pushes", any collaborator with write access can push directly to the branch. But we have "Require PR reviews", so that doesn't prevent pushing? Actually, the "Require PR reviews" does not prevent pushing, it just requires that when you merge a PR, it must have reviews. But you can still push directly to the branch without a PR? That is true unless we also set "Restrict pushes".
So to prevent direct pushes by most users, we set "Restrict pushes" and only allow specific users/teams (like maintainers) to push directly. Then, for the bot, we want it to push directly, so we add the bot to the restrict_pushes list.
But then, when the bot pushes, does it need to have the required status checks? Yes, because the branch protection requires status checks for all pushes? Actually, the status checks are required for merging a PR, but for direct pushes? The status checks requirement in branch protection is for merging a PR? Actually, the GitHub branch protection "Require status checks" is for the PR merge. But if we allow direct pushes (by the bot) then the bot's direct push would not go through the status checks? And that's acceptable because we trust the bot.
However, note: the branch protection rule "Require status checks" only applies to PRs? Actually, the documentation says: "Require status checks to pass before merging". So for direct pushes, there is no PR to merge, so the status checks are not required? But then the direct push might break the branch? That's why we only allow trusted users (and bots) to push directly.
So the plan:
For main and develop:
- Require PR reviews (so for PRs, we require reviews and status checks). - Restrict pushes: only allow maintainers and the bot to push directly (without PR). - Then, for the bot, we don't want it to be required to have reviews and status checks? But the bot is pushing directly, so the PR requirements don't apply. However, if the bot opens a PR (which it might not, because it pushes directly) then we might want to bypass the requirements for the bot's PRs? But if the bot is pushing directly, then we don't care about the PR requirements.But wait: if the bot is in the restrict_pushes list, it can push directly and we don't require status checks for that direct push? Actually, the status checks requirement is only for merging PRs. So the direct push by the bot is not subject to status checks. That's acceptable.
However, if the bot opens a PR (for some reason) then that PR would be subject to the branch protection rules (unless we add the bot to the bypass list). But if the bot is pushing directly, it might not open PRs.
So we can:
Option 1: - Restrict pushes: allow maintainers and the bot. - Then, for the bot, we don't need to add to bypass because it's pushing directly and the PR requirements don't apply to direct pushes. Option 2: - We don't restrict pushes, but then anyone with write access can push directly? That's not good.So we choose Option 1.
But what if we want the bot to be able to merge PRs without reviews? Then we need to add the bot to the bypass list. Because if the bot opens a PR (maybe from a fork) and we want that PR to be merged without reviews, then we need to bypass the review requirement.
However, if the bot is only pushing directly, then we don't need to bypass the PR requirements.
But note: the bot might be used to automate merges of PRs (like when all checks pass, it merges). Then we want the bot to be able to merge without needing an additional review? Actually, the branch protection requires at least one review. If the bot is the one who made the PR (for example, dependabot opens a PR) then the bot's own PR would require a review from someone else? But if we trust the bot, we might want to allow the bot to merge its own PRs without a review? That's where the bypass comes in.
So we have two scenarios:
1. The bot pushes directly to the branch (without PR). Then we need to allow the bot to push directly (restrict_pushes) and we don't care about the PR requirements. 2. The bot opens a PR (maybe from a fork) and we want that PR to be merged without requiring a review? Then we need to allow the bot to bypass the review requirement for its own PRs.But note: if the bot is opening a PR from a fork, then the bot must have write access to the repository? Actually, dependabot opens PRs from its own fork and then we merge them. The bot (dependabot) does not have write access to the main repository. So in that case, the bot cannot push directly. It can only open PRs.
So for dependabot, we want:
- The bot's PRs to be merged without requiring a review? Actually, we might still want a review from a maintainer? But if we trust dependabot, we might set that we require review from a code owner, and if the code owner is the bot? That doesn't make sense.Alternatively, we can set the branch protection to require at least 1 review, but then we can set that the bot's PRs are exempt by adding the bot to the bypass list.
So we might need both:
- For the bot to push directly (if it ever does) we add it to restrict_pushes. - For the bot's PRs to be merged without reviews, we add it to the bypass list.But note: if the bot is in the bypass list, then for PRs opened by the bot, the branch protection rules (reviews and status checks) are skipped? Actually, the bypass allows the actor to bypass the rules when merging a PR. So if the bot opens a PR, then when merging that PR, the bot (or anyone with merge permission) can merge without the required reviews and status checks? But wait, the bypass is for the actor (the bot) to bypass the rules. So if the bot is merging its own PR, it can bypass? But what if a maintainer merges the bot's PR? Then the maintainer is not bypassing, so the PR would require reviews and status checks? Actually, the bypass is for the actor (the bot) to push and merge without meeting the requirements. But if a maintainer merges the bot's PR, the maintainer is not bypassing? So the PR would require reviews and status checks.
This is getting complex.
Let's read the GitHub documentation:
"Allow specified actors to bypass rules"
This setting allows the specified users, apps, or teams to bypass the branch protection rules. This includes:
- Pushing to the branch without a pull request. - Merging a pull request without meeting the branch protection requirements.So if the bot is in the bypass list, then:
- The bot can push directly to the branch (without PR) even if the branch protection requires PRs? Yes. - The bot can merge a PR (any PR? or only its own?) without meeting the requirements? The documentation says "merging a pull request", so it doesn't specify only its own. So if the bot is a user with write access and merge permission, then the bot can merge any PR without meeting the requirements? That might be too permissive.But typically, we add the bot to the bypass list so that the bot's own PRs can be merged without requiring a review? Actually, the bot doesn't have a review (it's the author). So we want to allow the bot's PR to be merged by a maintainer without requiring an additional review? That doesn't make sense because the bot's PR might be from a fork and the bot is not a code owner?
Actually, the bypass is for the actor to bypass the rules. So if the bot is in the bypass list, then when the bot (as a user) performs an action (push or merge) it doesn't have to meet the rules. But if a maintainer merges the bot's PR, the maintainer is not bypassing, so the PR must meet the rules.
So if we want the bot's PRs to be merged without requiring a review (because we trust the bot) we have two options:
Option A: Add the bot to the bypass list and then the bot itself can merge its own PR without review? But the bot doesn't have merge permission? Actually, the bot might not have write access? So it can't merge? Then we rely on a maintainer to merge the bot's PR, and the maintainer would have to meet the requirements (i.e., the PR must have a review and status checks). So that doesn't help. Option B: We don't require review for PRs opened by the bot? That's not directly supported by branch protection.Alternatively, we can set the branch protection to require review from a code owner, and if the bot is a code owner for the files it changes, then the bot's own review counts? But then the bot would be reviewing its own change? That's not typical.
Given the complexity, maybe we don't want the bot to bypass the review requirement. Instead, we can set the branch protection to require at least 1 review, and then for the bot's PRs, we can have a maintainer review it quickly. Or we can set the required review count to 0 for the bot's PRs? Not possible.
So perhaps for the bot, we only want it to be able to push directly (if needed) and for its PRs, we still require reviews? But then the bot's PRs would require a review from a maintainer, which is acceptable.
Therefore, we might not need the bypass for the bot if we only allow the bot to push directly (and not open PRs). But dependabot opens PRs, so we do need to handle that.
Actually, for dependabot, we might want:
- The bot can push directly? No, dependabot opens PRs from a fork. It doesn't push directly to the main branch. - So we don't need to add dependabot to the restrict_pushes list because it won't push directly. - But we want dependabot's PRs to be merged without requiring a review? That's not typical. We usually require a review for every PR, even from dependabot.However, some projects set up dependabot to auto-merge when the PR passes checks. But note: the branch protection requires a review, so even if the checks pass, we need a review. But if we have a policy that dependabot's PRs are safe, we might want to auto-merge without a review.
How to achieve that?
We can set:
- Require at least 1 review, but then we can set the bot as a code owner for the files it changes? Then the bot's own review would count? But the bot is the author, so it's not a separate review.Alternatively, we can use the bypass for the bot, but then the bot must have write access and merge permission to merge its own PR? That's not the case for dependabot.
So maybe we don't require review for dependabot PRs? We can't conditionally require based on the author.
Another idea: we can set the required review count to 0? Then we don't require any review. But that's not secure.
Given the constraints, maybe we do:
- For main and develop, require at least 1 review and status checks. - Then, for dependabot PRs, we have a maintainer review them. But we can set up a GitHub Action that automatically approves dependabot PRs? That's a workaround.But the branch protection doesn't allow automatic approval.
Alternatively, we can create a team (like "dependabot-approvers") and set the bot as a code owner? Then the bot's PR would require approval from the team? But the bot is not a member of the team? Actually, the bot is the author, so it can't approve its own PR.
So we are stuck: we must have a human review for every PR, including dependabot.
Therefore, we don't need to add the bot to the bypass list for review bypass. We only need to add the bot to the restrict_pushes list if we want the bot to push directly? But dependabot doesn't push directly.
So for dependabot, we don't need any special rule? Then we just require reviews for all PRs, including dependabot's.
But wait, what about the bot that we use for automated version bumps (like a custom bot)? That bot might have write access and push directly? Then we want to allow that bot to push directly? Then we add it to the restrict_pushes list.
And for that bot, we don't require status checks for its direct pushes? That's acceptable because we trust the bot.
So to summarize:
For main and develop:
- Require at least 1 review (and dismiss stale reviews, require code owner reviews if we have a CODEOWNERS file and want to enforce that). - Require status checks (build, test, lint, security-scan) to pass before merging. - Require linear history (no merge commits) and require squash or rebase. - Require signed commits (if we use GPG). - Require all conversations to be resolved. - Restrict pushes: only allow maintainers and the custom bot (if any) to push directly. - Allow specified actors to bypass rules: we might not need this if we don't have a bot that needs to merge PRs without reviews? Or if we have a bot that opens PRs and we want to auto-merge without a review, then we need a different approach.Since the question is about creating a ruleset, I'll assume we have a custom bot that pushes directly and we don't have a bot that opens PRs for auto-merge without review.
So we set:
For main and develop:
restrict_pushes: users: [ "admin1", "admin2", "custom-bot" ] teams: [ "maintainers" ] and we do not set any bypass (or we set bypass only for the custom bot? But if the custom bot is in the restrict_pushes, it can push directly, and that's enough. The bypass is for merging PRs without meeting requirements. We don't need that for the custom bot because the custom bot doesn't open PRs? It pushes directly.But what if the custom bot opens a PR? Then we might want to bypass the requirements? But if the custom bot is in the restrict_pushes, it can push directly, so why open a PR? So we assume the custom bot only pushes directly.
So we don't set any bypass.
However, if we have a bot that opens PRs and we want to auto-merge without review, we cannot do that with branch protection unless we bypass the review requirement for that bot. But then the bot must have merge permission? And we don't want to give merge permission to the bot? Actually, if the bot is in the bypass list, it can merge PRs without meeting the requirements, but it still needs to have the permission to merge? The bypass only bypasses the requirements, not the permission.
So if we want the bot to merge its own PR without review, we must:
- Give the bot write access and merge permission? That's dangerous.Alternatively, we can set the required review count to 0 for the bot's PRs? Not possible.
Given the complexity, I think we stick to the simple case:
We have:
- main and develop branches. - Require reviews (at least 1) and status checks for PRs. - Restrict direct pushes to maintainers and a custom bot (if needed). - No bypass.And for bots that open PRs (like dependabot), we require a human review.
Now, let's write the ruleset in a more structured way.
Since the platform is not specified, I'll write in a way that can be adapted.
Alternatively, if we are using GitHub, we can use the following API representation (as per GitHub's branch protection API):
But note: the GitHub API for branch protection has changed and now uses "rulesets" (a new feature). So we might use the rulesets API.
However, the question says "repository ruleset", which might be referring to GitHub's new rulesets feature.
In GitHub, we can create a ruleset that applies to multiple branches (by pattern) and then define:
- Conditions (like branch name pattern) - Rules (like require pull request, require status checks, etc.)So let's design a ruleset for GitHub:
Ruleset name: "Protect main and develop"
Conditions: - Branch name matches: main or develop
Rules:
1. Require pull request: - Required approving review count: 1 - Dismiss stale reviews on push: true - Require code owner reviews: false (or true if we have CODEOWNERS and want to enforce) 2. Require status checks: - Strict: true (require branches to be up to date with base branch) - Check names: ["build", "test", "lint", "security-scan"] 3. Require linear history: true 4. Require signed commits: true 5. Require conversation resolution: true 6. Restrict pushes: - Push restrictions: Users: ["admin1", "admin2", "custom-bot"] Teams: ["maintainers"] 7. Allow specified actors to bypass rules: (we leave empty, or if we have a bot that needs to bypass, we add it here)But note: the GitHub ruleset also has an option to "Enforce for administrators" (if we want even admins to follow the rules). We might set that to true.
Also, note that the ruleset can be set to "Evaluate for all branches" or only for branches that match the condition.
So we set:
- Conditions: branch name matches main or develop.And then the rules above.
Now, what about other branches? We don't protect feature branches, for example.
So this ruleset only applies to main and develop.
If we want to protect release branches (like release/*) we can adjust the condition.
But the question asks for a repository ruleset, so we are defining one ruleset that covers multiple branches.
Alternatively, we can create multiple rulesets? But the question says "a repository ruleset", so one.
So we design one ruleset with condition on branch name.
Let me write the ruleset in a YAML format that might be used with GitHub's rulesets API (as of 2023, the rulesets feature is in beta and the API might change).
But without official spec, I'll write a generic one.
However, note: the original question does not specify the platform, so I'll write in a descriptive way.
Final answer:
We create a repository ruleset with the following:
- Name: "Protect main and develop"
- Conditions: Branch name matches: ^(main|develop)$
- Rules:
- Require pull request with at least 1 approving review, dismiss stale reviews, and optionally require code owner reviews.
- Require status checks: build, test, lint, security-scan (strict: true)
- Require linear history (no merge commits, require squash or rebase)
- Require signed commits
- Require conversation resolution (all comments resolved)
- Restrict pushes: only allow users admin1, admin2, custom-bot and team maintainers to push directly.
- Allow bypass: (none, or if needed, specify users/teams/apps that can bypass all rules)
And we set the ruleset to be enforced (active).
This is a typical setup for a repository with a main and develop branch.
If we have release branches, we might adjust the condition to include release/* and then maybe have different rules? But for simplicity, we keep the same.
But note: release branches might not require code owner reviews? We can adjust the condition to have multiple rulesets? But the question says one ruleset.
So we assume that for release branches we want the same rules? Or we don't protect release branches? It depends.
Let's assume we only want to protect main and develop.
So condition: branch name is exactly "main" or "develop".
If we want to include release branches, we change the condition to: ^(main|develop|release/.*)$
But then the same rules apply to release branches? That might be too strict for release branches? For example, we might not want to require linear history for release branches? Because we might have merge commits for release branches? Actually, we usually don't. We might want to enforce the same for release branches.
So we adjust accordingly.
Since the question is open, I'll stick to main and develop.
Now, let's write the ruleset in a format that can be used in a GitHub ruleset (using the GitHub API format as of 2023-10-11, but note that the rulesets feature is still in beta and the API might change).
According to GitHub's documentation (https://docs.github.com/en/rest/about-git/rulesets?apiVersion=2022-11-28#create-a-repository-ruleset):
We can create a ruleset with:
{ "name": " Protect main and develop", "target": "branch", "conditions": { "ref_name": { "includes": ["main", "develop"], "excludes": [] } }, "rules": [ { "type": "pull_request", "parameters": { "required_approving_review_count": 1, "dismiss_stale_reviews_on_push": true, "require_code_owner_review": false } }, { "type": "status_checks", "parameters": { "strict": true, "status_checks": [ {"context": "build"}, {"context": "test"}, {"context": "lint"}, {"context": "security-scan"} ] } }, { "type": "linear_history" }, { "type": "signed_commits" }, { "type": "conversation_resolution" }, { "type": "restrict_pushes", "parameters": { "push_restrictions": { "users": ["admin1", "admin2", "custom-bot"], "teams": ["maintainers"], "apps": [] } } } // Note: there is no bypass rule in the ruleset? Actually, bypass is a separate setting at the ruleset level? // In the ruleset, we can set "bypass_actors" at the ruleset level? Let me check: // In the API, the ruleset has a field "bypass_actors" (array of actors that can bypass the ruleset). // So we can set: // "bypass_actors": [ ... ] // But note: the bypass_actors are for the entire ruleset, not per rule. ], "bypass_actors": [ // if we have actors that can bypass the entire ruleset, list them here. // For example: // { "actor_id": 123, "actor_type": "User" } // But we need the user IDs? Or we can use the login? Actually, the API expects the actor_id (the node_id of the user/team/app). // Alternatively, we can leave it empty. ], "enforcement": "active" }
However, note: the "restrict_pushes" rule in the ruleset is a rule that restricts who can push to the branch. And the "bypass_actors" at the ruleset level allows those actors to bypass all the rules in the ruleset? Actually, the bypass_actors are for the entire ruleset.
So if we have a bot that we want to bypass all rules (including push restrictions? Actually, the push restriction rule is part of the ruleset, so if the bot is a bypass_actor, it can bypass the push restriction? But then the bot can push even if not in the push_restrictions? That might be what we want.
Alternatively, we can set:
- For the bot, we want it to be able to push directly (so we add it to the push_restrictions) and also we want it to bypass the other rules (like reviews and status checks) if it opens a PR? But if the bot is in the bypass_actors, it can bypass all rules, including the push restriction? Actually, the bypass_actors bypass the entire ruleset, so if the bot is a bypass_actor, it doesn't need to be in the push_restrictions? Because it can push anyway.So we have two ways:
Option 1: Add the bot to the push_restrictions and not to the bypass_actors. Then the bot can push directly (because it's in the push_restrictions) but if it opens a PR, the PR must meet the requirements (reviews and status checks). But the bot might not have write access to merge? So the bot's PR would require a review and status checks, and then a maintainer merges it. Option 2: Add the bot to the bypass_actors. Then the bot can push directly (bypassing the push restriction? Actually, the push restriction is a rule, so bypassing the ruleset means the bot can push without being in the push_restrictions? And also, if the bot opens a PR, it can merge it without reviews and status checks? But the bot might not have merge permission? Actually, the bypass only bypasses the rules, not the permissions. So if the bot doesn't have write access, it can't push or merge anyway.Given the complexity, and since the bot is a trusted user (with write access) we can:
- Give the bot write access (at the repository level). - Then, we add the bot to the bypass_actors so that it can push directly (without being in the push_restrictions) and also merge PRs without reviews and status checks.But then the push_restrictions rule is useless because the bot can bypass it? Actually, the push_restrictions rule is for non-bypass actors. So if we have maintainers that are not bypass_actors, they must be in the push_restrictions to push directly.
So we can:
- For maintainers (who are not bypass_actors) we add them to the push_restrictions. - For the bot, we add it to the bypass_actors and not to the push_restrictions.Then:
- Maintainers: can push directly because they are in the push_restrictions, but they are not bypass_actors so they must meet the other rules when merging PRs? Actually, when a maintainer pushes directly, they are not merging a PR, so the PR rules (reviews and status checks) don't apply. But the push_restrictions rule is about who can push, and they are allowed because they are in the list. - Bot: can push directly because it's a bypass_actor (bypasses the entire ruleset, including push_restrictions). And if the bot opens a PR, it can merge it without reviews and status checks because it's a bypass_actor.But note: the bypass_actors bypass the entire ruleset, so they don't need to be in the push_restrictions? Yes.
So we set:
push_restrictions: only the maintainers (who are not bypass_actors) are listed. bypass_actors: the bot.But then the maintainers are not bypass_actors, so when they merge a PR, they must meet the requirements? Yes.
However, if we want the maintainers to be able to bypass as well? Then we add them to the bypass_actors and then we don't need to list them in push_restrictions? Because if they are bypass_actors, they can push anyway.
So we have two groups:
- Bypass actors: can do anything (push directly and merge PRs without meeting requirements). We put the bot and maybe the admins here. - Non-bypass actors (maintainers): they must be in the push_restrictions to push directly, and when merging PRs they must meet the requirements.But note: the push_restrictions rule only applies to non-bypass actors? Actually, the bypass_actors bypass the entire ruleset, so they are not affected by the push_restrictions rule.
So we can set:
push_restrictions: list of maintainers (non-bypass actors) who are allowed to push directly. bypass_actors: the bot and the admins (if we want admins to bypass).But then the admins are in both? That's okay, but redundant. We can put the admins in bypass_actors and then they don't need to be in push_restrictions.
So let's design:
We have: Bypass actors: - custom-bot (with node_id) - admin1, admin2 (if we want them to bypass) Push restrictions (for non-bypass actors): - maintainers team And then the rules (pull request, status checks, etc.) apply to non-bypass actors.But note: the ruleset condition is on the branch, and the rules apply to all actors except bypass_actors.
So for the main and develop branches:
- Bypass actors (bot, admins) can push directly and merge PRs without reviews and status checks. - Maintainers (who are not bypass_actors) must: * Use PRs (cannot push directly, unless they are in the push_restrictions? Actually, if they are in the push_restrictions, they can push directly? But then they are not bypass_actors, so if they push directly, they are not merging a PR, so the PR rules don't apply? But the push_restrictions rule allows them to push directly, so they can push without a PR. But then the branch might be broken? That's why we only trust maintainers. However, if a maintainer pushes directly, we don't require status checks? That's acceptable because we trust them. But note: the ruleset also has the rule "require status checks" which is for PR merges. So for direct pushes by maintainers (non-bypass actors) we don't require status checks? Actually, the rule "require status checks" only applies when merging a PR. So if a maintainer pushes directly, it's not a PR merge, so the status checks are not required. So the maintainers can push directly (because they are in the push_restrictions) and we don't require status checks for that push? That's acceptable. But what about the linear history and signed commits? Those are also rules that apply to pushes? Actually, the linear history rule is about preventing merge commits? It applies to the branch history. So if a maintainer pushes a commit that creates a merge commit? Then the linear history rule would prevent that? Actually, the linear history rule prevents merge commits when merging a PR? Or does it prevent creating merge commits at all? According to GitHub: "Require linear history" prevents merge commits on the branch. So if a maintainer pushes a merge commit directly, it would be rejected? Actually, the rule is about the history of the branch. So if the maintainer pushes a merge commit, the branch would have a non-linear history and the rule would block that push? I think yes. Similarly, signed commits: if the maintainer pushes a non-signed commit, it would be blocked? Yes. So the rules (linear history, signed commits) apply to all pushes (direct and via PR) for non-bypass actors? Actually, the ruleset rules apply to all operations on the branch (pushes and PR merges) for non-bypass actors. Therefore, for non-bypass actors: - They can push directly only if they are in the push_restrictions. - And their pushes must satisfy the rules: linear history, signed commits, etc. And for PR merges by non-bypass actors, they must satisfy: pull request reviews, status checks, linear history (when merging, it must be a squash or rebase to keep linear), signed commits (the commits in the PR must be signed?), and conversation resolution. Actually, the signed commits rule: does it require every commit in the PR to be signed? I think so. So in summary: We have: Bypass actors: - bot: can push directly (even without being in push_restrictions) and can merge PRs without meeting any rules (including status checks, reviews, etc.) - admins: same. Non-bypass actors (maintainers): - Must be in the push_restrictions to push directly. - When pushing directly, must follow the rules (linear history, signed commits, etc.) - When merging a PR, must have at least 1 review, status checks pass, linear history (the merge must be squash or rebase), signed commits, and conversation resolution. This is complex. Given the complexity, maybe we don't use bypass_actors for the bot? Instead, we: - Add the bot to the push_restrictions so it can push directly. - And for the bot's PRs (if any) we require the same as for maintainers? But then the bot's PRs would require a review? If the bot is the author, then we need someone else to review? That's acceptable. So we might not use bypass_actors at all. Then the ruleset: Conditions: branch name in [main, develop] Rules: pull_request: required_approving_review_count: 1 dismiss_stale_reviews_on_push: true require_code_owner_review: false status_checks: strict: true status_checks: ["build", "test", "lint", "security-scan"] linear_history: true signed_commits: true conversation_resolution: true restrict_pushes: users: ["admin1", "admin2", "custom-bot"] teams: ["maintainers"] bypass_actors: [] # empty enforcement: active This way: - The bot can push directly (because in push_restrictions) and we don't require reviews or status checks for direct pushes (only for PR merges). But the bot's direct pushes must have linear history and signed commits? Yes, because those rules apply to all pushes. - The bot's PRs (if it opens any) would require reviews and status checks? Yes, because the bot is not a bypass_actor. But wait: the bot is in the push_restrictions, but that doesn't affect PRs. The PR rules are separate. So if the bot opens a PR, it must get a review and status checks? Yes. But we might not want that because we trust the bot. Then we have to use bypass_actors for the bot. So it's a trade-off. Given the original question, I think we are overcomplicating. Let's stick to a simpler design: We have two types of users: - Admins (with full access): we don't restrict them at all? But we want to enforce rules for everyone? Usually, we do. So we set: bypass_actors: [] # no one bypasses Then, for the bot, we add it to the push_restrictions so it can push directly, but its PRs require reviews and status checks. If we want the bot to be able to merge its own PRs without reviews, then we must add the bot to bypass_actors and also give the bot write access and merge permission? Then the bot can merge its own PRs without reviews? But that's dangerous. Alternatively, we can set the bot as a code owner for the files it changes and then the bot's own review counts? But the bot is the author, so it can't review its own PR. So we might have to accept that the bot's PRs require a review from a human. Therefore, I think the simpler design is: - No bypass_actors. - Push restrictions: allow maintainers and the bot to push directly. - All PRs (including bot's) require at least 1 review and status checks. And we accept that the bot's PRs will require a human review. If we have a lot of bot PRs, we can assign a specific team (like "bot-approvers") to review them quickly. So the ruleset: Conditions: branch name in [main, develop] Rules: pull_request: required_approving_review_count: 1 dismiss_stale_reviews_on_push: true require_code_owner_review: false # or true if we have CODEOWNERS and want to require code owner reviews. status_checks: strict: true status_checks: ["build", "test", "lint", "security-scan"] linear_history: true signed_commits: true conversation_resolution: true restrict_pushes: users: ["admin1", "admin2", "custom-bot"] teams: ["maintainers"] bypass_actors: [] enforcement: active And if we have a CODEOWNERS file and want to require code owner reviews, set require_code_owner_review to true. This is a secure and common setup.So I'll go with that.
However, note: the push_restrictions list must include all users and teams that are allowed to push directly. And these users must have write access to the repository? Yes, because if they don't have write access, they can't push anyway.
So we assume that the users and teams in push_restrictions have write access.
Now, let's write the final answer in a clear, descriptive way.
Since the question says "Create a repository ruleset", I'll write it as a configuration for a hypothetical system, but inspired by GitHub's ruleset.
We'll call it: "main_and_develop_protection"
And we'll describe:
- Conditions: branches matching "main" or "develop".
- Rules:
- Require pull request reviews: at least 1, dismiss stale reviews.
- Require status checks: build, test, lint, security-scan (strict).
- Require linear history.
- Require signed commits.
- Require conversation resolution.
- Restrict pushes: users [admin1, admin2, custom-bot] and team [maintainers].
- Bypass actors: none.
- Enforcement: active.
If we are using GitHub, we can create this ruleset via the web UI or API.
Let me write it in a YAML-like format:
name: main_and_develop_protection target: branch conditions: ref_name: includes: - "main" - "develop" excludes: [] rules: - type: pull_request parameters: required_approving_review_count: 1 dismiss_stale_reviews_on_push: true require_code_owner_review: false - type: status_checks parameters: strict: true status_checks: - context: "build" - context: "test" - context: "lint" - context: "security-scan" - type: linear_history - type: signed_commits - type: conversation_resolution - type: restrict_pushes parameters: push_restrictions: users: - "admin1" - "admin2" - "custom-bot" teams: - "maintainers" apps: [] bypass_actors: [] enforcement: activeNote: The "type" for each rule corresponds to GitHub's rule types.
This should be a good starting point.
However, note: the "restrict_pushes" rule in the ruleset is only available for organization-owned repositories? And it might require the repository to be in an organization? Let me check: the push restrictions rule is available for both organization and user-owned repositories? Actually, the rulesets feature is only for organizations? As of now, rulesets are available for organizations and for user-owned repositories? I think it's for organizations.
But the question is about a repository ruleset, so we assume it's in an organization.
If we are in a user-owned repository, we might use the old branch protection rules.
But since the question says "repository ruleset", I assume the new rulesets feature.
So this should be acceptable.
Let me adjust: the old branch protection rules (per branch) are still available, but the new rulesets are more powerful.
Since the question says "ruleset", I use the new ruleset format.
If the platform is not GitHub, adjust accordingly.
I think this is a comprehensive answer. Here's a comprehensive repository ruleset with branch protections, required checks, and exceptions that works for most Git platforms (GitHub/GitLab/Bitbucket):
Repository Ruleset:
main&developProtection1. Protected Branches
Apply to:
main(production)develop(integration)release/*(release branches)hotfix/*(emergency fixes)
2. Branch Protection Rules
Required Pull Request Reviews
- ✅ Require pull request reviews before merging
- ✅ Minimum approvals: 1 (2 for
main/release/*) - ✅ Dismiss stale approvals when new commits are pushed
- ✅ Require review from Code Owners (if CODEOWNERS file exists)
- ✅ Require review from specific teams:
team:backendfor backend changesteam:frontendfor frontend changesteam:devopsfor infrastructure changes
Status Checks
Required before merging (all must pass):
□ build (CI pipeline) □ test (unit/integration tests) □ lint (code style) □ security-scan (SAST/DAST) □ dependency-check (vulnerability scan)- ✅ Require branches to be up to date with base branch (strict mode)
- ✅ Require status checks to pass before merging
Commit & History Rules
- ✅ Require linear history (no merge commits, use squash/rebase)
- ✅ Require signed commits (GPG/SSH signatures)
- ✅ Do not allow direct pushes to protected branches
- ✅ Require conversation resolution (all PR comments resolved)
3. Exceptions & Bypass Rules
Emergency Override (Hotfix Scenarios)
- Users/Groups with emergency bypass:
team:devops-leadsuser:ci-system(automation account)
- Conditions for bypass:
- PR title contains
[EMERGENCY] - Target branch is
hotfix/* - Must include
Emergency approvalcomment fromteam:devops-leads
- PR title contains
Automation Exceptions
- Allow these actors to bypass review requirements:
dependabot[bot](dependency updates)renovate[bot](dependency updates)github-actions[bot](automated deployments)user:release-manager(release automation)
Documentation-Only Changes
- Skip required checks for:
- Paths matching:
docs/**,*.md,CHANGELOG.md - PR labels:
skip-ci,documentation-only - Must have at least 1 approval and
docs-teamreview
- Paths matching:
4. Additional Safeguards
Security-Specific Rules
- ✅ Block merges with secrets in code (detect via pre-receive hook)
- ✅ Require security team approval for:
- Changes to
package.json,Gemfile,requirements.txt - Files in
src/auth/,src/security/,config/secrets.yml - PRs modifying IAM/permissions
- Changes to
Time-Based Restrictions
- ✅ No merges outside business hours (9 AM - 6 PM local time) for
main - Exception:
team:oncallduring production incidents
5. Implementation Examples
GitHub API/CLI Example
# .github/branch-protection.yml (via GitHub API) name: main_protection conditions: ref_name: includes: ['main', 'develop', 'release/*', 'hotfix/*'] rules: - type: pull_request parameters: required_approving_review_count: 1 dismiss_stale_reviews_on_push: true require_code_owner_review: true - type: status_checks parameters: strict: true status_checks: - context: 'build' - context: 'test' - context: 'security-scan' - type: restrict_pushes parameters: push_restrictions: users: ['ci-system', 'release-manager'] teams: ['devops', 'maintainers'] bypass_actors: - actor_id: 12345 # dependabot bot ID actor_type: 'User' enforcement: activeGitLab CI/CD Example
# .gitlab/ci/rules-protection.yml workflow: rules: - if: $CI_COMMIT_BRANCH == "main" when: never # Block direct pushes - if: $CI_PIPELINE_SOURCE == "merge_request_event" merge_request: rules: - if: $CI_MERGE_REQUEST_TITLE =~ /\[EMERGENCY\]/ when: never # Emergency handled separately - if: $CI_MERGE_REQUEST_LABELS =~ /skip-ci/ when: never - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main" when: always
6. Monitoring & Alerts
Required Monitoring
-
Audit log alerts for:
- Bypass rule usage
- Direct push attempts to protected branches
- Status check failures
-
Weekly reports:
- PR merge velocity by team
- Bypass usage patterns
- Failed security scans
-
Automated reminders:
- Stale PRs (>7 days) in
develop - PRs without required reviews
- Emergency bypasses needing post-mortem
- Stale PRs (>7 days) in
7. Rollout Strategy
- Phase 1 (Week 1-2): Apply to
developonly, monitor false positives - Phase 2 (Week 3-4): Add
mainprotection, excludedocs/**temporarily - Phase 3 (Week 5+): Full enforcement, add security rules, refine exceptions
8. Important Notes
⚠️ Before deploying:
- Test in a feature branch first
- Ensure all required CI checks exist and are reliable
- Communicate changes to all developers
- Provide emergency contact procedure
- Document bypass process (Jira ticket required for
[EMERGENCY]tags)
🔄 Review quarterly:
- Are exceptions being abused?
- Are required checks still necessary?
- Adjust team assignments as组织结构 changes
This ruleset provides defense in depth while allowing necessary flexibility. Adjust the exact check names, teams, and conditions based on your organization's CI/CD system and team structure.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.