The GitHub Provider allows you to manage GitHub resources programmatically with Terraform. From creating repositories to managing teams, access permissions, and branch protections, this provider helps automate your GitHub configurations and enforce consistency across projects.
Key Features:
Example Use Case: Creating a GitHub Repository
Here’s how to create a public GitHub repository with Terraform:
provider "github" {
token = var.github_token
}
resource "github_repository" "example" {
name = "example-repo"
description = "My example repository"
private = false
topics = ["terraform", "example", "github"]
visibility = "public"
tags = {
Environment = "Development"
Team = "DevOps"
}
}
What’s Happening Here?
provider
block authenticates Terraform with GitHub using a personal access token (var.github_token
).github_repository
resource creates a public repository named example-repo
.topics
attribute adds tags for easier categorization in GitHub.Advanced Tip:
Add branch protection to enforce rules like requiring code reviews before merging:
resource "github_branch_protection_v3" "main" {
repository = github_repository.example.name
pattern = "main"
enforce_admins = true
required_pull_request_reviews {
dismiss_stale_reviews = true
require_code_owner_reviews = true
}
required_status_checks {
strict = true
contexts = ["ci/circleci"]
}
}
This configuration applies branch protection to the main
branch, requiring successful status checks and code reviews before merging.