Github Provider

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:

  • Automate the creation and management of repositories.
  • Configure teams and collaborators with specific permissions.
  • Manage branch protections to enforce code review policies.
  • Integrate with GitHub Actions for CI/CD workflows.

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?

  • The provider block authenticates Terraform with GitHub using a personal access token (var.github_token).
  • The github_repository resource creates a public repository named example-repo.
  • The topics attribute adds tags for easier categorization in GitHub.
  • Metadata such as a description and visibility level are set.

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.