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:
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)
I’m facing this but only when trying to create files in nested directories.
For example this works:
./circle/config.yamlAnd this fails:./github/workflows/build.yamlI can only create 1 directory deep.
EDIT: Fixed my issue, in my case it’s because to update
./github/worklowsyou need theworkflowsscope on your token. Had nothing to do with nested directory.I was also facing this issue, adding
auto_init = trueto thegithub_repositoryfixed 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 = truecreated an initial commit with empty README (docs here), after whichterraform applywas 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_branchdata source to access themainbranch that was created by settingauto_init = trueand replaced any literal"main"withdata.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
masterbranch whereas others had themainbranch.The 404 hint actually meant that the branch specified in the
github_repository_fileresource wasmainwhich didn’t exist in that repo.I solved it by replacing an explicit branch name of
mainwithdata.github_repository.default_branchwhich 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
ownereither explicitly in the provider or throughGITHUB_OWNER😭Documentation is misleading mentioning that
owneris 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