From ff423c6b27a2ecc825a00a98ec95029f5f88b048 Mon Sep 17 00:00:00 2001 From: Dave Arnold Date: Fri, 21 Feb 2025 11:51:16 -0800 Subject: [PATCH] Add preconditions for non-main default branch creation and setting --- github_branch.tf | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/github_branch.tf b/github_branch.tf index 2d599d9..de147fe 100644 --- a/github_branch.tf +++ b/github_branch.tf @@ -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