@@ -144,6 +144,19 @@ resource "github_repository" "repository" {
144144 }
145145}
146146
147+ # ---------------------------------------------------------------------------------------------------------------------
148+ # Wait for the repository to become fully available in the GitHub API before
149+ # applying dependent resources. This avoids 404 race conditions where the
150+ # GitHub provider issues a PATCH immediately after the initial POST and the
151+ # repository is not yet committed/indexed on the GitHub side.
152+ # ---------------------------------------------------------------------------------------------------------------------
153+
154+ resource "time_sleep" "wait_for_repository" {
155+ depends_on = [github_repository . repository ]
156+
157+ create_duration = " 30s"
158+ }
159+
147160# ---------------------------------------------------------------------------------------------------------------------
148161# Manage branches
149162# https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch
@@ -156,6 +169,8 @@ locals {
156169resource "github_branch" "branch" {
157170 for_each = local. branches_map
158171
172+ depends_on = [time_sleep . wait_for_repository ]
173+
159174 repository = github_repository. repository . name
160175 branch = each. key
161176 source_branch = try (each. value . source_branch , null )
@@ -173,7 +188,7 @@ resource "github_branch_default" "default" {
173188 repository = github_repository. repository . name
174189 branch = local. default_branch
175190
176- depends_on = [github_branch . branch ]
191+ depends_on = [github_branch . branch , time_sleep . wait_for_repository ]
177192}
178193
179194# ---------------------------------------------------------------------------------------------------------------------
@@ -191,6 +206,7 @@ resource "github_branch_protection_v3" "branch_protection" {
191206 github_team_repository . team_repository ,
192207 github_team_repository . team_repository_by_slug ,
193208 github_branch . branch ,
209+ time_sleep . wait_for_repository ,
194210 ]
195211
196212 repository = github_repository. repository . name
@@ -300,6 +316,8 @@ locals {
300316resource "github_issue_label" "label" {
301317 for_each = local. issue_labels_create ? local. issue_labels : {}
302318
319+ depends_on = [time_sleep . wait_for_repository ]
320+
303321 repository = github_repository. repository . name
304322 name = each. value . name
305323 description = each. value . description
@@ -329,6 +347,8 @@ locals {
329347resource "github_repository_collaborator" "collaborator" {
330348 for_each = local. collaborators
331349
350+ depends_on = [time_sleep . wait_for_repository ]
351+
332352 repository = github_repository. repository . name
333353 username = each. key
334354 permission = each. value
@@ -357,6 +377,8 @@ locals {
357377resource "github_team_repository" "team_repository" {
358378 count = length (local. team_ids )
359379
380+ depends_on = [time_sleep . wait_for_repository ]
381+
360382 repository = github_repository. repository . name
361383 team_id = local. team_ids [count . index ]. team_id
362384 permission = local. team_ids [count . index ]. permission
@@ -389,7 +411,7 @@ resource "github_team_repository" "team_repository_by_slug" {
389411 team_id = each. value . slug
390412 permission = each. value . permission
391413
392- depends_on = [var . module_depends_on ]
414+ depends_on = [var . module_depends_on , time_sleep . wait_for_repository ]
393415}
394416
395417# ---------------------------------------------------------------------------------------------------------------------
@@ -412,6 +434,8 @@ locals {
412434resource "github_repository_deploy_key" "deploy_key_computed" {
413435 count = length (local. deploy_keys_computed )
414436
437+ depends_on = [time_sleep . wait_for_repository ]
438+
415439 repository = github_repository. repository . name
416440 title = local. deploy_keys_computed [count . index ]. title
417441 key = local. deploy_keys_computed [count . index ]. key
@@ -434,6 +458,8 @@ locals {
434458resource "github_repository_deploy_key" "deploy_key" {
435459 for_each = local. deploy_keys
436460
461+ depends_on = [time_sleep . wait_for_repository ]
462+
437463 repository = github_repository. repository . name
438464 title = each. value . title
439465 key = each. value . key
@@ -453,6 +479,8 @@ locals {
453479resource "github_repository_project" "repository_project" {
454480 for_each = local. projects
455481
482+ depends_on = [time_sleep . wait_for_repository ]
483+
456484 repository = github_repository. repository . name
457485 name = each. value . name
458486 body = each. value . body
@@ -465,6 +493,8 @@ resource "github_repository_project" "repository_project" {
465493resource "github_repository_webhook" "repository_webhook" {
466494 count = length (var. webhooks )
467495
496+ depends_on = [time_sleep . wait_for_repository ]
497+
468498 repository = github_repository. repository . name
469499 # the optional `name` attribute causes an error so it has been removed
470500 # > Error: "name": [REMOVED] The `name` attribute is no longer necessary.
@@ -492,6 +522,8 @@ locals {
492522resource "github_repository_autolink_reference" "repository_autolink_reference" {
493523 for_each = local. autolink_references
494524
525+ depends_on = [time_sleep . wait_for_repository ]
526+
495527 repository = github_repository. repository . name
496528 key_prefix = each. value . key_prefix
497529 target_url_template = each. value . target_url_template
@@ -504,6 +536,8 @@ resource "github_repository_autolink_reference" "repository_autolink_reference"
504536resource "github_app_installation_repository" "app_installation_repository" {
505537 for_each = var. app_installations
506538
539+ depends_on = [time_sleep . wait_for_repository ]
540+
507541 repository = github_repository. repository . name
508542 installation_id = each. value
509543}
0 commit comments