terraform-provider-github
terraform-provider-github copied to clipboard
This resource can only be used in the context of an organization, "" is a user
Terraform Version
- Terraform version:
1.0.0
- Github version:
4.11.0
Affected Resource(s)
- github_membership
Terraform Configuration Files
resource "github_membership" "configuration" {
username = var.username
role = var.role
}
variable "username" {}
variable "role" {
default = "member"
}
provider "github" {
owner = "foo"
}
Debug Output
Debug/Trace doesn't show any new output that could specify the error.
Panic Output
N/A
Expected Behavior
Should be able to output a plan to remove github_membership
resource belonging to an organization.
Actual Behavior
Errors:
Error: This resource can only be used in the context of an organization, "" is a user.
with module.users_foo["bar"].github_membership.configuration,
on modules/user-memberships/main.tf line 1, in resource "github_membership" "configuration":
1: resource "github_membership" "configuration" {
Steps to Reproduce
-
terraform plan --refresh=false
Important Factoids
- Errors and crashes when running
plan
on Jenkins. There were no changes on our Jenkins configuration, and we were able to runplan
andapply
with no errors prior to this config modification. - Locally running the same configuration with
terraform plan --refresh=false
successfully create a plan. However, it is run with my own Github token, supplied in theprovider
resource. Whereas Jenkins utilizes a global token (which has been working fine until now) in envGITHUB_TOKEN
References
Possibly related to https://github.com/integrations/terraform-provider-github/issues/501
I was experiencing this behaviour when updating the remote (terraform cloud) state file from my CLI using terraform import github_team.core 1234567
.
I resolved the issue by locally setting the environment variable GITHUB_TOKEN
to my personal access token (which has the same privileges as the token set on my terrraform cloud workspace)
In my case the issue was that I was trying to access resources locally that I was not authenticated to do.
see:
https://registry.terraform.io/providers/integrations/github/latest/docs#authentication
Hey, I have also come across this same problem. The issue happens for me when using modules, it seems like the provider owner
value is not inherited by modules and GITHUB_OWNER
must be set in the environment in order to fix.
edited to add: Like @martyca, I have also found that I must have GITHUB_TOKEN
set even though I have provided the token to the github provider block.
still happening in 4.13.0
@roobert workaround of environment variables still works, but it relies now on a deprecated functionality.
╷
│ Warning: "organization": [DEPRECATED] Use owner (or GITHUB_OWNER) instead of organization (or GITHUB_ORGANIZATION)
│
│ with provider["registry.terraform.io/hashicorp/github"],
│ on <empty> line 0:
│ (source code not available)
│
Encountering the same in v4.17.0.
Yes, as @hazcod said, with v4.17.0 I faced the same problem creating a new team
Hi, same error with v4.18.0 too.
Same with v4.19.0... Workaround:
export GITHUB_OWNER="${TF_VAR_github_owner}"
export GITHUB_TOKEN="${TF_VAR_github_token}"
exporting GITHUB_OWNER
does not solve the problem for me.
v4.24.1
Still needing to do export GITHUB_TOKEN="${TF_VAR_github_token}"
to workaround.
Same with v4.19.0... Workaround:
export GITHUB_OWNER="${TF_VAR_github_owner}" export GITHUB_TOKEN="${TF_VAR_github_token}"
This solution works for me with integrations/github
provider version 4.28.0 so long as the provider configuration block is empty. In other words:
provider "github" {
token = "var.token"
owner = "my-org"
}
will fail even with the GITHUB_TOKEN
and GITHUB_OWNER
environment variables set, but
provider "github" {}
will work if the environment variables are set.
Same with v4.19.0... Workaround:
export GITHUB_OWNER="${TF_VAR_github_owner}" export GITHUB_TOKEN="${TF_VAR_github_token}"
This solution works for me with
integrations/github
provider version 4.28.0 so long as the provider configuration block is empty. In other words:provider "github" { token = "var.token" owner = "my-org" }
will fail even with the
GITHUB_TOKEN
andGITHUB_OWNER
environment variables set, butprovider "github" {}
will work if the environment variables are set.
+1 with v.5.7.0
it seems like the provider owner value is not inherited by modules and GITHUB_OWNER must be set in the environment in order to fix.
I wonder if that's a thing we can adjust in our code or a limitation of Terraform as a whole.
Work For me, Provider configure:
variable "GITHUB_TOKEN" {
description = "Github Token Admin"
type = string
}
variable "GITHUB_OWNER" {
description = "Github Owner Organization"
type = string
}
required_providers {
github = {
source = "integrations/github"
version = "~> 4.0"
}
}
provider "github" {
token = var.GITHUB_TOKEN
owner = var.GITHUB_OWNER
}
export TF_VAR_GITHUB_TOKEN={personal_token} export TF_VAR_GITHUB_OWNER={org-name} export GITHUB_OWNER="${TF_VAR_GITHUB_OWNER}" export GITHUB_TOKEN="${TF_VAR_GITHUB_TOKEN}"
I'm in the process of updating our TF code base from the 2.x version of github when I ran into this issue.
It looks like this issue starts in v3.0.0. I tested on v2.9.1 and it works without issue.
Hmm...here's the diff between those two tags. There's some changes in github/config.go that could potentially be at fault.
BEWARE: this error is very misleading. We thought the Go library needed because of https://github.com/integrations/terraform-provider-github/issues/1391
For us, it turns out our Personal Access Token just stopped working for some reason. We made a new token and everything started working again.
Seems like this has come up a lot: https://github.com/integrations/terraform-provider-github/search?q=in+the+context+of+an+organization+is+a+user&type=issues
Maybe this can be added to the README or something as a troubleshooting step?
@thetimbecker agreed that usually the cause is an invalid PAT! Would you be interested in opening up a PR to update the docs accordingly?
Same with v4.19.0... Workaround:
export GITHUB_OWNER="${TF_VAR_github_owner}" export GITHUB_TOKEN="${TF_VAR_github_token}"
This solution works for me with
integrations/github
provider version 4.28.0 so long as the provider configuration block is empty. In other words:provider "github" { token = "var.token" owner = "my-org" }
will fail even with the
GITHUB_TOKEN
andGITHUB_OWNER
environment variables set, butprovider "github" {}
will work if the environment variables are set.
+1 with v.5.7.0
Here's another confirmation 4 months later. This workaround still works in v5.18.0.
@thetimbecker agreed that usually the cause is an invalid PAT! Would you be interested in opening up a PR to update the docs accordingly?
Clearly I never got around to this 😅
Unfortunately I can't take the time to do it well right now, especially since the issue it's fresh in my mind anymore.
👋 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!