Search code examples
azure-devopsterraformazure-pipelinesworkspace

Terraform Azure Pipeline is not getting the workspace


I have a pipeline created in Azure Devops that uses terraform module. I have been able to run my pipeline but I'm having issues to detect the created workspace.

The pipeline tasks are described below: enter image description here

The bash script creates the workspace in case it doesn't exist, here you can see the script:

#!/bin/bash

echo "*************************************************************"
echo "*              Create or select workspace                   *"
echo "*************************************************************"

if [ $(terraform workspace list | grep -c "$1") -eq 0 ] ; then
  echo "** Create new workspace $1 **"
  terraform workspace new "$1" -no-color
else
  echo "** Switch to workspace $1 **"
  terraform workspace select "$1" -no-color
fi

I'm certain that the workspace has been created but the terraform subsequent tasks are not picking up the workspace. enter image description here

You can see is setting default instead of development. This is is in terraform plan task

2021-03-12T18:13:48.0424826Z [1m  # azurerm_resource_group.k8s[0m will be created[0m[0m
2021-03-12T18:13:48.0426216Z [0m  [32m+[0m[0m resource "azurerm_resource_group" "k8s" {
2021-03-12T18:13:48.0427763Z       [32m+[0m [0m[1m[0mid[0m[0m       = (known after apply)
2021-03-12T18:13:48.0428525Z       [32m+[0m [0m[1m[0mlocation[0m[0m = "eastus"
2021-03-12T18:13:48.0429278Z       [32m+[0m [0m[1m[0mname[0m[0m     = "default-k8s"
2021-03-12T18:13:48.0430000Z       [32m+[0m [0m[1m[0mtags[0m[0m     = {
2021-03-12T18:13:48.0430713Z           [32m+[0m [0m"environment" = "default"
2021-03-12T18:13:48.0431181Z         }
2021-03-12T18:13:48.0431534Z     }

Has someone faced this issue before, if so any advice on having the terraform tasks detect the workspace that has been created in the bash script?


Solution

  • I was missing a key detail in the bash script section. Which is the working directory where I wanted my scripts to be executed.

    enter image description here You can see that is in the Advanced section. Without that path the scripts were running in the wrong place.

    As a result I have the development workspace and the resource group development-k8s.

    2021-03-12T19:40:13.7898170Z [1m  # azurerm_resource_group.k8s[0m will be created[0m[0m
    2021-03-12T19:40:13.7898875Z [0m  [32m+[0m[0m resource "azurerm_resource_group" "k8s" {
    2021-03-12T19:40:13.7928911Z       [32m+[0m [0m[1m[0mid[0m[0m       = (known after apply)
    2021-03-12T19:40:13.7930291Z       [32m+[0m [0m[1m[0mlocation[0m[0m = "eastus"
    2021-03-12T19:40:13.7954850Z       [32m+[0m [0m[1m[0mname[0m[0m     = "development-k8s"
    2021-03-12T19:40:13.7955573Z       [32m+[0m [0m[1m[0mtags[0m[0m     = {
    2021-03-12T19:40:13.7956351Z           [32m+[0m [0m"environment" = "development"
    2021-03-12T19:40:13.7956951Z         }
    2021-03-12T19:40:13.7957351Z     } 
    

    I hope it saves you the several hours I spent going back and forth through the process :)