This document provides instructions for setting up branch protection rules for the SovereignCore repository.
Branch protection rules ensure:
- Code quality through required reviews
- Automated testing before merging
- Prevention of accidental force pushes
- Consistent deployment process
-
Navigate to Repository Settings
- Go to your GitHub repository
- Click on "Settings" → "Branches"
- Click "Add branch protection rule"
-
Branch name pattern:
main -
Enable the following rules:
✅ Require a pull request before merging
- Require approvals: 1 (or 2 for critical projects)
- Dismiss stale pull request approvals when new commits are pushed
- Require review from Code Owners (if CODEOWNERS file exists)
✅ Require status checks to pass before merging
- Require branches to be up to date before merging
- Status checks that are required:
Code QualityTests (3.11)(or all Python versions)CodeQL Security AnalysisDocker Build & Scan
✅ Require conversation resolution before merging
- All review comments must be resolved
✅ Require signed commits
- Ensures commit authenticity
✅ Require linear history
- Prevents merge commits, enforces rebase or squash
✅ Do not allow bypassing the above settings
- Applies to administrators too
✅ Restrict who can push to matching branches
- Only allow specific users/teams (optional)
✅ Allow force pushes: ❌ Disabled
✅ Allow deletions: ❌ Disabled
-
Branch name pattern:
develop -
Enable the following rules:
✅ Require a pull request before merging
- Require approvals: 1
✅ Require status checks to pass before merging
- Status checks that are required:
Code QualityTests (3.11)Docker Build & Scan
✅ Require conversation resolution before merging
✅ Allow force pushes: ❌ Disabled
✅ Allow deletions: ❌ Disabled
-
Branch name pattern:
feature/* -
Enable the following rules:
✅ Require status checks to pass before merging
- Status checks that are required:
Code QualityTests (3.11)
- Status checks that are required:
You can also set up branch protection using GitHub CLI:
# Install GitHub CLI if not already installed
brew install gh
# Authenticate
gh auth login
# Enable branch protection for main
gh api repos/:owner/:repo/branches/main/protection \
--method PUT \
--field required_status_checks[strict]=true \
--field required_status_checks[contexts][]=Code Quality \
--field required_status_checks[contexts][]=Tests \
--field required_pull_request_reviews[required_approving_review_count]=1 \
--field required_pull_request_reviews[dismiss_stale_reviews]=true \
--field enforce_admins=true \
--field required_linear_history=true \
--field allow_force_pushes=false \
--field allow_deletions=falseCreate a .github/CODEOWNERS file to automatically request reviews from specific people:
# Default owners for everything in the repo
* @your-username
# API and security-critical files
/api_server.py @your-username @security-team
/requirements.txt @your-username
/.github/workflows/ @your-username @devops-team
# Docker and deployment
/Dockerfile @your-username @devops-team
/docker-compose.yml @your-username @devops-team
# Security configurations
/redis.conf @your-username @security-team
/users.acl @your-username @security-team
-
Navigate to Repository Settings
- Go to "Settings" → "Environments" → "production"
-
Enable the following:
✅ Required reviewers
- Add specific users who must approve production deployments
- Minimum: 1-2 reviewers
✅ Wait timer
- Optional: Add a wait time (e.g., 5 minutes) before deployment
✅ Deployment branches
- Only allow deployments from
mainbranch
✅ Environment secrets
- Add production-specific secrets:
SECRET_KEYREDIS_PASSWORDGRAFANA_PASSWORD- etc.
-
Navigate to Repository Settings
- Go to "Settings" → "Environments" → "staging"
-
Enable the following:
✅ Deployment branches
- Allow deployments from
mainanddevelopbranches
✅ Environment secrets
- Add staging-specific secrets
- Allow deployments from
After setting up branch protection:
- Try to push directly to
main- should be blocked - Create a PR without passing tests - should not be mergeable
- Create a PR with passing tests - should be mergeable after approval
- Try to force push - should be blocked
- Go to "Settings" → "Security & analysis"
- Enable:
- Dependency graph
- Dependabot alerts
- Dependabot security updates
- Go to "Settings" → "Security & analysis"
- Enable:
- Secret scanning
- Push protection (prevents committing secrets)
- Go to "Security" → "Code scanning"
- Set up CodeQL analysis (already configured in CI/CD)