Skip to content

[BUG]: github_repository: Pages is enabled before gh-pages branch is created – API returns 422 #2671

Open
@subhodas-opengov

Description

@subhodas-opengov

Expected Behavior

A single terraform apply should
1. create the repository,
2. create the gh-pages branch,
3. enable GitHub Pages that serves from that branch, and
4. exit successfully (plan: 2 to add, 0 to change, 0 to destroy).

Actual Behavior

During the same apply the provider sends the POST /repos/{owner}/{repo}/pages request before the gh-pages branch exists, so GitHub responds

422 Validation Failed – The gh-pages branch must exist before GitHub Pages can be built.

Terraform Version

Terraform v1.5.0
on linux_amd64
 provider registry.terraform.io/integrations/github v6.5.2


Affected Resource(s)

•	github_repository
•	github_branch

Terraform Configuration Files

The smallest reproducible case

terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 6.5"
    }
  }
}

provider "github" {
  owner = var.github_owner          # e.g. "my-org"
  token = var.github_token          # PAT with repo + pages:write
}

# --- repository ------------------------------------------------------
resource "github_repository" "pages_site" {
  name        = "pages-test-single-apply"
  visibility  = "public"
  auto_init   = true                 # seeds the default branch (main)

  pages {                            # <-- fails because branch not yet there
    source {
      branch = "gh-pages"
      path   = "/"
    }
  }

  delete_branch_on_merge = true
}

# --- branch ----------------------------------------------------------
resource "github_branch" "gh_pages" {
  repository    = github_repository.pages_site.name
  branch        = "gh-pages"
  source_branch = github_repository.pages_site.default_branch
}

Steps to Reproduce

Steps to Reproduce
1. terraform init
2. terraform apply → observe the 422 error above.

Debug Output

github_repository.pages_site: Creating...
│ Error: POST https://api.github.com/repos/<org>/pages-test-single-apply/pages: 422 The gh-pages branch must exist before GitHub Pages can be built. []
│
│   with github_repository.pages_site,
│   on main.tf line 18, in resource "github_repository" "pages_site":
│   18: resource "github_repository" "pages_site" {

Panic Output

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: TriageThis is being looked at and prioritizedType: BugSomething isn't working as documented

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions