Search code examples
powershelltfsbuild-processtfs-2015release-management

web.config changes via TFS 2015 Release Management


In the past I've using web.config transforms when manually deploying code to set environment specific setting values and attributes. I am transitioning from environment specific manual builds to a single TFS 2015 Build deployed to multiple environments via Release Management. Environment specfic application settings values configured in the web.config are tokenized. This method essentially inserts tokens into setting values during the build process. When deployed the tokens are replaced with matching Release definition configuration values.

This method is insufficient setting attributes of non-settings however. Examples of these transforms include:

<httpCookies requireSSL="true" xdt:Transform="Insert" />
<compilation xdt:Transform="RemoveAttributes(debug)" />
<httpRuntime xdt:Transform="RemoveAttributes(executionTimeout,maxRequestLength,useFullyQualifiedRedirectUrl,minFreeThreads,minLocalRequestFreeThreads,appRequestQueueLimit,enableVersionHeader)"/>
<httpRuntime enableVersionHeader="false" maxRequestLength="12288" xdt:Transform="SetAttributes"/>
<customErrors mode="On" xdt:Transform="SetAttributes"/>

What is the best way to update these attributes during release?


Solution

  • Both Web Deploy's parameters.xml method and transforms can be used with Release Management. Transforms would be triggered from Build and the process of replacing tokens created by a publish would be triggered by Release Management.

    To trigger transforms during the build, you can do this one of two ways:

    1. Add the following MSBuild parameters to force the transformation to happen during the build

      /p:UseWPP_CopyWebApplication=true /p:PipelineDependsOnBuild=false

    2. Create a publish profile using the MSDeploy Package option and then trigger the packaging in Build using the following MSBuild parameters:

      /p:DeployOnBuild=true /p:PublishProfile=[nameOfProfile]

    Either of the above methods will cause normal Web.config XDT's to run. If you need other XML files to be transformed, you'll need to first install SlowCheetah.

    Token Replace and Parameters

    Now that you have a build artifact with XDT's run, you can use token replacement and the WinRM tasks from Release Management. These will take the Web Deploy package from the Build and execute the SetParameters command before deploying it. The trick is to take the SetParameters.xml file and run a token replace on it first, swapping out Release environment variables first.