Whether you are developing for web or windows. Replacing the configuration variables persisted in the web/app.config is always a pain during the build and deployment process using TFS. In this walkthrough we’ll learn how to use the “ReplaceTokens” task during build and deployment to update the configuration easily.

Pre-Requisites

This walk through assumes you already have a web based project set up in Team Foundation Server or Visual Studio Team Services.

Walkthrough

  • Open Team Foundation Server and navigate to the marketplace by clicking on the ‘shopping bag’ icon from the top right corner in the navigation bar.

    NavigateToMarketplace

  • In the extension marketplace search for replace tokens, download and add this extension to TFS. You can find the replace token extension by browisng to ReplaceToken Task in Marketplace. By default collection administrators have permissions to approve extensions added to a team foundation server.

    ReplaceTokenExtension

  • Navigate to the build hub and create a new build definition. In the example build pipeline below, I have used the visual studio build template that comprises of build, test and package.

    DefaultBuildPipeline

  • Click the add build step link to add the replace tokens task to the build pipeline. The target files field accepts a wild card keyword, the default will look for all config files in your repository. As highlighted in the advanced section, you can change the prefix and suffix value based on the convention used in your project.

    ReplaceTokenTask

  • Let’s look at the config file I am planning to update with this replace tokens task. As you can see in the below screen shot, I am looking to replace the appSetting key “Variable1” the current value of variable1 key is “#{Variable1}#”.

    AppConfigVariable

  • In order to replace the value of variable1 in the web.config, I am going to configure the “replace token” configuration.

    BuildTokenVariableReplace

  • Now navigate to the variables tab in your build definition and add a new variable that matches the name of the value field in the key under appSettings file that you are planning to replace.

    ReplaceTokenVariableExample

  • Save the build definition and queue a build. The replace token task should execute and update the logs. See screen shot below.

    BuildOutputReplaceToken

  • To ensure that the configuration file has been successfully updated, navigate to the folder agent working folder in the server D:\DCDWHTFSB01_A1_work\7\s\Main\Source\Edft.Web.UI and open the web.config file. As indicated in the screen shot below the value of the key “Variable1” has successfully been updated by the variable specified in the build definition.

    ReplaceTokenLogFileOutput

  • This task can be used during build or release. The results will be the same.

The great benefit of this task is that you don’t need to separately manage the variable values in config files or parameter files, instead you can simply update the values directly from the build definition by maintaining the master copy of the variables in the build definition directly. What’s even cooler is that the build definition allows you to store variables encrypted as well. This is ideal for passwords and sensitive values that now don’t need to reside in configuration files and can instead directly be applied from build definitions directly.


About author
Tarun Arora
Tarun Arora
Tarun Arora is obsessed with high-quality working software, DevOps, Continuous Delivery and Agile. His core strengths are Azure, Azure DevOps, PowerShell, SQL and WPF. He is a Microsoft MVP in Visual Studio Development Tools and the author of 'DevOps & ALM with TFS 2015'.
We Are
  • onlyutkarsh
    Utkarsh Shigihalli
    Microsoft MVP, Technologist & DevOps Coach


  • arora_tarun
    Tarun Arora
    Microsoft MVP, Author & DevOps Coach at Avanade

Do you like our posts? Subscribe to our newsletter!
Our Book