-3
- task: TerraformTaskV4@4
  name: terraformOutput
  inputs:
   provider: 'azurerm'
   command: 'output'
   workingDirectory: '$(Pipeline.Workspace)'
   environmentServiceNameAzureRM: service_connection

This task is giving the output in json (not in output.json file), how can I extract this and use it in a separate stage for the app deployment?

devopsseeker
  • 19
  • 1
  • 6

1 Answers1

1

To save output to json file you should use custom settings:

- task: TerraformTaskV4@4
  inputs:
    provider: 'azurerm'
    command: 'custom'
    customCommand: 'output'
    outputTo: 'file'
    fileName: 'output.json'
    environmentServiceNameAzureRM: 'rg-the-code-manual'

Then you could use this bash snippet:

    output=$(jq 'to_entries|map([.key] + [.value.value] + [.value.sensitive])' output.json | jq -c '.[]')
    for row in $(echo "${output}" ); do

        variable=$(echo "${row}" | jq -r '.[0]')
        value=$(echo "${row}" | jq -r '.[1]')
        isSecret=$(echo "${row}" | jq -r '.[2]')

        echo "Creating variable ${variable}"
        echo "##vso[task.setvariable variable=${variable};isSecret=${isSecret}]${value}"
        
    done

It will create variable and secret variable based on outputs you have.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107