Open j4t1nd3r opened 9 months ago
I did get it to work by:
trigger:
branches:
include:
- main
pool:
vmImage: "windows-latest"
jobs:
- job: TF_WORKSPACE_def
displayName: "TF_WORKSPACE def"
steps:
- task: TerraformInstaller@1
displayName: "Terraform_install 1.5.7"
inputs:
terraformVersion: 1.5.7
- task: TerraformTaskV4@4
displayName: "Terraform_init"
inputs:
provider: "azurerm"
command: "init"
workingDirectory: tf
backendServiceArm: "REMOVED-platform-nonprod (REMOVED)"
backendAzureRmResourceGroupName: "REMOVED-nonprod-base-infra-rg"
backendAzureRmStorageAccountName: REMOVED
backendAzureRmContainerName: tfstate-jat
backendAzureRmKey: "jr-sandbox-tf-wp"
- task: TerraformTaskV4@4
displayName: "Terraform_plan"
inputs:
workingDirectory: tf
command: plan
environmentServiceNameAzureRM: "REMOVED-platform-nonprod (REMOVED)"
- job: TF_WORKSPACE_dev
displayName: "TF_WORKSPACE dev"
dependsOn: TF_WORKSPACE_def
variables:
scope: dev
steps:
- task: TerraformInstaller@1
displayName: "Terraform_install 1.5.7"
inputs:
terraformVersion: 1.5.7
- task: TerraformTaskV4@4
displayName: "Terraform_init"
inputs:
provider: "azurerm"
command: "init"
workingDirectory: tf
backendServiceArm: "REMOVED-platform-nonprod (REMOVED)"
backendAzureRmResourceGroupName: "REMOVED-nonprod-base-infra-rg"
backendAzureRmStorageAccountName: REMOVED
backendAzureRmContainerName: tfstate-jat
backendAzureRmKey: "jr-sandbox-tf-wp"
- task: TerraformTaskV4@4
displayName: "Terraform_Workspace new|select"
inputs:
command: custom
customCommand: workspace
commandOptions: select -or-create $(scope)
workingDirectory: tf
environmentServiceNameAzureRM: "REMOVED-platform-nonprod (REMOVED)"
- task: TerraformTaskV4@4
displayName: "Terraform_plan"
inputs:
workingDirectory: tf
command: plan
environmentServiceNameAzureRM: "REMOVED-platform-nonprod (REMOVED)"
env:
TF_WORKSPACE: $(scope)
- job: TF_WORKSPACE_test
displayName: "TF_WORKSPACE test"
dependsOn: TF_WORKSPACE_dev
variables:
scope: test
steps:
- task: TerraformInstaller@1
displayName: "Terraform_install 1.5.7"
inputs:
terraformVersion: 1.5.7
- task: TerraformTaskV4@4
displayName: "Terraform_init"
inputs:
provider: "azurerm"
command: "init"
workingDirectory: tf
backendServiceArm: "REMOVED-platform-nonprod (REMOVED)"
backendAzureRmResourceGroupName: "REMOVED-nonprod-base-infra-rg"
backendAzureRmStorageAccountName: REMOVED
backendAzureRmContainerName: tfstate-jat
backendAzureRmKey: "jr-sandbox-tf-wp"
- task: TerraformTaskV4@4
displayName: "Terraform_Workspace new|select"
inputs:
command: custom
customCommand: workspace
commandOptions: select -or-create $(scope)
workingDirectory: tf
environmentServiceNameAzureRM: "REMOVED-platform-nonprod (REMOVED)"
- task: TerraformTaskV4@4
displayName: "Terraform_plan"
inputs:
workingDirectory: tf
command: plan
environmentServiceNameAzureRM: "REMOVED-platform-nonprod (REMOVED)"
env:
TF_WORKSPACE: $(scope)
However I would still like to know what is with the inconsistency of TF_WORKSPACE of creating the first workspace and not the second one when it is set as mentioned in my original post.
Hi sorry for late reply. Looks like you solved the issue. I will take a look on the inconsistent behavior.
I have the following pipeline:
Backend Config:
Problem
On first run:
test workspace is not created, I get this message:
The currently selected workspace (test) 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:
Troubleshooting
Between each pipeline run, I am deleting the state file as I make changes to try and figure this out..
I have switched around the variable value for TF_WORKSPACE (dev and test) for the 2nd and 3rd job. *The first job is the one without TF_WORKSPACE set so it uses the default workspace
The second job creates the relevant workspace, the third job gives us the error outline previously.
Can someone explain why dev workspace is created but the test one is not? Surely either both should be or neither considering the test of the code but the variable TF_WORKSPACE is the same?
What is the correct way to create a workspace using this task and the variable TF_WORKSPACE if the method I am trying to use above is incorrect?
If this is not a task issue, do I need to raise this on the terraform github?