Sunday, 14 June 2009

Outputting the Subversion revision into your web page

A few months ago when I stared using stackoverflow I noticed that they output the current Subversion revision number into their html source.

Stackoverflow revision number

This is both cool and useful and as I’m about to become a SVN Nazi and make sure that all new projects at work are placed under source control (only a few are at present) I wanted to know how to do it.

Getting the tools

Most of our projects are built using ASP.NET so I wanted to know how to get this working from within Visual Studio. Thankfully this didn’t take long, a few Google’s turned up MSBuild Community Tasks Project hosted over at who are the good folks behind Subversion and many many Subversion tools. I had a quick look over the list of tasks and spotted the “SvnVersion” task and thought that sounds about right. I downloaded and installed the Community Tasks.

I also downloaded the Visual Studio Web Deployment projects after having read ScottGu’s blog post regarding them. You can get the VS2008 version here or the VS2005 version here if you need it.

Getting the revision into your HTML

To get the revision number into my HTML I chose to add a Web Deployment project (right click on your website/web app in the solution explorer and “Add web deployment project” then right click on this new project and “Open project file”. What you’ll see now is the XML of the MSBuild file and towards the bottom of this file should be a comment with four target tags in it like this.

<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.WebDeployment.targets.
<Target Name="BeforeBuild">
<Target Name="BeforeMerge">
<Target Name="AfterMerge">
<Target Name="AfterBuild">

We’re going to use the AfterBuild target so to place the revision number in a little bit of HTML that’s going to live in a master page, so let’s set up the master page first.

My site is a .NET MVC application and I’m going to add the revision number to the Site.Master. To do this I added the following HTML to the footer of the master page.

<span id=”rev”>revision REVISION</span>

The SvnRevision task will get the revision number from our working copy and store it in a property name that we define.

We can then use another Community Task, FileUpdate, to update the master page after the build.

Here is the XML of the AfterBuild target looks, note also that before the target I have imported the Community Tasks that we downloaded and installed earlier.

<!-- Import the community build tasks-->
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<target name="AfterBuild">
<! -- Add the SvnVersion build task -->
<svnversion toolpath="$(ProgramFiles)\VisualSVN\bin" localpath="$(MSBuildProjectDirectory)">
<output propertyname="Revision" taskparameter="Revision" />
<fileupdate replacementtext="revision $(Revision)" regex="revision REVISION" files="$(Configuration)\Views\Shared\Site.Master" />

The FileUpdate finds the string defined in the regex attribute and replaces it with the string in the replacementText attribute. $(Revision) is MSBuild syntax for a property (like a variable in other languages) where “Revision” is the property name. The files attribute tells the fileUpdate action which file to work with when doing this replacemnet. In this example $(Configuration) is a property that the build engine creates which contains the name of the current build configuration (Debug or Release are the defaults).

So to see this in action, right click on the Web Deployment Project and click Build. Then navigate to the solution folder. Inside that folder should be another folder named the same as your Web Deployment Project and in there, a folder for what ever configuration you had selected in Visual Studio (Debug is the default). Open this folder then browse to the folder containing the master page and open the master page in your favourite text editor. Find the span from the earlier step and you should see that the uppercase “REVISION” has been replaced with the current revision of your working copy.


  1. Fіrst οff Ӏ wοuld like to say excellent blog!

    I haԁ a quicκ questiоn that I'd like to ask if you don't mind.
    I waѕ іnterestеd tо know how
    уοu centег yοuгѕelf and cleаr your minԁ prіor to ωrіting.
    I haѵe hаd a hard tіme cleаring my minԁ in getting my iԁeas out there.
    I tгuly dο enjoy wгiting but іt just
    sееms like the fiгѕt 10 tο 15 minutes aгe usually lоѕt sіmplу just trying
    to figuгe оut how tο begin. Any suggestіons oг
    hіnts? Cheers!

    Revіew my wеbsitе; epoxy garage floor phoenix

  2. I am sure this post has touched all the internet people, its really
    really good post on building up new blog.

    my website; SEO Training

  3. I savour, cause I discovered exactly what I was having a look for.
    You've ended my four day lengthy hunt! God Bless you man. Have a great day. Bye

    My web site :: unblocked games

  4. This is among the most common design and almost usually in black.
    These include the most appealing parts in a very human body.

    The other well-liked Hawaiian Flower Tattoos designs, which
    might be received well by tattoo lovers, are hibiscus and orchid.

    Apart from cosmic tattoo designs, artists like to draw
    designs for contests with this genre and exhibit their special
    talents. In earlier times these sorts of body art were considered
    a sign of rebellion, but as more and much more everyone is
    getting tattoos and piercings, body art will be considered a suitable portion of modern life.

    Review my webpage :: Angel Tattoos