-
-
Notifications
You must be signed in to change notification settings - Fork 2
05 Branch Protection Rules
Umutcan ΓNER edited this page Jun 24, 2025
·
2 revisions
Your safety net for code quality - Learn how branch protection rules keep our main branches secure, enforce quality standards, and automate deployments while maintaining a smooth development workflow.
- π― Overview
- π‘οΈ Protection Strategy
- π Protection Matrix
- π Required Status Checks
- π₯ Review Requirements
- π Merge Strategies
- π Auto-Deployment Rules
- π Additional Protections
- β‘ Quick Reference
- π§ Troubleshooting
- πͺ Next Steps
Branch protection rules are automated safeguards that ensure code quality and security:
|
|
|
|
See how different branches have different protection levels
flowchart TD
subgraph "Protection Levels"
Main[π main<br/>Maximum Protection]
Staging[π§ͺ staging<br/>High Protection]
Develop[π§ develop<br/>Standard Protection]
Feature[π feature/*<br/>No Protection]
end
subgraph "Requirements"
Reviews2[2 Reviews<br/>Core Team]
Reviews1[1 Review<br/>Any Maintainer]
Checks[All CI Checks<br/>Must Pass]
Security[Security Scans<br/>No Critical Issues]
UpToDate[Up-to-date<br/>With Base]
end
Main --> Reviews2 & Checks & Security & UpToDate
Staging --> Reviews1 & Checks & Security
Develop --> Reviews1 & Checks & Security
Feature --> NoReq[No Requirements]
style Main fill:#FF6B6B,stroke:#333,stroke-width:3px,color:#fff
style Staging fill:#FFD93D,stroke:#333,stroke-width:2px
style Develop fill:#6BCF7F,stroke:#333,stroke-width:2px
style Feature fill:#4ECDC4,stroke:#333,stroke-width:1px
Complete overview of all protection rules
Branch | Reviews | Reviewers | Dismiss Stale | Enforce Admins | Restrictions | Delete Protection |
---|---|---|---|---|---|---|
π main | 2+ | @core team | β | β | Admins only | β |
π§ͺ staging | 1+ | Any maintainer | β | β | None | β |
π§ develop | 1+ | Any maintainer | β | β | None | β |
π feature/* | 0 | - | - | - | None | β |
Setting | Purpose | Impact |
---|---|---|
Dismiss Stale Reviews | Outdated approvals removed when new commits pushed | Ensures reviews are for latest code |
Enforce for Admins | Rules apply even to repository admins | No bypassing for anyone |
Restrict Push Access | Limit who can push even with approval | Extra security layer |
Require Up-to-date | Must merge latest changes before merging | Prevents conflicts |
All checks that must pass before merging
flowchart LR
PR[Pull Request] --> Validation{Validation Layer}
Validation --> Format[β
PR Format<br/>Semantic title<br/>Size limits]
Validation --> Branch[β
Branch Name<br/>Correct pattern]
Validation --> Breaking[β
Breaking Changes<br/>Documentation]
Format & Branch & Breaking --> Quality{Quality Layer}
Quality --> Build[β
Build Check<br/>Compilation]
Quality --> Lint[β
Biome Lint<br/>Code style]
Quality --> Test[β
Vitest Tests<br/>All passing]
Quality --> Type[β
TypeScript<br/>No errors]
Build & Lint & Test & Type --> Security{Security Layer}
Security --> CodeQL[β
CodeQL<br/>SAST analysis]
Security --> Deps[β
Dependencies<br/>No vulnerabilities]
Security --> Secrets[β
Secrets<br/>None exposed]
CodeQL & Deps & Secrets --> Ready[Ready to Merge]
style Ready fill:#6BCF7F
Check Type | Workflow | Required for | Blocking Criteria |
---|---|---|---|
PR Validation | pr-validation.yml |
All PRs | Invalid format/size |
Build | build.yml |
PRs to main | Build failure |
CI Pipeline | ci.yml |
develop/staging | Any test failure |
Security Suite | security.yml |
All branches | Critical vulnerabilities (Details) |
Bundle Analysis | build.yml |
PRs to main | Size regression >10% |
# Example of required status checks for main branch
required_status_checks:
strict: true # Require branches to be up-to-date
contexts:
# Format checks
- "PR Validation / validate"
# Quality checks
- "Build / build"
- "CI / lint"
- "CI / test"
- "CI / typecheck"
# Security checks
- "Security / security-scan"
- "CodeQL / Analyze (javascript)"
Who needs to review your code
flowchart TD
subgraph "Pull Request Review Flow"
Create[PR Created] --> AssignAuthor[Auto-assign Author]
AssignAuthor --> CheckFiles{Check Changed Files}
CheckFiles -->|Critical Path| CoreTeam[Assign @core Team]
CheckFiles -->|Regular Files| AnyMaintainer[Any Maintainer]
CoreTeam --> RequireReview{Target Branch?}
AnyMaintainer --> RequireReview
RequireReview -->|main| TwoReviews[2 Reviews Required]
RequireReview -->|staging| OneReview[1 Review Required]
RequireReview -->|develop| OneReview
TwoReviews --> Approved[PR Approved]
OneReview --> Approved
end
Critical paths requiring @zopiolabs/core team review:
Path Pattern | Why Critical | Review Requirement |
---|---|---|
/packages/core/ |
Framework foundation | 2 from core team |
/packages/auth* |
Security critical | 2 from core team |
/packages/database/ |
Data integrity | 2 from core team |
/.github/ |
CI/CD pipeline | 1 from core team |
**/.env* |
Secrets management | 2 from core team |
**/package.json |
Dependencies | 1 from core team |
Scenario | Action | Applies To |
---|---|---|
New commits pushed | Previous approvals dismissed | All branches |
Review marked stale | Must re-review | All branches |
Admin override | Not allowed | main branch |
Emergency hotfix | 1 review minimum | main branch |
How code gets merged into protected branches
Branch | Merge Method | Commit History | Use When |
---|---|---|---|
π main | Squash & Merge | Clean, linear | Production releases |
π§ͺ staging | Squash & Merge | Clean, linear | Pre-prod testing |
π§ develop | Squash & Merge | Clean, linear | Feature integration |
π feature/* | Any method | Preserves all | Developer preference |
stateDiagram-v2
[*] --> PRCreated: Open PR
PRCreated --> ChecksRunning: Automated checks start
state ChecksRunning {
[*] --> Validation: PR Validation
Validation --> CI: CI Pipeline
CI --> Security: Security Scans
Security --> [*]: All Pass
}
ChecksRunning --> ReviewRequired: Request reviews
state ReviewRequired {
[*] --> Reviewing: Reviewers assigned
Reviewing --> ChangesRequested: Issues found
ChangesRequested --> Reviewing: Address feedback
Reviewing --> Approved: LGTM
}
ReviewRequired --> ReadyToMerge: All requirements met
state ReadyToMerge {
[*] --> UpToDate: Check if current
UpToDate --> Outdated: New commits on base
Outdated --> UpdateBranch: Merge/rebase base
UpdateBranch --> UpToDate: Re-run checks
UpToDate --> Mergeable: Can merge
}
ReadyToMerge --> Merged: Squash & merge
Merged --> [*]: PR closed
- Not enabled for any branches (security best practice)
- All merges require manual approval
- Consider enabling for dependabot updates only
How deployments are triggered by branch protection
flowchart LR
subgraph "Branch Deployments"
Main[π main] -->|Auto-deploy| Prod[Production<br/>app.zopio.dev]
Staging[π§ͺ staging] -->|Auto-deploy| Stage[Staging<br/>staging.zopio.dev]
Develop[π§ develop] -->|Auto-deploy| Dev[Development<br/>dev.zopio.dev]
end
subgraph "Deployment Protection"
Prod -->|Required| ProdChecks[β
All checks<br/>β
2 reviews<br/>β
No vulnerabilities]
Stage -->|Required| StageChecks[β
CI passing<br/>β
1 review]
Dev -->|Required| DevChecks[β
Build success]
end
subgraph "Vercel Integration"
ProdChecks --> VercelProd[Vercel Production]
StageChecks --> VercelPreview[Vercel Preview]
DevChecks --> VercelDev[Vercel Development]
end
Environment | Branch | URL | Additional Protection |
---|---|---|---|
Production | main | app.zopio.dev | β’ Deployment approvals β’ Rollback capability |
Staging | staging | staging.zopio.dev | β’ E2E tests required β’ QA sign-off |
Development | develop | dev.zopio.dev | β’ Automatic deployment β’ Latest features |
Extra security measures for protected branches
Protection | Enabled On | Purpose | Implementation |
---|---|---|---|
Force Push Protection | All protected | Preserve history | GitHub setting |
Deletion Protection | All protected | Prevent accidents | GitHub setting |
Signed Commits | Recommended | Verify authorship | Git GPG/SSH |
Linear History | main, staging | Clean history | Squash merge |
Tag Protection | v*.. | Release integrity | Pattern rule |
flowchart TD
subgraph "Security Layers"
Secret[Secret Scanning<br/>Real-time detection]
Depend[Dependency Scanning<br/>Daily + PR triggered]
Code[Code Scanning<br/>CodeQL analysis]
Container[Container Scanning<br/>Docker images]
end
subgraph "Actions on Detection"
Secret --> Block1[Block push<br/>Alert security team]
Depend --> Block2[Block if critical<br/>Create issue]
Code --> Block3[Create alert<br/>Suggest fix]
Container --> Block4[Fail build<br/>Report in PR]
end
subgraph "Resolution"
Block1 --> Fix[Developer fixes]
Block2 --> Fix
Block3 --> Fix
Block4 --> Fix
Fix --> Rescan[Re-run security checks]
Rescan --> Pass[All clear β
]
end
All protected branch activities are logged:
- Who merged what and when
- Review approvals and dismissals
- Override attempts (if any)
- Failed merge attempts
- Protection rule changes
Common scenarios and solutions
# Check if your PR can be merged
gh pr checks
# View required status checks
gh pr view --json statusCheckRollup
# Update branch with base
git fetch origin
git rebase origin/develop # or staging/main
# Check review status
gh pr view --json reviews
Question | Answer |
---|---|
Why can't I push to main? | Direct pushes disabled - use PR |
Why was my review dismissed? | New commits pushed after approval |
How many reviews do I need? | main: 2, staging/develop: 1 |
Can admins bypass rules? | No for main, yes for others |
Why is merge blocked? | Check failed status checks |
-
Before Creating PR:
- Run all checks locally
- Ensure branch is up-to-date
- Use semantic PR title
-
During Review:
- Respond to all comments
- Don't push until ready
- Request specific reviewers
-
Before Merging:
- Verify all checks green
- Ensure reviews approved
- Check deployment preview
Solutions for common protection issues
Issue | Cause | Solution |
---|---|---|
"Branch out-of-date" | Base branch has new commits | Merge or rebase latest changes |
"Required check failed" | CI/Security check not passing | Fix the failing check |
"Needs approval" | Missing required reviews | Request reviews from team |
"Changes requested" | Reviewer requested changes | Address feedback and re-request |
# Option 1: Merge (preserves commits)
git fetch origin
git merge origin/develop
# Option 2: Rebase (cleaner history)
git fetch origin
git rebase origin/develop
# If conflicts occur
git status # See conflicted files
# Fix conflicts in your editor
git add .
git rebase --continue
For critical hotfixes when normal process is too slow:
- Create hotfix branch from main
- Get expedited review from senior team member
- Ensure critical checks pass (security, build)
- Document in PR why expedited process used
- Follow up with full review post-merge
Now that you understand branch protection, explore these areas:
|
|
- π GitHub Docs: Branch Protection
- π‘οΈ GitHub Docs: CODEOWNERS
- π GitHub Docs: Required Status Checks
Questions? Check Repository Settings | Need help? Ask in Discussions
β¬ Back to Top | β¬ Previous: Workflow Diagram | β‘ Next: Pipeline Overview