Skip to content

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

Closed

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

Activity

  1. added
    Type: Bug
    Status: Triage
    on May 27, 2025
  2. moved this from 🆕 Triage to 🔥 Backlog in 🧰 Octokit Activeon Jun 3, 2025
  3. moved this from Backlog to In Progress in Terraform Provideron Feb 19, 2026
  4. removed
    Status: Triage
    on Mar 22, 2026
  5. self-assigned this
    on Mar 22, 2026
  6. added this to the v6.12.0 milestone on Apr 18, 2026
  7. modified the milestones: v6.12.0, v6.13.0 on Apr 20, 2026
  8. moved this from In Progress to Done in Terraform Provideron Apr 22, 2026
  9. moved this from 🔥 Backlog to ✅ Done in 🧰 Octokit Activeon Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type: Bug

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions