Step 16 - Promote Terraform Variables - Build
  • 18 Nov 2019
  • 1 Minute to read
  • Contributors
  • Comment
  • Dark
    Light
  • PDF

Step 16 - Promote Terraform Variables - Build

  • Comment
  • Dark
    Light
  • PDF

Article Summary

In this task the aim is to use the outputs.json file produced by terraform which will contain the json for any variables we specify as output variables. The task will use Powershell to take the variables in the json file and to set them up as pipeline variables to they can be used later in the build pipeline.

In this case we need to setup a property in the Logic App which contains the connection string for the Service Bus instance. Terraform will create/update Service Bus and the output variable will allow us to update the Logic App to dynamically pull in this setting which we do not know and can not calculate upfront.

To achieve this we will use the Azure CLI task and change it to the latest v2 preview so we can use Powershell. It will read the outputs.json file into an object and we will then specify properties we want to promote.

A picture of this task is below.

image.png

YAML

The yaml for this task is below:


steps:
- task: AzureCLI@2
  displayName: 'Azure CLI - Promote Terraform Variables'
  inputs:
    azureSubscription: '[TBC]'
    scriptType: ps
    scriptLocation: inlineScript
    inlineScript: |
     $file = Get-Content -Path '$(Build.ArtifactStagingDirectory)\Terraform\outputs.json'
     Write-Host 'File Content:'
     Write-Host $file
     
     Write-Host "Convert terrafrom output from $jsonPath to devops vars."
     
     $json = $file | ConvertFrom-Json
     Write-Host "Json Object:"
     Write-Host $json
     
     Write-Host "##vso[task.setvariable variable=ServiceBusLogicAppConnectionString]$($json.ServiceBusLogicAppConnectionString.value)"

Powershell

The Powershell used in the task script is below:

$file = Get-Content -Path '$(Build.ArtifactStagingDirectory)\Terraform\outputs.json'
Write-Host 'File Content:'
Write-Host $file

Write-Host "Convert terrafrom output from $jsonPath to devops vars."

$json = $file | ConvertFrom-Json
Write-Host "Json Object:"
Write-Host $json

Write-Host "##vso[task.setvariable variable=ServiceBusLogicAppConnectionString]$($json.ServiceBusLogicAppConnectionString.value)"

Was this article helpful?