terraform-provider-github: 404 error when creating files using github_repository_file resource

Terraform Version

= v1.0.5

Affected Resource(s)

github_repository_file

Terraform Configuration Files

resource "github_repository" "repos" {
  for_each = local.repos

  name         = each.key
  description  = join("", [each.value.description, " - Managed by Terraform"])
  visibility   = each.value.visibility
  homepage_url = each.value.homepage_url
}

resource "github_repository_file" "gitignore" {
  for_each = local.repos

  content             = file("templates/.gitignore")
  file                = ".gitignore"
  repository          = github_repository.repos[each.key].name
  branch              = "main"
  overwrite_on_create = true
  commit_message      = "Managed by Terraform"
  commit_author       = "user"
  commit_email        = "user@email.com"
}

Expected Behavior

The repositories should be created, and a default .gitignore file should be created with contents from the given template file.

Actual Behavior

The repositories are created. The .gitignore file fails with the following error:

Error: unexpected status code: 404 Not Found

with github_repository_file.gitignore[“terraform”], on repos.tf line 10, in resource “github_repository_file” “gitignore”: 10: resource “github_repository_file” “gitignore” {

This error is repeated 27 times, one for each repository.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply

Important Factoids

I am running this against my personal Github account. My Github token has the following permissions:

  • repo
    • repo:status
    • repo_deployment
    • public_repo
    • repo:invite
    • security_events

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 14
  • Comments: 20 (3 by maintainers)

Most upvoted comments

I’m facing this but only when trying to create files in nested directories.

For example this works: ./circle/config.yaml And this fails: ./github/workflows/build.yaml

I can only create 1 directory deep.

EDIT: Fixed my issue, in my case it’s because to update ./github/worklows you need the workflows scope on your token. Had nothing to do with nested directory.

I was also facing this issue, adding auto_init = true to the github_repository fixed it for me. I did have to manually delete the repo and reapply the terraform config to get it to work.

The issue seems to have been that the repository was empty and had no branches. Adding auto_init = true created an initial commit with empty README (docs here), after which terraform apply was able to successfully create the files in the main branch. I didn’t see it directly, but I would imagine that the underlying API call to create the files contains the branch name in the path, which would explain the 404 response received when the branch did not exist.

I also used the github_branch data source to access the main branch that was created by setting auto_init = true and replaced any literal "main" with data.github_branch.main.branch.

I actually had this error because in my state I have the resource pointing to a branch that doesn’t exist anymore, even if the new one I specify was there. I solved deleting the resource from the state and then re-apply.

I had this issue and realized the cryptic 404 on one repo despite having applied this same resource in a module to all my other github repos for a year was because that one repo only had the master branch whereas others had the main branch.

The 404 hint actually meant that the branch specified in the github_repository_file resource was main which didn’t exist in that repo.

I solved it by replacing an explicit branch name of main with data.github_repository.default_branch which dynamically determines the default branch name.

You can see the real code example here as I deploy my CODEOWNERS file like this:

https://github.com/HariSekhon/Terraform/blob/master/github_repo/codeowners.tf#L27

and the data source chained from the repo creation to generate proper implicit depends_on:

https://github.com/HariSekhon/Terraform/blob/master/github_repo/repo.tf#L67

Lost 3 hours of my life to this and the solution was to provide owner either explicitly in the provider or through GITHUB_OWNER 😭

Documentation is misleading mentioning that owner is optional.

it has something to do with the set PAT. If i dont set it, url is malformed like that GET /repos//RentTheRunway/bell_labs/branches/master HTTP/1.1

if i set it it fills the username /repos/lerljaku/RentTheRunway/bell_labs/branches/master HTTP/1.1 but it doesnt work if the repo owner is organization instead of user

EDIT:

when passing filling owner to github provider, it works then

provider "github" {
  token = var.gh_pat
  owner = var.gh_org
}