Skip to content

Commit

Permalink
Add preconditions for non-main default branch creation and setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Arnold committed Feb 21, 2025
1 parent 83a4f99 commit ff423c6
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions github_branch.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,42 @@
# slug = var.github_codeowners_team
# }

# not creating main branch because its created by default when repo is created
# Create non-main default branch if specified
resource "github_branch" "branch" {
count = var.github_default_branch == "main" ? 0 : 1
repository = local.github_repo.name
branch = var.github_default_branch

# Only create if not "main" and repository exists
lifecycle {
precondition {
condition = var.github_default_branch != "main"
error_message = "Default branch is 'main', skipping branch creation as it's created by default"
}
}

depends_on = [
github_repository.repo
]
}


# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_default
# Set the default branch
resource "github_branch_default" "default_main_branch" {
count = var.github_default_branch == "main" ? 0 : 1
repository = local.github_repo.name
branch = var.github_default_branch

# Only set if not "main"
lifecycle {
precondition {
condition = var.github_default_branch != "main"
error_message = "Default branch is already 'main', no need to set default"
}
}

depends_on = [
github_branch.branch
]
}


data "github_user" "pull_request_bypassers" {
for_each = toset(var.pull_request_bypassers)
username = each.value
Expand Down

0 comments on commit ff423c6

Please sign in to comment.