Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
faf4f2e
feat: add functionality and tests for GitHub Enterprise rulesets
Ravio1i Jan 22, 2026
ed459e9
docs: GitHub enterprise ruleset data source and resource
Ravio1i Jan 22, 2026
5dc60c0
refactor: simplify logging messages for enterprise ruleset operations…
Ravio1i Jan 23, 2026
7d617c7
feat: add description to GitHub enterprise ruleset resource and updat…
Ravio1i Jan 23, 2026
002aaf6
chore: improve error handling for setting attributes in GitHub enterp…
Ravio1i Jan 23, 2026
e7f3b4a
refactor: streamline test check composition in GitHub enterprise rule…
Ravio1i Jan 23, 2026
44217d8
fix: update conflicts handling for repository_name in GitHub enterpri…
Ravio1i Jan 23, 2026
72690d7
feat: add repository target rules and update handling in GitHub enter…
Ravio1i Jan 23, 2026
35bbb90
feat: add organization_id condition support to GitHub enterprise ruleset
Ravio1i Jan 23, 2026
49b847c
feat: add merge queue and required deployments support to GitHub ente…
Ravio1i Jan 23, 2026
0782637
feat: remove merge queue and required deployments from GitHub enterpr…
Ravio1i Jan 23, 2026
3021f70
docs: add examples for GitHub Enterprise rulesets including branch, t…
Ravio1i Jan 23, 2026
ab31f98
feat: add customization validation for enterprise ruleset configuration
Ravio1i Jan 23, 2026
94dce56
docs: enhance enterprise ruleset examples with repository target supp…
Ravio1i Jan 23, 2026
7dd2b13
feat: add import functionality for GitHub enterprise ruleset and upda…
Ravio1i Jan 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions examples/enterprise_rulesets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# GitHub Enterprise Ruleset Examples

This directory demonstrates how to configure GitHub Enterprise rulesets using the Terraform GitHub provider.

## Overview

Enterprise rulesets allow you to enforce policies across all organizations in your GitHub Enterprise. The examples showcase all four target types:

- **Branch Target** (`branch_target.tf`) - Branch protection rules with PR requirements, status checks, and commit patterns
- **Tag Target** (`tag_target.tf`) - Tag protection rules with naming patterns and immutability controls
- **Push Target** (`push_target.tf`) - File restrictions, size limits, and content policies (beta feature)
- **Repository Target** (`rulesets.tf`) - Repository management rules for creation, deletion, and naming conventions

## Requirements

- GitHub Enterprise Cloud account
- Personal access token with enterprise admin permissions
- Terraform >= 0.14

## Usage

1. Set your environment variables:

```bash
export TF_VAR_github_token="your_github_token"
export TF_VAR_enterprise_slug="your-enterprise-slug"
```

2. Customize the examples by replacing `"your-enterprise"` with your actual enterprise slug

3. Apply the configuration:

```bash
terraform init
terraform plan
terraform apply
```

## Target Types

Each target type supports different rules:

- **Branch/Tag**: creation, deletion, update, signatures, linear history, PR requirements, status checks
- **Push**: file restrictions, size limits, file extensions, commit patterns
- **Repository**: creation, deletion, transfer, naming patterns, visibility controls

See the individual `.tf` files for detailed examples and available rules.

## Important Notes

- All enterprise rulesets require organization and repository targeting via `conditions`
- The `push` target is currently in beta and subject to change
- Branch and tag targets require `ref_name` conditions
- Repository and push targets do not use `ref_name` conditions
136 changes: 136 additions & 0 deletions examples/enterprise_rulesets/branch_rulesets.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Example: Branch target ruleset with comprehensive branch protection rules
# This ruleset applies to branches across the enterprise

