Automated Web Server Migration with MSBUILD

At work, one of the projects I'm working on has started down the path of testing. Testing is a beautiful thing. It grounds you, it angers you, and it confuses you, but at the end of the day you revisit your product and realise how much of a beautiful thing it's become.

The project I'm dealing with is asp.net based and going to be tested by a team of about 6 test engineers. The general cycle goes test, fix in dev, deploy to test. Even on a day-to-day basis, doing repetitive migration tasks is best avoided. So how can we achieve this?

Through MSbuild of course!

The process of migration is pretty straightforward:

  1. Delete everything out of the Test directory, except for the environment-specific Web.Config
  2. Copy everything from Dev to Test, except the Web.Config

And the script?

<Project DefaultTargets="Migrate"  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

 

    <PropertyGroup>

        <Destination>\\TestServer\d$\MyProject</Destination>

        <Source>\\DevServer\d$\MyProject</Source>

    </PropertyGroup>

 

    <ItemGroup>

        <DestinationFiles Include="$(Destination)\**\*.*" Exclude="$(Destination)\**\Web.Config"/>

        <SourceFiles Include="$(Source)\**\*.*" />

    </ItemGroup>

 

    <Target Name="Clean">

        <!-- Clean files -->

        <Delete Files="@(DestinationFiles)" />

 

        <!-- Clean Directories -->

        <Exec Command="for /f %%i in ('dir /b/ad') do rd /q/s %%i" WorkingDirectory="$(Destination)" />

    </Target>

 

    <Target Name="Copy">

        <CreateItem Include="$(Source)\**\*.*" Exclude="$(Source)\**\Web.Config">

            <Output TaskParameter="Include" ItemName="SourceFilesToCopy" />

        </CreateItem>

 

        <Copy

            SourceFiles="@(SourceFilesToCopy)"

            DestinationFolder="$(Destination)\%(SourceFilesToCopy.RecursiveDir)"

            ContinueOnError="false"

            />

    </Target>

 

    <Target Name="Migrate" >

        <CallTarget Targets="Clean;Copy;" />

    </Target>

 

</Project>

The "Clean" has two different functions. The first is to go in and delete all of the files that exist on the test server (bar web.config). Next it executes a cmd prompt which iteratively loops through all the directories in the root of the test project web folder, and blows them away using the remove directory (rd) command.

Next it's on to copying the files over from the dev server to test. To do this, the script creates an item array of the filepaths of the files in the dev environment. Using this, it then copies them in their respective hierarchy to the test server. If you do a straight copy without using an item array, you'll end up with all your files in the base folder. You'll also need to ensure you use % when referencing (SourceFilesToCopy.RecirsiveDir), as this tells msbuild to treat it as an array that needs to have the command applied to each item, rather than using $ which will essentially give you a big semi-colon delimited string of your filepaths.

This script alone won't automatically execute. For that you need to schedule it to run. At work we use Hudson which supports CRON jobs which I use to execute this script at midnight each night.

It makes life a lot easier for us developers. Nowadays it's merely a process of working through your bugs during the day, checking your code in, and you can be assured that when you & your testers come in the next day, the Test environment has been updated with all your changes from the previous day ready for more testing.

posted @ Wednesday, November 14, 2007 7:43 AM

Print
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345