次の方法で共有


Parsing your ARM Template Output in VSTS Pipelines

Did you know you can store your ARM template output in a VSTS variable and parse that data for later usage in your VSTS Release Pipeline?

If you are using the Azure Resource Group Deployment Task in your VSTS Release Definitions you now have the option to store the output values from your ARM template in the Deployments Output section.

In this blog post I explain how you can use the Deployments Output values in the rest of your Release Pipeline.

ARM Template

Before we can start, we first need to have an ARM Template which some output. In this example I don't deploy any Azure Resources I just want to output something.

armtemplate.json

 {
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstName": {
      "type": "string",
      "metadata": {
        "description": "The first name of user"
      }
    },
      "lastName": {
        "type": "string",
        "metadata": {
          "description": "The last name of user"
        }
      }
    },
  "variables": {},
  "resources": [],
  "outputs": {
    "firstNameOutput": {
      "value": "[parameters('firstName')]",
      "type": "string"
    },
    "lastNameOutput": {
      "value": "[parameters('lastName')]",
      "type": "string"
    }
  }
}

This ARM Deployment has 2 parameters for firstName and lastName.

armtemplate.parameters.json

 {
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "firstName": {
      "value": "Stefan"
    },
    "lastName": {
      "value": "Stranger"
    }
  }
}

When you deploy above ARM Template you would see the following output being shown in the Azure Portal.

PowerShell script

For parsing the output of the ARM Template we are going to create a PowerShell script.

Folder view in Visual Studio:

Parse-ARMOutput.ps1:

 param (
    [Parameter(Mandatory=$true)][string]$ARMOutput
    )

#region Convert from json
$json = $ARMOutput | convertfrom-json
#endregion

#region Parse ARM Template Output
Write-Output -InputObject ('Hello {0} {1}' -f $json.firstNameOutput.value, $json.lastNameOutput.value)
#endregion

Build and Release Definition

After committing the ARM Template and script files to your Repository it's time to create a Build and Release Definition in Visual Studio Team Services (VSTS).

Build Definition:

Release Definition:

Make sure you define Deployment outputs variable in the Azure Resource Group Deployment VSTS Task. This value is going to be used in the next step of the Release Pipeline.

Create a new Release Task for parsing the output from the Deployment Outputs section using the PowerShell script created earlier.

PowerShell VSTS Task

For parsing the Deployment Outputs from the Azure Resource Group Task we are using the PowerShell Task from VSTS.

When you now trigger a release you see that the output from the ARM Template deployment is being parsed in the PowerShell Script VSTS task.

Hope you find some more scenario's in which you can use the Deployment Outputs option from the Azure Resource Group Task.

References:

Comments

  • Anonymous
    July 31, 2018
    You saved my day! Exactly what I was looking for! Thanks Stefan!