1. Analyze the Request: The user wants to create a "repository ruleset". This is a specific feature in GitHub (and similar platforms like GitLab or Bitbucket, though "ruleset" usually implies GitHub's newer "Repository Rules" feature). The request asks for three main components:
* Branch protections (standard stuff: require reviews, status checks, etc.).
* Checks (CI/CD status checks).
* Exceptions (bypass lists).
-
Identify the Platform: While the concept exists elsewhere, the terminology "Repository Ruleset" is distinctively GitHub (introduced late 2022/early 2023). I will focus on GitHub's implementation but write it in a way that is applicable generally, while providing specific JSON/Terraform/YAML examples that are GitHub-centric.
-
Determine the Structure of the Answer: A good answer should be:
- Actionable: Provide code/configuration examples.
- Explanatory: Explain why these settings are chosen.
- Categorized: Break down the three requested areas (Protections, Checks, Exceptions).
- Scenario-based: Offer a "Strict/Production" vs. "Relaxed/Development" option.
-
Drafting the Content - Step-by-Step:
-
Introduction: Briefly define what a Repository Ruleset is (modern replacement for Branch Protection Rules) and why it's better (reusability, layered enforcement).
-
Component 1: Branch Protections (The "What"):
- Restrictions: No force pushes, no deletions.
- Reviews: Require PR, require approval (1 or 2), dismiss stale reviews.
- Merge Strategy: Linear history or squash? (Let's suggest requiring linear history for cleaner logs).
-
Component 2: Checks (The "Validation"):
- CI Status: Require tests to pass.
- Code Scanning: Require CodeQL/Dependabot checks.
- Workflow files: Require status checks to pass from specific workflows.
-
Component 3: Exceptions (The "Who"):
- Bypass list: Repository Admins, Maintainers, or specific teams (e.g.,
release-engineering).
- Deployment Keys: (Less common in rulesets, but relevant).
-
Implementation Methods (The "How"):
- Option A: GitHub UI (The Click-Ops way). Describe the steps.
- Option B: GitHub REST API (JSON). Provide a JSON payload example. This is technical and precise.
- Option C: Terraform (IaC). This is best practice for DevOps. I'll use the
github_repository_ruleset resource.
-
Refining the Examples (Self-Correction during drafting):
-
Constructing the Scenario: Let's create a "Standard Production Ruleset".
- Name: "Main Branch Protection".
- Target:
main and master.
- Bypass: Admins (usually Organization Admins or a specific
bot account).
- Rules: PR required (1 review), Linear History, No Force Push, Status Checks (CI/Tests).
-
Final Review of the Output:
- Does it cover branch protections? Yes.
- Does it cover checks? Yes.
- Does it cover exceptions? Yes.
- Is the tone professional and helpful? Yes.
-
Formatting: Use Markdown code blocks, bold text for emphasis, and lists for readability.
(Self-Correction Details during JSON construction):
I need to be careful with the type field in the JSON rules. GitHub uses specific strings like "creation", "update", "deletion", "required_linear_history", "pull_request", "required_status_checks".
For pull_request, I need nested parameters like dismiss_stale_reviews_on_push, require_code_owner_review, required_approving_review_count.
For required_status_checks, I need required_status_checks array with context.
Let's write the Terraform code clearly too, as that's popular.
- Final Output Generation: (This matches the provided good response).
Here is a comprehensive guide to creating a Repository Ruleset.
While "Branch Protection" exists as a legacy feature, Repository Rulesets (available on GitHub Pro, Team, and Enterprise) are the modern standard. They allow you to define layered protections (e.g., one rule for main, another for release/*) and manage bypass permissions more granularly.
Below is a configuration for a Strict Production Ruleset, including the JSON representation (for API usage), a Terraform definition (for Infrastructure as Code), and a breakdown of the logic.
Scenario: Protecting main and release branches
Goal: Enforce code quality, prevent force-pushes, and allow specific teams to bypass rules during emergencies.
1. The Configuration Logic
Before implementing, define the three pillars requested:
A. Branch Protections (The "What")
- Target:
main, master, and release/* branches.
- Restrictions:
- Force Pushes: Block (prevent history overwriting).
- Deletions: Block.
- Linear History: Require (enforce rebase/merge strategies).
B. Checks (The "Validation")
- Status Checks: Must pass CI (e.g.,
ci/tests, build-success) and code scanning (e.g., CodeQL).
- Pull Request Requirements:
- Require 1 approving review.
- Dismiss stale reviews when new commits are pushed.
- Require conversation resolution (no open threads allowed).
C. Exceptions (The "Who")
- Bypass List: Repository Admins, the
Release-Managers team, and the dependabot[bot] service account.
2. Implementation: GitHub UI
To create this via the GitHub web interface:
- Navigate to the repository Settings.
- In the left sidebar, click Branches.
- Click New ruleset > New branch ruleset.
- Naming: Name it
Production Protection.
- Enforcement: Set to Active.
- Bypass Actors (Exceptions):
- Click Add bypass actor.
- Select Repository Admin (Role) or specific Teams.
- Target Branches:
- Click Add target > Include by pattern.
- Enter
main, then add another target for release/*.
- Rules:
- Check Block force pushes.
- Check Restrict deletions.
- Check Require a pull request before merging.
- Set required approvals to
1.
- Check Dismiss stale pull request approvals when new commits are pushed.
- Check Require review from Code Owners (optional).
- Check Require status checks to pass.
- Search for your CI checks (e.g.,
build, test) and add them.
- Click Create.
3. Implementation: Terraform (IaC)
If you manage your repository via Terraform, use the github_repository_ruleset resource.
resource "github_repository_ruleset" "main_protection" {
repository = "your-repo-name"
name = "Production Branch Protection"
target = "branch"
enforcement = "active"
# C. Exceptions (Bypass Actors)
bypass_actors {
actor_id = 5 # The ID for "Repository Admins" role (usually 5 in GitHub)
actor_type = "RepositoryRole"
bypass_mode = "always"
}
bypass_actors {
actor_id = github_team.release_managers.id # Reference a team ID
actor_type = "Team"
bypass_mode = "always"
}
# A. Branch Protections (Targeting)
conditions {
ref_name {
include = ["refs/heads/main", "refs/heads/release/*"]
exclude = []
}
}
# A & B. Rules
rules {
deletion = true
non_fast_forward = true # Blocks force pushes
pull_request {
required_approving_review_count = 1
dismiss_stale_reviews_on_push = true
required_review_thread_resolution = true
}
required_status_checks {
required_check {
context = "ci/build"
}
required_check {
context = "ci/security-scan"
}
}
}
}
4. Implementation: JSON (API)
If you are automating via a script (using gh api or curl), here is the JSON payload structure.
{
"name": "Production Ruleset",
"target": "branch",
"enforcement": "active",
"bypass_actors": [
{
"actor_id": 5,
"actor_type": "RepositoryRole",
"bypass_mode": "always"
}
],
"conditions": {
"ref_name": {
"include": [
"refs/heads/main",
"refs/heads/release/*"
],
"exclude": []
}
},
"rules": [
{
"type": "deletion"
},
{
"type": "non_fast_forward"
},
{
"type": "pull_request",
"parameters": {
"required_approving_review_count": 1,
"dismiss_stale_reviews_on_push": true,
"require_last_push_approval": false,
"required_review_thread_resolution": true
}
},
{
"type": "required_status_checks",
"parameters": {
"strict_required_status_checks_policy": false,
"required_status_checks": [
{
"context": "ci/build"
},
{
"context": "ci/security-scan"
}
]
}
}
]
}
To apply this via GitHub CLI:
gh api repos/{owner}/{repo}/rulesets -X POST -f @ruleset.json