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

remove workspace env var and not set it to an empty string. #388

Merged
merged 1 commit into from
May 13, 2023

Conversation

bjhaid
Copy link
Contributor

@bjhaid bjhaid commented May 2, 2023

Setting the environment variable to an empty string confuses the terraform binary as seen below:

TF_WORKSPACE= terraform init -no-color -input=false -get=false -upgrade=false -backend=false

Error: Terraform Cloud returned an unexpected error

invalid value for workspace

With this patch the TF_WORKSPACE will not exist and avoids the above problem. I have tested this locally with terraform 1.4.5 and it works.

@hashicorp-cla
Copy link

hashicorp-cla commented May 2, 2023

CLA assistant check
All committers have signed the CLA.

@mrak
Copy link

mrak commented May 3, 2023

Some additional context: the current behavior (explicitly setting TF_WORKSPACE="") breaks tag-selected workspaces using Terraform Cloud such as this:

terraform {
  cloud {
    organization = "MyOrg"

    workspaces {
      tags = [
        "owner:my-team",
        "project:my-project",
      ]
    }
  }
}

Normally you'd use terraform workspace select to select the workspace, but terraform workspace cannot be used until terraform init has been run. In order for terraform init to run you need to either specify TF_WORKSPACE=my-workspace or you need to populate .terraform/environment.

The bug here is if TF_WORKSPACE is defined regardless of value terraform init skips any checking of .terraform/environment, including if the value of the env var is "".

By removing the TF_WORKSPACE env var instead of setting it to an empty string allows the automation pattern of using .terraform/environment to function as intended.

See the official HashiCorp help article Selecting a workspace when running Terraform in automation

Copy link
Member

@kmoe kmoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may well be a bug in the Terraform automation plugin - have let the team know. In any case this is the intended behaviour for terraform-exec. Thanks!

Setting the environment variable to an empty string confuses the
terraform binary as seen below:

```
TF_WORKSPACE= terraform init -no-color -input=false -get=false -upgrade=false -backend=false

Error: Terraform Cloud returned an unexpected error

invalid value for workspace
```

With this patch the `TF_WORKSPACE` will not exist and avoids the above
problem. I have tested this locally with terraform 1.4.5 and it works.
@bjhaid
Copy link
Contributor Author

bjhaid commented May 10, 2023

@kmoe I fixed the tests can you help re-trigger the builds, thanks!

@bjhaid
Copy link
Contributor Author

bjhaid commented May 11, 2023

@kmoe looks like all checks have passed, how do we get this merged and released?

@kmoe kmoe merged commit 8d60e83 into hashicorp:main May 13, 2023
@bjhaid bjhaid deleted the workspace_env branch May 15, 2023 21:16
@mrak
Copy link

mrak commented Aug 4, 2023

Any chance to get a release out with this change? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants