IF-Blueprint Blog

Passing variables from ARM deployments to Azure DevOps tasks

Deploying Azure components in an automated way is simple, thanks to Azure Resource Manager templates – or short: ARM templates. You can define which Azure components you want to provision and how they should be configured. However, this blog post is not about ARM templates itself, if you like to read more about them go here.

If you use Azure DevOps to deploy your ARM templates, there might be a situation where you need values in other Azure DevOps tasks. Those values might be generated during the deployment or you just want to specify them only once.

For example, if you provision an Azure App Service by using an ARM template and you want to deploy an ASP.NET application after the deployment to that App Service, you need the name of the App Service. This name might contain a random generated string, for the example, there could be an ARM variable:

In this example the App Service name contains a hash string which depends on the resource group it gets deployed to. This hash might look like tcvhiyu5h2o5o.

Getting such a variable into another Azure DevOps tasks requires only 2 steps.

Create an ARM output variable

ARM templates allow to save certain values as output variables. Those variables can be inspected in a deployment in the Azure Portal.

View of the output variables for a certain deployment in the Azure Portal

Creating such an output variable is easy, just create an “output” section in the ARM template. To create the “AppServiceName” variable from the screenshot above, we just need to define a json literal object with the name, the type this variable has and of course its value:

After having defined the output variable for the ARM deployment, we need to get this variable into our Azure DevOps Release Pipeline.

Reading ARM output variables in Azure DevOps

The Release Pipeline in Azure DevOps should deploy the ARM template to Azure and afterwards some binaries to the Azure App Service defined in the ARM template. This would boil down to mainly 2 steps in our Release Pipeline, except that we need another step for reading the output variable:

Azure DevOps Release Pipeline

The first step is of course the deployment of the ARM template. This is straightforward, except that we need to specify a name for this deployment. This is necessary so we can retrieve the output variable in the second step. You can specify the name of the deployment under “Advanced”.

I choose the release name as the deployment name, which has the advantage of easily identifying which deployment belongs to which release.

After having done the deployment under a certain name, we can read the output variable in the second step. This step is a “Azure PowerShell” step with an inline PowerShell code:

As you can see, the Cmdlet “Get-AzResourceGroupDeployment” needs a “Name” parameter, where we specify the same name as in the first step.  The last line just assigns the value from the output variable to an environment variable. This way we can use it in any follow up release step.

The last step is the App Service deployment. This step requires the name of the App Service the binaries should be deployed to. Here we can just use the previously created environment variable.

Conclusion

ARM output variables allow to streamline deployments with the help of Azure DevOps Release Pipelines. You can specify different aspects of your Azure infrastructure in one place and can use them for further deployment tasks in a Release Pipeline without having to use duplicate values.