Replace ConnectionString values using transformations in Team Services

First off, check out this great overview https://blogs.msdn.microsoft.com/devops/2017/04/05/managing-configuration-app-settings-for-multiple-environments-in-your-cd-pipeline/

And be aware of how stinky-easy this is when deploying to Azure web sites.  Note: Creation of variables in Team Services, and slot settings in Azure are typically done by an Operations or Infrastructure/Deployment team, *not* by developers :).

And also for IIS apps https://www.visualstudio.com/en-us/docs/build/apps/cd/deploy-webdeploy-iis-deploygroups#environment-specific-configuration

 

But what about other app types that don't have tasks that incorporate transforms?  It's still easy, by using Colin's ALM Build and Release Tools task. Specifically, the Replace Tokens task.

 

Here's a quick walkthrough, using a connection string value as an example.

  1. Create a variable in each environment with the name/value pair for the connection string. The "Name" must match the name of our connection string in web.config.
  2. Add the "Replace Tokens" task to the release. This will search all files with a .config extension and perform a match against any "names" that match an environment variable.
  3. In order to retain the ability to do easy debugging locally, keep the appropriate connection string setting in the web.config.
  4. Create "Debug" and "Release" configs that will tokenize the connection string when it is built by VSTS. In our case, it will replace the actual connectionstring value with "__DefaultConnectionString__" so that the replace tokens task from #2 will be able to find/replace it.

That's it!  Debug locally using a local database (or whatever is appropriate), and have releases flow into the release pipeline where all other connection strings will be populated based on the environment in Team Services, or the slot setting in Azure.

Easy!