This guide explains how to import existing GitHub repositories into Terraform management.
Add the repository to terraform.tfvars. Match the current settings as closely as possible:
repositories = {
"existing-repo" = {
description = "My existing repository"
visibility = "private" # or "public"
has_issues = true
has_wiki = false
topics = ["existing", "imported"]
# Add branch_protection block if the repo has protection
branch_protection = {
pattern = "main"
required_pull_request_reviews = {
required_approving_review_count = 1
dismiss_stale_reviews = true
}
}
}
}terraform import 'module.repositories["existing-repo"].github_repository.this' zfael/existing-repoCheck if your repo has branch protection:
If it has protection on main:
terraform import 'module.branch_protection["existing-repo"].github_branch_protection.this' zfael/existing-repo:mainFor other branches, replace main with the branch pattern.
terraform planThis will show any differences between your configuration and the actual repository state.
If terraform plan shows changes you don't want:
- Update
terraform.tfvarsto match the current state - Run
terraform planagain - Repeat until plan shows no changes (or only desired changes)
If you want to apply any differences:
terraform apply