Skip to content

Fix: New repos fail to create due to empty repository default branch error #234

@akuzminsky

Description

@akuzminsky

Problem

When creating a new repository via Terraform, the github_branch_default resource fails because the repo is empty (no commits/branches yet):

Error: PATCH https://api.github.com/repos/infrahouse/terraform-aws-percona-server: 422 Validation Failed 
[{Resource:Repository Field:default_branch Code:invalid Message:Cannot update default branch for an empty repository. Please init the repository and push first.}]

  with module.repos["terraform-aws-percona-server"].github_branch_default.main,
  on modules/plain-repo/repos.tf line 45, in resource "github_branch_default" "main":
   45: resource "github_branch_default" "main" {

The github_repository_file resources create files, but the github_branch_default resource runs before the branch actually exists.

Proposed Solution

Create repos from a template repository that already has an initial commit. This ensures:

  1. The main branch exists from the start
  2. github_branch_default can successfully set the default branch
  3. Subsequent github_repository_file resources can commit to the existing branch

Related

  • Template repo already exists: terraform-template (module ih_tf_template in repos.tf)
  • The plain-repo module would need to use template argument in github_repository resource

Workaround

Currently requires manual intervention: re-run terraform apply after the initial files are pushed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions