Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

terraform-init do not support TF_ACTION_WORKSPACE #46

Closed
hideto0710 opened this issue Aug 29, 2019 · 4 comments
Closed

terraform-init do not support TF_ACTION_WORKSPACE #46

hideto0710 opened this issue Aug 29, 2019 · 4 comments

Comments

@hideto0710
Copy link

config

- name: terraform-init
  uses: hashicorp/terraform-github-actions/[email protected]
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    TF_ACTION_WORKING_DIR: .
    TF_ACTION_WORKSPACE: production
    TF_ACTION_TFE_TOKEN: ${{ secrets.TF_ACTION_TFE_TOKEN }}

Log

Run hashicorp/terraform-github-actions/[email protected]
/usr/bin/docker run --name ec5e59344abd5d0741c0b5dfca8bc09b44ad_9a9c0b --label 20ec5e --workdir /github/workspace --rm -e GITHUB_TOKEN -e TF_ACTION_WORKING_DIR -e TF_ACTION_WORKSPACE -e TF_ACTION_TFE_TOKEN -e HOME -e GITHUB_REF -e GITHUB_SHA -e GITHUB_REPOSITORY -e GITHUB_ACTOR -e GITHUB_WORKFLOW -e GITHUB_HEAD_REF -e GITHUB_BASE_REF -e GITHUB_EVENT_NAME -e GITHUB_WORKSPACE -e GITHUB_ACTION -e GITHUB_EVENT_PATH -e RUNNER_OS -e RUNNER_TOOL_CACHE -e RUNNER_TEMP -e RUNNER_WORKSPACE -v "/var/run/docker.sock":"/var/run/docker.sock" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" -v "/home/runner/work/wevox-terraform/wevox-terraform":"/github/workspace" 20ec5e:59344abd5d0741c0b5dfca8bc09b44ad
2019/08/29 13:30:36 [DEBUG] Using modified User-Agent: Terraform/0.12.7 terraform-github-actions/1.0

Initializing the backend...

Successfully configured the backend "remote"! Terraform will automatically
use this backend unless the backend configuration changes.

The currently selected workspace (default) does not exist.
  This is expected behavior when the selected workspace did not have an
  existing non-empty state. Please enter a number to select a workspace:
  
  1. production
  2. staging

  Enter a value: 

Error: Failed to select workspace: input not a valid number
##[error]Docker run failed with exit code 1
@hideto0710
Copy link
Author

hideto0710 commented Aug 29, 2019

related hashicorp/terraform#21393 ??

I could fix it by using TF_WORKSPACE

@sudomateo
Copy link
Contributor

Good point! Using TF_WORKSPACE is the preferred method. Glad you're unblocked for now.

@sudomateo
Copy link
Contributor

I tested the TF_WORKSPACE environment variable. It's currently the best method to select a workspace during automation.

@davehewy
Copy link

After following the thread to the bottom a concoction of all your ideas will work.

desired_workspace="test"
export TF_WORKSPACE="default"
terraform init -input=false
# without unsetting the env var it will bork workspace selection command
unset TF_WORKSPACE 
exists=$(terraform workspace list | sed $'s/\r//' | grep -E "(^|\s+)${desired_workspace}$" > /dev/null 2>&1; echo $?)
if [ "${exists}" = "0" ]; then
  terraform workspace select ${desired_workspace}
else
  terraform workspace new ${desired_workspace}
fi

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

No branches or pull requests

3 participants