terraform-provider-github
terraform-provider-github copied to clipboard
Unable to create a repository in an organisation
Hello,
I'm trying to create a repository in an organisation using my personal access token. My config is something like this:
terraform {
required_version = "=1.0.3"
required_providers {
aws = {
version = "=3.52.0"
source = "hashicorp/aws"
}
tls = {
source = "hashicorp/tls"
version = "=3.1.0"
}
github = {
source = "integrations/github"
version = "=4.13.0"
}
}
}
provider "github" {
owner = "headincloud"
}
resource "github_repository" "app_repo" {
name = "cluster-apps"
description = "App repository"
visibility = "private"
delete_branch_on_merge = "true"
}
I have set the GITHUB_TOKEN to my PAT (which I granted all the permissions for now), but the repo still gets created under my own user-name, not onder my organisation.
Now, maybe creating organisation repos doesn't work with PAT's, and I should use OAuth? But I have no clue on how to proceed with this. When trying to create a new GitHub OAuth app, it asks for a homepage URL and callback URL but I have no clue on what I should put here for TerraForm. The documentation is not clear on this either...
So, what is the correct approach here? I believe the documentation could be improved on this subject.
Terraform version: 1.0.3 Github provider version: 4.13.0
Update:
gh repo create headincloud/my-test-repo works just fine, so PAT's are allowed to create organisation repositories.
So, the owner statement in the provider section seems to get ignored? I have no other GITHUB_* env set, only GITHUB_TOKEN.
Update 2:
So it works when I set GITHUB_OWNER. Am I correct to assume that if you use GITHUB_TOKEN env var, you also need to use the GITHUB_OWNER env var?
I've encountered this same problem. Using the owner parameter in the provider block did not work, but setting the GITHUB_OWNER envvar did.
Seeing the same issue, which appears to be a regression from previous versions
I faced this issue only when trying to create a repo in a child module. It doesn't pick up the org configuration from the parent module and you have to explicitly pass an aliased provider to the module.
hi @serain ,
I'm facing the same issue within the module. Can you show an example of how you passed in the provider to the module? Just via a dedicated variable in the module or via the provider meta argument from the module (https://www.terraform.io/docs/language/meta-arguments/module-providers.html)?
Same issue with current version (4.17.0).
It seems the documentation covering the usage of the owner / token GitHub provider arguments, and how they could be replaced by the GITHUB_OWNER and GITHUB_TOKEN env. variables is misleading 🙅🏻♂️
In order to get it to work:
-
On the provider file:
provider "github" {} -
On the environment running
terraform apply:export GITHUB_OWNER="your-org-name-here" export GITHUB_TOKEN="your-secret-token-here"
I've spent some time playing with this recently, and I'm not able to reproduce the issue. Here's a super pared-down template as an example:
terraform {
required_providers {
github = {
source = "integrations/github"
version = "4.18.0"
}
}
}
provider "github" {
owner = "kfcampbell-terraform-provider"
token = "ghp_personal_token_redacted"
}
resource "github_repository" "app_repo" {
name = "876-repro"
description = "App repository"
visibility = "private"
delete_branch_on_merge = "true"
}
That repo has been created successfully here in my test organization rather than my personal profile. Can you try that reproduction and let me know if it works for you?
Hi @kfcampbell , thanks for your reply 👋🏻
I just ran your example, changing the owner between my personal account, and an owned organization account, getting it to work in both. Then I tried switching my integrations/github provider back to version 4.17.0 to try to replicate my original setup. However, I could not get the same results as I got when I wrote in this issue 🤔
Perhaps I was confused with another 404 error I got back then (regarding the creation of a branch using a non-existing main branch as the default source), and I thought that 404 error was that Terraform was not able to locate the repository in the first place...
Not sure, but it seems solved.
I just encountered this with github provider 4.19.2, terraform 1.1.4.
This problem occured for me with 4.20.1, settings the GITHUB_OWNER environment variable fixed it for me.
I faced this issue only when trying to create a repo in a child module. It doesn't pick up the org configuration from the parent module and you have to explicitly pass an aliased provider to the module.
FWIW, this occurs only in the above situation.
Had this problem, scratched my head on it for an hour or two, but @serain pointed me in the right direction.
The child module didn't have a required_providers {} block, so when terraform init performed module resolution, it would configure the integrations/github provider in the root module, and then bring up hashicorp/github in the child module.
The child module of course wouldn't get the provider config because the providers don't match, leading to it thinking that the owner config was blank.
To fix it, in your root module:
terraform {
required_providers {
github = {
source = "integrations/github"
}
}
}
provider "github" {
owner = "YourOrgName"
token = "YourToken" // Or use ENV
}
Then in the child module:
terraform {
required_providers {
github = {
source = "integrations/github"
}
}
}
@RulerOf this is something that's bitten multiple people. Would you (or anybody else) have any interest in creating a PR for our documentation that might explain the issue?
@kfcampbell I could certainly take a stab at it, you can assign this issue to me if you like.
Out of curiosity, this smells like an issue that should be resolved in core terraform. When terraform tries to guess providers based on resources, it should prioritize providers that are explicitly or implicitly passed from parent modules, and not... whatever it's doing presently.
Agreed...I used to think (maybe still should think?) I had a misunderstanding of Terraform and now I'm super careful to add a required_providers block everywhere.
This error is probably related to the transition of the provider from Hashicorp- to GitHub-owned.
Thanks for giving it a shot! Please @ me on the PR if/when you get to it.
Initializing provider plugins...
- Finding integrations/github versions matching "~> 5.0"... ╷ │ Error: Failed to install provider
👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!
Bump.
👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!
Bump.
Bump.
Still seeing this issue