Per-environment config value tokenization for Azure Web Apps using VSTS Release Management

Published on
Reading time
Authors

For the majority of the last ten years I've been working with delivery of solutions where build and deployment comes from some centralised location.

When Microsoft made InRelease part of TFS as Release Management, I couldn't wait to use it. Unfortunately in its state at that time the learning curve was quite steep and the immediate value was outweighed by the effort to get up and running.

Roll forward to 2016 and we find Release Management as a modern, web-based feature of Visual Studio Team Services (VSTS). The cherry on the cake is that a lot of the learning curve has dropped away as a result.

In this post I'm going to look at how we can deploy a Web Deploy (or MS Deploy) packaged Web Application to an Azure Web Application and define different deployment environments with varying configurations.

Many people would apply configuration transformations at build time, but in my scenario I want to deploy the same compiled package to multiple environments without the need to recompile anything.

My Challenge

The build definition for my Web Application results in a package that allows it to be deployed to an Azure Web App by Web Deploy. The result is the web.config configuration file is in a zip file that is transferred to the server for deployment by Web Deploy.

Clearly at this point I don't have access to the web.config file in the drop folder so I can't transform it with Release Management. Or can I?!

Using Web Deploy Parameters

Thankfully the design of Web Deploy provides for the scenario I described above though use of either commandline arguments or a specially formatted input file that I will call the "SetParameters" file.

Given this is a first-class feature in the broader Microsoft developer toolkit, I'd expected that there would be a few Tasks in VSTS that I could use to get all of this up and running... I got close, but couldn't quite get it functioning as I wanted.

Through the rest of this post I will walk you through the setup to get this going.

Note: I am going to assume you have setup Build and Release Management definitions in VSTS already. Your Build should package to deploy to an Azure Web App and the Release Management definition to deploy it.

VSTS Release Management Setup

The first thing to get all of this up and running is to add the Release Management Utilities extension to your subscription. This extension includes the Tokenizer Task which will be key to getting the configuration per-environment up and running.

You also need to define an "Environment" in Release Management for each deployment target we have, which will also be used as a container for environmental configuration items to replace at deployment time. A sample is shown below with two Environments defined

Environments

We'll come back to VSTS later, for now, let's look at the project changes you need to make.

Source Project Changes

For the purpose of this exercise I'm just worrying about web.config changes.

First of all, you need to tokenise the settings you wish to transform. I have provided a sample below that shows how this looks in a web.config. The format of two underscores on either side of your token placeholder is required.

web.config
<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="LoggingDatabaseAccount" value="__docdburi__" />
    <add key="LoggingDatabaseKey" value="__docdbkey__" />
    <add key="LoggingDatabase" value="__loggingdb__" />
    <add key="LoggingDatabaseCollection" value="__loggingdbcollection__" />
  </appSettings>
</configuration>

The next item we need to do is to add a new XML file to our Visual Studio project at the root level. This file should be called "Parameters.xml" and I have included a sample below that shows what we need to add to if it we want to ensure we replace the tokens in the above sample web.config.

You'll notice one additional item in the file below that isn't related directly to the web.config above - the IIS Website name that will be used when deployed. I found if I didn't include this the deployment would fail.

parameters.xml
<parameters>
  <parameter name="IIS Web Application Name" description="IIS Web Application Name" defaultvalue="__iisappname__">
    <parameterentry kind="ProviderPath" scope="IiSApp" match="@defaultvalue"/>
  </parameter>
  <parameter name="LoggingDatabaseAccount" description="Main Document DB Account" defaultvalue="__docdburi__">
    <parameterentry kind="XmlFile" scope="\\Web.config$" match="/configuration/appSettings/add[@key='LoggingDatabaseAccount']/@value"/>
  </parameter>
  <parameter name="LoggingDatabaseKey" description="Key to access Main Document DB Account" defaultvalue="__docdbkey__">
    <parameterentry kind="XmlFile" scope="\\Web.config$" match="/configuration/appSettings/add[@key='LoggingDatabaseKey']/@value"/>
  </parameter>
  <parameter name="LoggingDatabase" description="Document DB Id" defaultvalue="__loggingdb__">
    <parameterentry kind="XmlFile" scope="\\Web.config$" match="/configuration/appSettings/add[@key='LoggingDatabase']/@value"/>
  </parameter>
  <parameter name="LoggingDatabaseCollection" description="Document DB Collection" defaultvalue="__loggingdbcollection__">
    <parameterentry kind="XmlFile" scope="\\Web.config$" match="/configuration/appSettings/add[@key='LoggingDatabaseCollection']/@value"/>
  </parameter>
</parameters>

When you add this file, make sure to set the properties for it to a Build Action of "None" and Copy to Output Directory of "Do not copy".

Note: if you haven't already done so, you should run a Build so that you have Build Artifacts ready to select in a later step.

Add the Tokenizer to your Release Management Definition

We need now to return to VSTS' web interface and modify our existing Release Management definition (or create a new one) that adds the Tokenizer utility to the process.

You will need to repeat this so all your environments have the same setup. I've shown how my Test environment setup looks like below (note that I changed the default description of the Tokenizer Task).

Release Management Definition

Configuration of the Tokenizer is pretty straight forward at this point, especially if we've already run a build. Simply select the SetParameters.xml file your build already produced.

Tokenizer setting

Define values to replace Tokens

This is where we define the values that will be used to replace the tokens at deployment time.

Click on the three dots at the top right of the environment definition and from the menu select "Configuration variables..." as shown below.

Variable Definition

A dialog loads that allows us to define the values that will go into our web.config for this environment. The great thing you'll note is that you can obfuscate sensitive details (in my example, the key to access the Document DB account). This is non-reversible too - you can't "unhide" the value and see the plain-text version.

Token Values

We're almost done!

Explicitly select SetParameters file for deployment

I'm using the 3.* (preview) version of the Deploy Azure App Service Release Management Task, which I have configured as shown.

App Service Task

At this point, if you create a new Release and deploy to the configured environment you will find that the deployed web.config contains the values you specified in VSTS and you will no longer need multiple builds to send the same package to multiple environments.

Happy Days! 🙂