MSDeploy - Transform configs for different teams during a local build

We have the situation where we have two teams working on the same project. Each team has it's own local database server, the connection strings are different.

On build in Visual Studio I would like the correct connectionstrings to be transformed into my /ConnectionStrings.config (it's worth noting I've pulled my connection strings into their own file which lives in the web root).

Web.config

<connectionStrings configSource="ConnectionStrings.config" />

One way to do this is to create a Tasks.build which hooks into Afterbuild Actvity. Here is the contents of this file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
<BuildDependsOn>
  $(BuildDependsOn);
  TransformConnectionStrings
</BuildDependsOn>
  </PropertyGroup>
  <UsingTask TaskName="TransformXml"
                    AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
  <PropertyGroup>
<ConfigurationInput>$(MSBuildProjectDirectory)\..\$(ProjectName)\</ConfigurationInput>
<TransformConfiguration Condition="'$(Configuration)' == 'MyTeam'">MyTeam</TransformConfiguration>
<TransformConfiguration Condition="'$(Configuration)' == 'OtherTeam' Or '$(Configuration)' == 'Debug'">OtherTeam</TransformConfiguration></PropertyGroup>

  <Target Name="TransformConnectionStrings">
<TransformXml Condition="Exists('$(ConfigurationInput)\Configuration\Local\ConnectionStrings.$(TransformConfiguration).config')" Source="$(ConfigurationInput)\Configuration\Local\ConnectionStrings.config" Transform="$(ConfigurationInput)\Configuration\Local\ConnectionStrings.$(TransformConfiguration).config"                                                  Destination="$(ConfigurationInput)\ConnectionStrings.config" />
  </Target>  
</Project>

To run this script on build unload your website project and add this line:

<Import Project="..\Build\Tasks.build" />
<Target Name="BeforeBuild">
     </Target>

The last thing do do is create a configuration setting for each team in VisualStudio.

For a local build you would usually build with project confguration set to 'Debug'.

Open the Configuration Manager dropdown in VS and Click The Configuration Dropdown to add a Configuration.

Configuration Manager

Check to add new Project configurations and copy settings from debug.

Next to each project put each back to Debug except for your Website project.

Repeat these steps for each other team you have.

You should build using the solution configuration selected for your team.

Dave Leigh

Web, and long time Sitecore developer based in Bristol, UK, working at Valtech - valtech.co.uk - @valtech.
I occasionally do other things too.