You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
404 error when creating files using github_repository_file resource #897
resource"github_repository""repos" {
for_each=local.reposname=each.keydescription=join("", [each.value.description, " - Managed by Terraform"])
visibility=each.value.visibilityhomepage_url=each.value.homepage_url
}
resource"github_repository_file""gitignore" {
for_each=local.reposcontent=file("templates/.gitignore")
file=".gitignore"repository=github_repository.repos[each.key].namebranch="main"overwrite_on_create=truecommit_message="Managed by Terraform"commit_author="user"commit_email="[email protected]"
}
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
Reacted by felixg, Enrico La Cava, Daniel Radu, Peter Johnson, Alejandra Ramos, Bartlomiej Danek, Richard Mathieson, Naomichi Yamakita, pneigel-ca, Paul Johnson and 6 more
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.
Reacted by Bishwash, Mark Sutton, Erin, Miłosz Smółka, Ira, Jels Boulangier, Patrick Heneise, Caroline Chiari, Daniel Imber, Simon Templer and 5 more
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.
Reacted by bugi, Snehil Gupta, Sean O'Neill, Arun ak and Marian Puscasu
Reacted by Dave Wongillies, pneigel-ca, Walter Vides and shawn-mason
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
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.
Reacted by Shon Diaz, Christian Mergenthaler and Willian Tavares
I ran into this error and discovered the cause in my case was that there are two Github providers: hashicorp/github and integrations/github. The integrations/github provider works fine, the hashicorp/github provider throws 404 errors. So if your module doesn't explicitly require integrations/github, it'll use the hashicorp provider and blow up.
This problem happens when there's a mismatch between both implementations (integrations and hashicorp).
How to reproduce:
Set up a top level provider to integrations/terraform-provider-github with owner property different from the current user
Create a module that don't define integrations/terraform-provider-github (so it will use the hashicorp/terraform-provider-github by defalt)
Define a github_repository_file inside the module
Expected behaviour:
File is created
Current behaviour:
github_repository_file from a module don't see the integrations/terraform-provider-github owner and try to create the file in the current user repository
Reacted by Mateusz Łoskot, Lucas Telles and Alexandre Nicolaie
So what I ended up doing was log in to my postgres database and manually update the state JSON. Since I had successfully been able to use github_repository_file resource with other repositories, I copied a good value and tweaked it for the repository that wasn't working. Then, I deleted my local terraform state (rm -f .terraform/terraform.tfstate) and reinitialized from my backend (terraform init --backend-config=${CONN_STR}).
I have the same issue, have checked all the above fixes/work arounds and nothing seems to work.
I am trying to create 20+ files and some of them get created but the remaining end up with the same error unexpected status code: 404 Not Found
But if I apply the changes again then the remaining files get created.
I have also checked the API rate limit and it looks good.
I am using the following config
cdktf 0.13.1
terraform v1.3.2
github provider -> registry.terraform.io/integrations/github 4.31.0
In my case something weird happened, I have a fine-grained PAT with content write permission for the target repo which has worked so far without issues, however when I tried to use both the github_repository_file data source and resource the Error: unexpected status code: 404 Not Found was returned, after trying must of the things mentioned above without success I decided to remove all the permissions from the PAT and then started added them again, the weird thing is that the permission combination that worked was properties and content write permissions, this is weird so I then remove the properties permission and guess what? it worked, so it work with the original permission I had at the beginning but I had to disable the same permissions, after this the issues didn't show up again.
I also hit this error and none of these replies fixed it and found another possible cause.
For me, the github_repository_file repository name should NOT include the org. For example, My-Org/my-repo fails with non descriptive 404, while my-repo works as expected.
Now that is a vague error. @shawn-mason your fix worked for me. What confuses me is I have a number of github resources and some require the repository to be org/repo and others, like this case, want just the repo name. I tried everything above and the very last one worked, ha!
It's just funny how many people had this error because of a missing org and mine was because I had it.
You must add a required_providers block to every module that will create resources with this provider. If you do not explicitly require integrations/github in a submodule, your terraform run may break in hard-to-troubleshoot ways.
I also hit this error and none of these replies fixed it and found another possible cause.
For me, the github_repository_file repository name should NOT include the org. For example, My-Org/my-repo fails with non descriptive 404, while my-repo works as expected.
Confirm this was it for me as well. Seems like something that can easily be fixed/handled in the resource I assume
Activity
serain commentedon Sep 17, 2021
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.jcudit commentedon Sep 28, 2021
Can we get more detailed debug logging posted to the issue? Adding
TF_LOG=DEBUGwill help us determine which request is causing the 404.kunalnanda commentedon Sep 29, 2021
@jcudit Here is the debug log:
rtjfarrimond commentedon Oct 19, 2021
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.FalconerTC commentedon Nov 2, 2021
Seeing the same thing with the following TF
Produces the following debug logs
lerljaku commentedon Nov 16, 2021
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
lerrigatto commentedon Jan 26, 2022
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.
jamesggraf-m1 commentedon Feb 24, 2022
I ran into this error and discovered the cause in my case was that there are two Github providers:
hashicorp/githubandintegrations/github. Theintegrations/githubprovider works fine, thehashicorp/githubprovider throws 404 errors. So if your module doesn't explicitly requireintegrations/github, it'll use the hashicorp provider and blow up.ahasna commentedon Mar 22, 2022
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.rotilho commentedon Apr 5, 2022
This problem happens when there's a mismatch between both implementations (integrations and hashicorp).
How to reproduce:
integrations/terraform-provider-githubwith owner property different from the current userintegrations/terraform-provider-github(so it will use thehashicorp/terraform-provider-githubby defalt)Expected behaviour:
Current behaviour:
github_repository_filefrom a module don't see theintegrations/terraform-provider-githubowner and try to create the file in the current user repositorymemexty commentedon Apr 6, 2022
I have the same problem. My repo was empty, so i recreate it with non empty readme file and the problem has gone.
douglascayers commentedon Apr 25, 2022
In my case,
So what I ended up doing was log in to my postgres database and manually update the state JSON. Since I had successfully been able to use
github_repository_fileresource with other repositories, I copied a good value and tweaked it for the repository that wasn't working. Then, I deleted my local terraform state (rm -f .terraform/terraform.tfstate) and reinitialized from my backend (terraform init --backend-config=${CONN_STR}).Here's the JSON snippet I added to my state:
{ "version": 4, "terraform_version": "1.1.9", "resources": [ { "mode": "managed", "type": "github_repository_file", "name": "readme", "provider": "provider[\"registry.terraform.io/integrations/github\"]", "instances": [ { "index_key": "my-awesome-repo", "schema_version": 0, "attributes": { "branch": "main", "commit_author": "my-git-username", "commit_email": "[email protected]", "commit_message": "Created by Terraform", "commit_sha": "af1d88db0dc7a616eca89d88ef25235de7cfe41e", "content": "my-awesome-repo", "file": "README.md", "id": "my-awesome-repo/README.md", "overwrite_on_create": false, "repository": "my-awesome-repo", "sha": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed" }, "sensitive_attributes": [], "private": "cyQstM==", "dependencies": [ "github_repository.my-awesome-repo" ] } ] } ] }Note,
commit_shashould be the commit that represents the file content, usually the most recent commit to the file in GitHub.Note,
shais the SHA1 hash of the file contents. To get the known value in GitHub, use the following curl command against the API:The JSON response includes the
shavalue.shbmx commentedon Oct 26, 2022
I have the same issue, have checked all the above fixes/work arounds and nothing seems to work.
I am trying to create 20+ files and some of them get created but the remaining end up with the same error
unexpected status code: 404 Not FoundBut if I apply the changes again then the remaining files get created.
I have also checked the API rate limit and it looks good.
I am using the following config
cdktf 0.13.1
terraform v1.3.2
github provider -> registry.terraform.io/integrations/github 4.31.0
10 remaining items
yiskaneto commentedon Apr 4, 2024
In my case something weird happened, I have a fine-grained PAT with content write permission for the target repo which has worked so far without issues, however when I tried to use both the
github_repository_filedata source and resource theError: unexpected status code: 404 Not Foundwas returned, after trying must of the things mentioned above without success I decided to remove all the permissions from the PAT and then started added them again, the weird thing is that the permission combination that worked was properties and content write permissions, this is weird so I then remove the properties permission and guess what? it worked, so it work with the original permission I had at the beginning but I had to disable the same permissions, after this the issues didn't show up again.dd84ai commentedon Nov 16, 2024
I managed to resolve my case of problem receiving [] 404 error when creating web hoook
I granted all possible permissions (Administration, Webhooks, Content)
But in the end it was resolved only when i specified
ownerin a providerI reached success with fine grained token i made sure was create for organization
shawn-mason commentedon Apr 8, 2025
I also hit this error and none of these replies fixed it and found another possible cause.
For me, the github_repository_file repository name should NOT include the org. For example, My-Org/my-repo fails with non descriptive 404, while my-repo works as expected.
kferrone commentedon Apr 15, 2025
Now that is a vague error. @shawn-mason your fix worked for me. What confuses me is I have a number of github resources and some require the repository to be org/repo and others, like this case, want just the repo name. I tried everything above and the very last one worked, ha!
It's just funny how many people had this error because of a missing org and mine was because I had it.
taraspos commentedon May 16, 2025
If you're trying to create resource from the module this might solve the problem:
fatbasstard commentedon Jun 20, 2025
Confirm this was it for me as well. Seems like something that can easily be fixed/handled in the resource I assume
deiga commentedon Jan 4, 2026
I think there are at least 2 separate issues surfaced here:
deiga commentedon Jul 3, 2026
Closing as stale. There is not enough recent information to help investigations