resource "github_enterprise_ruleset" "branch_protection" {
enterprise_slug = "your-enterprise"
name = "branch-protection-ruleset"
target = "branch"
enforcement = "active"

# Optional: Allow certain users/teams to bypass the ruleset
bypass_actors {
actor_id = 1
actor_type = "OrganizationAdmin"
bypass_mode = "always"
}

bypass_actors {
actor_type = "DeployKey"
bypass_mode = "always"
}

# Conditions define which organizations, repositories, and refs this ruleset applies to
conditions {
# Target all organizations in the enterprise
organization_name {
include = ["~ALL"]
exclude = []
}

# Target all repositories
repository_name {
include = ["~ALL"]
exclude = ["test-*"] # Exclude test repositories
}

# Target all branches (required for branch target)
ref_name {
include = ["~DEFAULT_BRANCH", "main", "master", "release/*"]
exclude = ["experimental/*"]
}
}

# Rules that apply to matching branches
rules {
# Prevent branch creation without bypass permission
creation = true

# Prevent branch updates without bypass permission
update = false

# Prevent branch deletion without bypass permission
deletion = true

# Require linear history (no merge commits)
required_linear_history = true

# Require signed commits
required_signatures = true

# Prevent force pushes
non_fast_forward = true

# Pull request requirements
pull_request {
dismiss_stale_reviews_on_push = true
require_code_owner_review = true
require_last_push_approval = true
required_approving_review_count = 2
required_review_thread_resolution = true
allowed_merge_methods = ["squash", "merge"]
}

# Status check requirements
required_status_checks {
strict_required_status_checks_policy = true
do_not_enforce_on_create = false

required_check {
context = "ci/build"
integration_id = 0
}

required_check {
context = "ci/test"
integration_id = 0
}
}

# Commit message pattern requirements
commit_message_pattern {
name = "Conventional Commits"
operator = "regex"
pattern = "^(feat|fix|docs|style|refactor|test|chore)(\\(.+\\))?: .{1,50}"
negate = false
}

# Commit author email pattern
commit_author_email_pattern {
name = "Corporate Email Only"
operator = "regex"
pattern = "@your-company\\.com$"
negate = false
}

# Committer email pattern
committer_email_pattern {
name = "Corporate Email Only"
operator = "regex"
pattern = "@your-company\\.com$"
negate = false
}

# Branch name pattern (only for branch target)
branch_name_pattern {
name = "Valid Branch Names"
operator = "regex"
pattern = "^(main|master|develop|feature/|bugfix/|hotfix/|release/)"
negate = false
}

# Code scanning requirements
required_code_scanning {
required_code_scanning_tool {
tool = "CodeQL"
alerts_threshold = "errors"
security_alerts_threshold = "high_or_higher"
}
}

# Copilot code review (if enabled)
copilot_code_review {
review_on_push = true
review_draft_pull_requests = false
}
}
}
8 changes: 8 additions & 0 deletions examples/enterprise_rulesets/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
github = {
source = "integrations/github"
version = "~> 6.0"
}
}
}
154 changes: 154 additions & 0 deletions examples/enterprise_rulesets/push_rulesets.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Example: Push target ruleset for file and content restrictions
# This ruleset applies to all pushes across the enterprise

resource "github_enterprise_ruleset" "push_restrictions" {
enterprise_slug = "your-enterprise"
name = "push-restrictions-ruleset"
target = "push"
enforcement = "active"

# Allow deploy keys and organization admins to bypass
bypass_actors {
actor_type = "DeployKey"
bypass_mode = "always"
}

bypass_actors {
actor_id = 1
actor_type = "OrganizationAdmin"
bypass_mode = "always"
}

# Conditions define which organizations and repositories this ruleset applies to
# Note: ref_name is NOT used for push target
conditions {
# Target all organizations
organization_name {
include = ["~ALL"]
exclude = []
}

# Target all repositories
repository_name {
include = ["~ALL"]
exclude = ["sandbox-*"]
}
}

# Rules that apply to all pushes
rules {
# Restrict specific file paths from being pushed
file_path_restriction {
restricted_file_paths = [
"secrets.txt",
"*.key",
"*.pem",
".env",
"credentials/*"
]
}

# Limit maximum file size to prevent large files
max_file_size {
max_file_size = 100 # Max 100 MB
}

# Limit maximum file path length
max_file_path_length {
max_file_path_length = 255
}

# Restrict specific file extensions
file_extension_restriction {
restricted_file_extensions = [
"*.exe",
"*.dll",
"*.so",
"*.dylib",
"*.zip",
"*.tar.gz"
]
}

# Commit message pattern
commit_message_pattern {
name = "Valid Commit Message"
operator = "regex"
pattern = "^(feat|fix|docs|style|refactor|test|chore)(\\(.+\\))?: .+"
negate = false
}

# Commit author email pattern
commit_author_email_pattern {
name = "Corporate Email"
operator = "ends_with"
pattern = "@your-company.com"
negate = false
}

# Committer email pattern
committer_email_pattern {
name = "Corporate Email"
operator = "ends_with"
pattern = "@your-company.com"
negate = false
}
}
}

# Example: Security-focused push ruleset
resource "github_enterprise_ruleset" "security_push_restrictions" {
enterprise_slug = "your-enterprise"
name = "security-push-restrictions"
target = "push"
enforcement = "active"

conditions {
organization_name {
include = ["~ALL"]
exclude = []
}

repository_name {
include = ["*-prod", "*-production"]
exclude = []
}
}

rules {
# Block common secret file patterns
file_path_restriction {
restricted_file_paths = [
"*.pem",
"*.key",
"*.cert",
"*.p12",
"*.pfx",
".env",
".env.*",
"secrets.yml",
"credentials.json"
]
}

# Strict file size limits for production
max_file_size {
max_file_size = 50 # Max 50 MB
}

# Block executable and archive files
file_extension_restriction {
restricted_file_extensions = [
"*.exe",
"*.dll",
"*.so",
"*.dylib",
"*.bin",
"*.dmg"
]
}

# Require signed commits
required_signatures = true
}
}
Loading