MSBuild is a powerful tool for automation but as the name implies, it is a tool primarily for compiling code. Beyond the initial build MSBuild can also be used in preparation for deployments. To assist with the deployment of websites Microsoft produced Web Deployment Projects which carry out a series of tasks to build a website with the option to replace configuration sections which is a very useful feature. Once I have compiled a project I have multiple deployment targets so I wanted to just reconfigure a project in preparation for a deployment and I found a good way get this done outside of a web deployment project.
Replacing configuration sections is handled by the
ReplaceConfigSections task which can be accessed in the
Microsoft.WebDeployment.Tasks.dll assembly. You just have to pull it into your own script with the
UsingTask directive which is shown in the example script.
In the script below
WDTargetDir property defines the directory where the
Web.config file must exist. The next two properties are optional and are self explanatory. Finally the items in the
WebConfigReplacementFiles group which are processed when the
ReplaceConfigSections task is executed. The configuration file for the replacement section can be a relative path set by the
WDTargetDir property. This sample shows how
appSettings.config is used when the
$(Configuration) is not defined and
Release is defined. This distinction is controlled with the
I am now making use of the
ReplaceConfigSections task as a part of my continuous integration process which employs a build server which is integrated with my source control system. When I commit my updates the build server pulls the updates, compiles and tests my changes. Once I know the project has been built successfully and has passed my tests I can then safely run the deployment task for the target platform with various targets like development, staging and production. With this automation in place it makes it possible to eliminate human error and a good deal of manual work.