SHIFT-WIKI

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


parsebicepoutputinpipeline

Parse Bicep Output in Azure DevOps Pipeline

In Azure ARM and YAML Project I already showed how to parse the output in a pipeline for ARM templates using Powershell. I now have a use case for parsing the output from BICEP in a pipeline using bash.

So we will first do a bicep deployment in which we will parse the output and then show how you can find the variable in the next task.

Deployment Task

- task: AzureCLI@2 
  displayName: Set
  inputs:
    azureSubscription: $(azureServiceConnection)
    scriptType: bash
    scriptLocation: inlineScript
    inlineScript: |
      set -e
      az account set --subscription $(subName)
      deploymentSuffixAttempt=$(deploymentSuffix).$(System.StageAttempt).$(System.JobAttempt)
 
      echo '##[Section]Deploy vnet & private endpoint network config'
 
      az deployment group create \
        --resource-group $(rgName) \
        --name "hub-vnet-$deploymentSuffixAttempt" \
        --template-file ./hub/hub-vnet.bicep \
        --parameters deploymentSuffix=$deploymentSuffixAttempt \
        --parameters locationShort=$(regionShort) \
        --parameters subscriptionId=$(subName) \
        --parameters environment=$(environment) \
        --parameters hubVnetName=$(vnetName) \
        --parameters azureFirewall=$(AzureFirewallIP)
 
      echo 'Parse deployment output naar Azure DevOps & Bash variabelen'
 
      deploymentOutput=$(az deployment group show \
        --resource-group $(rgName) \
        --name $(vnetName)-$deploymentSuffixAttempt \
        --query properties.outputs | jq -c 'to_entries[] | [.key, .value.value]')
      while IFS=$'\n' read -r c; do
        outputname=$(echo "$c" | jq -r '.[0]')
        outputvalue=$(echo "$c" | jq -r '.[1]')
        echo "Log variabele: $outputname : $outputvalue"
        declare $outputname=$outputvalue 
        echo "##vso[task.setvariable variable=$outputname;isoutput=true]$outputvalue"
      done <<< "$deploymentOutput"

Show

In this task you can see the variable, but watch it, the output name will have a prefix of “AZURECLI4_”. So if the var is called “varName” it will be available as “AZURECLI4_varName”

- task: AzureCLI@2 
  displayName: Check
  inputs:
    azureSubscription: $(azureServiceConnection)
    scriptType: bash
    scriptLocation: inlineScript
    inlineScript: |
      set -e
 
      #Temp: check for thevar
      printenv | sort
parsebicepoutputinpipeline.txt · Last modified: 2023/05/22 09:20 by sjoerd