Deployment web site Zip with MsBuild and TeamCity


One of the most useful practices that I’ve been using during the last years is continuous integration and for that purpose, TeamCity is the tool of choice, which offers a free professional version with up to 20 users and 20 build configurations, enough for a small team.

TeamCity Logo

Recently I tried (again, first time I found some trouble) to create an MsBuild script to make a web publication to a folder an then zip that folder in order to make a simple distributable with all the things needed to set up our web project that anyone in the organization could download and install, instead of having to create that zip myself or publishing through Visual Studio.

Finally, I got the script and seems to work using MsBuildCommunityTasks and some community knowledge.

The first part deals with publishing the site:

  <PropertyGroup>
    <OutputFolder>$(MSBuildProjectDirectory)\publish</OutputFolder>
    <TempFolder>$(MSBuildProjectDirectory)\temp\</TempFolder>
  </PropertyGroup>
  <Target Name="PublishSite">
    <Message Text="Publishing in folder: $(OutputFolder)" />
    <RemoveDir Directories="$(OutputFolder);$(TempFolder)" ContinueOnError="true" />
    <MSBuild
      Projects="src\Adapting.Web\Adapting.Web.csproj"
      Targets="ResolveReferences;_WPPCopyWebApplication"
      Properties="Configuration=Release;WebProjectOutputDir=$(OutputFolder);OutDir=$(TempFolder)"
      StopOnFirstFailure="true" />
    <RemoveDir Directories="$(TempFolder)" ContinueOnError="true" />
  </Target>

I define some variables and then I call the _WPPCopyWebApplication which is the important target (in .NET 2.0 was _CopyWebApplication). This should be the same as publishing a web site to a folder using the wizard.

Then, I get the files in the OutputFolder and make a zip with them:

  <Target Name="PackageSite" DependsOnTargets="PublishSite">
    <ItemGroup>
      <PublishFiles Include="$(OutputFolder)\**" />
    </ItemGroup>
    <Message Text="Packaging Site" />
    <Zip
      Files="@(PublishFiles)"
      ZipFileName="release-web.zip"
      WorkingDirectory="$(OutputFolder)"
      />
  </Target>

I simplified this a little bit because I have as well a target that generates the build number from SVN and changes the AssemblyInfo.cs and uses this numbers to name the zip.

In TeamCity, you can create a new configuration with *.zip as artifacts, that points to your SCM (Subversion in my case) and the most important thing is that uses the MsBuild runner targeting PackageSite.

The only thing that was that if I put the <ItemGroup> section out of the <Target>, works on the command line but not inside TeamCity.

That’s all, I hope this helps.

Sources:

Related Posts

Bye bye Kurobox

A deserved farewell to a device that has worked flawlessly for so many years

DIY Arduino Christmas tree lights

A detailed explanation of my home made light switcher made with Arduino and a relay shield

Back to blogging

My statement on getting back to blogging. Will this go anywhere?

Lessons learned optimizing MySQL

A summary of some things I discovered while trying to optimize the performance of a production MySQL server.

Simple Mini Profiler Glimpse plugin

I just created my first Glimpse plugin, integration between Mininprofiler and Glimpse.

Carbon Copy Cloner saved my day

Just a gratitude post because this piece of software was extremely useful when others don't. And it was my birthday.

Troubleshooting MSDTC, RPC and NServiceBus issues

MSDTC is rare and can cause many headaches. This is the process I followed to track down the problems I was having

Using fail2ban with nginx in Debian

fail2ban helps you fight spam and bots but comes with an Apache sample. I converted it to handle Nginx information.

How msiinv saved my day

mssinv is a tool to manage MSI installed packages and I used it to track a problem with a package partially installed, that had to be removed with this tool

Install ASP.NET MVC 3 Manually

Due to some strange problems in my PC, I had to install this package manually and this is applicable to many other installers