0

I have a Terraform output task which is displaying the data but I want to use the app_name as input variable in the deployment stage, could you help here?

          - task: TerraformTaskV4@4
            name: terraformOutput
            inputs:
              provider: 'azurerm'
              command: 'custom'
              workingDirectory: '$(Pipeline.Workspace)'
              environmentServiceNameAzureRM: service_connection
              customCommand: 'output'
              outputTo: 'file'
              fileName: 'output.json'
              outputFormat: 'json'

https://marketplace.visualstudio.com/items?itemName=charleszipp.azure-pipelines-tasks-terraform#terraform-output-to-pipeline-variables says it created the output as TF_OUT_, but no, it doesn't work. Where does this output.json gets saved? Then I tried getting it from the bash and it does not return anything.

- bash: |
     WEBAPP_NAME=$(cat $(terraformOutput.jsonOutputVariablesPath) | jq '.webapp_name.value' -r)
devopsseeker
  • 19
  • 1
  • 6

1 Answers1

0

First I will recommend you to check if output.json is being created. you can add following task after terraform task.

- bash: ls -la $(Pipeline.Workspace)
  displayName: 'List Pipeline Workspace'

This will help you determine if output.json is being created and where it's being saved.

If output.json is being created in $(Pipeline.Workspace), then in your bash script, you should be able to access it using the correct path:

- bash: |
    WEBAPP_NAME=$(cat $(Pipeline.Workspace)/output.json | jq '.webapp_name.value' -r)
    echo "WEBAPP_NAME=$WEBAPP_NAME"
  displayName: 'Extract Terraform Output'

To use the WEBAPP_NAME in subsequent tasks or stages, you'll need to set it as an Azure Pipeline variable:

- bash: |
    WEBAPP_NAME=$(cat $(Pipeline.Workspace)/output.json | jq '.webapp_name.value' -r)
    echo "##vso[task.setvariable variable=WEBAPP_NAME]$WEBAPP_NAME"
  displayName: 'Set WEBAPP_NAME as Pipeline Variable'

Another Option is to use TerraformCLI Task like below. (Notice the task is different)

- task: TerraformCLI@0
  displayName: 'terraform output'
  inputs:
    command: output
    # ensure working directory targets same directory as apply step
    # if not specified $(System.DefaultWorkingDirectory) will be used
    # workingDirectory: $(my_terraform_templates_dir)

Then you can use like below:

- bash: |
    echo 'some_string is $(TF_OUT_SOME_STRING)'
    echo 'some_sensitive_string is $(TF_OUT_SOME_SENSITIVE_STRING)'
  displayName: echo tf output vars
Daredevil
  • 732
  • 5
  • 13