Staging an ASP.NET application with MSBuild
I spent some time this weekend hacking around our build process for Bundl.it and wanted to share a small success.
We have three folks tending the bundl.it garden, one in Jersey, one in the UK, and myself in Manitoba. We mostly use Google Wave and twitter to stay in touch and have just started using http://agilezen.com to track our work items. All that to say that our communication is largely asynchronous which can make scheduling deployments a bit tricky so I’ve been working on a one-click (or one batch file) deployment for our ASP.NET MVC app.
We’re using subversion for source control. Those .svn files don’t belong on the server, though, and we don’t really need any .cs code files sitting around either so the first thing we do is use an MSBuild script to build our ASP.NET MVC project to a local staging folder.
First we include the Microsoft.WebApplications.Targets file: [xml firstline=”7”] <Import Project="c:/program files/MSBuild/Microsoft/ VisualStudio/v9.0/WebApplications/ Microsoft.WebApplications.Targets"/>[/xml] The Stage target looks like this: [xml firstline=”49”] <Target Name="Stage"> <CallTarget Targets="Clean;Build;Test;"/> <Message Text="Trying to stage to $(StageDir)"/> <MSBuild Projects="bundl.it\bundl.it.csproj" Targets="Build" Properties="Configuration=Release; OutDir=.\bin\; WebProjectOutputDir=$(StagingDir)" StopOnFirstFailure="true" /> </Target>[/xml] Let’s go over this one piece at a time. [xml firstline=”50;”] <CallTarget Targets="Clean;Build;Test;"/>[/xml] Calling the Clean, Build, and Test targets ensures that we have a clean build and all automated tests have passed. [xml firstline=”51;”] <Message Text="Trying to stage to $(StageDir)"/>[/xml] The message task logs our intention. [xml firstline=”52;”] <MSBuild Projects="bundl.it\bundl.it.csproj" Targets="Build" Properties="Configuration=Release; OutDir=.\bin\; WebProjectOutputDir=$(StagingDir)" StopOnFirstFailure="true" />[/xml] Finally, this MSBuild task is where the staging magic happens. The “OutDir=.\bin\” property tells MSBuild to compile our ASP.NET application into a DLL and place bundl.it.dll in our app’s /bin folder, then the WebProjectOutputDir property tells MSBuild to copy our project into the $(StagingDir) folder.
The end result in our StagingDir looks a lot leaner:
Here you can see that all the .svn folders are gone, as are all the .cs code files. In fact, only those items from the project that are designated as “Content” have been copied over.
The staging folder is now ready for a clean and simple FTP Sync operation to get our content into production.