Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create a repository in an organisation #876

Open
jeroenjacobs79 opened this issue Aug 9, 2021 · 19 comments
Open

Unable to create a repository in an organisation #876

jeroenjacobs79 opened this issue Aug 9, 2021 · 19 comments
Labels
Authentication hacktoberfest Issues for participation in Hacktoberfest Provider Status: Up for grabs Issues that are ready to be worked on by anyone Type: Bug Something isn't working as documented

Comments

@jeroenjacobs79
Copy link

jeroenjacobs79 commented Aug 9, 2021

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

@jeroenjacobs79
Copy link
Author

jeroenjacobs79 commented Aug 9, 2021

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?

@nicksantamaria
Copy link

I've encountered this same problem. Using the owner parameter in the provider block did not work, but setting the GITHUB_OWNER envvar did.

@glennpratt
Copy link

Seeing the same issue, which appears to be a regression from previous versions

@jcudit jcudit added Authentication Type: Bug Something isn't working as documented Provider labels Aug 31, 2021
@serain
Copy link

serain commented Sep 17, 2021

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.

@mariodavid
Copy link

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)?

dan-hill2802 added a commit to north-kite/repo-manager that referenced this issue Oct 3, 2021
dan-hill2802 added a commit to north-kite/repo-manager that referenced this issue Oct 3, 2021
@Sinclert
Copy link

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"
    

@kfcampbell
Copy link
Member

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?

@Sinclert
Copy link

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.

@akloss-cibo
Copy link

I just encountered this with github provider 4.19.2, terraform 1.1.4.

@Jonas-Sander
Copy link

This problem occured for me with 4.20.1, settings the GITHUB_OWNER environment variable fixed it for me.

@pksunkara
Copy link

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.

@RulerOf
Copy link
Contributor

RulerOf commented Nov 4, 2022

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"
    }
  }
}

@nickfloyd nickfloyd added Status: Triage This is being looked at and prioritized Priority: Normal labels Nov 4, 2022
@kfcampbell
Copy link
Member

@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?

@RulerOf
Copy link
Contributor

RulerOf commented Nov 10, 2022

@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.

@kfcampbell
Copy link
Member

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.

@kfcampbell
Copy link
Member

Thanks for giving it a shot! Please @ me on the PR if/when you get to it.

@nickfloyd nickfloyd moved this to 🔥 Backlog in 🧰 Octokit Active Dec 5, 2022
kfcampbell added a commit that referenced this issue Jan 3, 2023
Discussed over in #876, using implicit provider inheritance with this provider does not work, because terraform will prefer the `hashicorp/github` module by default.

Tagging @kfcampbell who requested the PR :)

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
@nickfloyd nickfloyd added the Status: Up for grabs Issues that are ready to be worked on by anyone label Jan 13, 2023
@rajansingh277
Copy link

Initializing provider plugins...

  • Finding integrations/github versions matching "~> 5.0"...

    │ Error: Failed to install provider

@nickfloyd nickfloyd added the hacktoberfest Issues for participation in Hacktoberfest label Sep 20, 2023
@kfcampbell kfcampbell removed the Status: Triage This is being looked at and prioritized label Oct 20, 2023
avidspartan1 pushed a commit to avidspartan1/terraform-provider-github that referenced this issue Feb 5, 2024
…ns#1460)

Discussed over in integrations#876, using implicit provider inheritance with this provider does not work, because terraform will prefer the `hashicorp/github` module by default.

Tagging @kfcampbell who requested the PR :)

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
Copy link

👋 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!

@github-actions github-actions bot added the Status: Stale Used by stalebot to clean house label Jul 17, 2024
@RobbieMcKinstry
Copy link

Bump.

@github-actions github-actions bot removed the Status: Stale Used by stalebot to clean house label Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Authentication hacktoberfest Issues for participation in Hacktoberfest Provider Status: Up for grabs Issues that are ready to be worked on by anyone Type: Bug Something isn't working as documented
Projects
None yet
Development

No branches or pull requests