views:

2786

answers:

12

stackoverflow.com has a version number at the bottom: "svn revision: 679"

I want to use such automatic versioning with my .NET web, win forms, wpf projects/solutions.

+13  A: 

You can do it by adding the following anywhere in your code

$Id:$

So for example Jeff did:

<div id="svnrevision">svn revision: $Id:$</div>

and when checked in the server replaced $Id:$ with the current revision number. I also found this reference.

There is also $Date:$, $Rev:$, $Revision:$

Nick Berardi
Won't this just insert the revision number of the last change to that particular file?I tried setting up a two-file repository and changed one, and tried both export and checkout, and the other file stayed at 1 in both cases.
Lasse V. Karlsen
lassevk is correct, this only changes when the file itself changes.
Bruno Lopes
Using VisualSVN and TortoiseSVN, adding $Id:$ just caused the page to render "$Id:$". It may be useful to note what server/client this answer works under.
T Pops
You also need to add the the `svn:keywords` Subversion property to the file and make sure it includes the `Id` value. However, it's of little use: this particular file might not have been modified.
Álvaro G. Vicario
+11  A: 

Looks like Jeff is using CruiseControl.NET based on some leafing through the podcast transcripts. This seems to have automated deployment capabilities from source control to production. Might this be where the insertion is happening?

saint_groceon
If you're not using CruiseControl.Net then check out my answer (shameless plug!) - http://stackoverflow.com/questions/163/how-do-i-sync-the-svn-revision-number-with-my-aspnet-web-site#374153 ;o)
Andrew
+10  A: 

As a reply to Nick: You need to make sure that the Files are having the svn:keywords Attribute set as well, otherwise $Id$ etc. are not auto-replaced.

See this page: http://svnbook.red-bean.com/en/1.0/ch07s02.html (Scroll down to svn:keywords)

Michael Stum
+6  A: 

I've just tried this by using $Rev$ and it works - to an extent.

The problem is that it appears to only update the keyword when the file it appears in is changed.

This means that if changes are made to the project which don't affect the file with the $Rev$ in it (in my case, my ASP.NET master page), the project will have a higher revision than gets displayed to the user.

Does anybody have any thoughts on this? Is there a way to get SVN to always update the keywords in the file?

Thanks, - Chris

Chris Roberts
You're right, you need to use a different mechanism.
Lasse V. Karlsen
+2  A: 

Baloon raises a very good point which I am interested to know the answer to.

GateKiller
+5  A: 

@Balloon

$rev and others like it are revisions for the individual files, so they won't change unless the file changes. The number on the webpage is (most likely, I'm assuming here) the svn revision number for the whole project. That is different than the file revisions, which others have been pointing to.

In this case I assume that CCNET is pulling the revision number of the project and rewriting a part of the webpage with that number. Any CI solution should be able to do this, set this up myself with CCNET and Teamcity (although not webpages, but automatic versioning of deployment/assembly versions).

In order for you to do this, use a CI solution that supports it, or use your build process (MSbuild/Nant) to store that version and write it to the files before "deploying" it.

James Pogran

James Pogran
+15  A: 

We do this with xUnit.net for our automated builds. We use CruiseControl.net (and are trying out TeamCity). The MSBuild task that we run for continuous integration automatically changes the build number for us, so the resulting build ZIP file contains a properly versioned set of DLLs and EXEs.

Our MSBuild file contains a UsingTask reference for a DLL which does regular expression replacements: (you're welcome to use this DLL, as it's covered by the MS-PL license as well)

  <UsingTask
     AssemblyFile="3rdParty\CodePlex.MSBuildTasks.dll"
     TaskName="CodePlex.MSBuildTasks.RegexReplace"/>

Next, we extract the build number, which is provided automatically by the CI system. You could also get your source control provider to provide the source revision number if you want, but we found the build # in the CI system was more useful, because not only can see the integration results by the CI build number, that also provides a link back to the changeset(s) which were included in the build.

 <!-- Cascading attempts to find a build number -->

 <PropertyGroup Condition="'$(BuildNumber)' == ''">
   <BuildNumber>$(BUILD_NUMBER)</BuildNumber>
 </PropertyGroup>
 <PropertyGroup Condition="'$(BuildNumber)' == ''">
   <BuildNumber>$(ccnetlabel)</BuildNumber>
 </PropertyGroup>
 <PropertyGroup Condition="'$(BuildNumber)' == ''">
   <BuildNumber>0</BuildNumber>
 </PropertyGroup>

(We try BUILD_NUMBER, which is from TeamCity, then ccnetlabel, which is from CC.net, and if neither is present, we default to 0, so that we can test the automated build script manually.)

Next, we have a task which sets the build number into a GlobalAssemblyInfo.cs file that we link into all of our projects:

 <Target Name="SetVersionNumber">
   <RegexReplace
       Pattern='AssemblyVersion\("(\d+\.\d+\.\d+)\.\d+"\)'
       Replacement='AssemblyVersion("$1.$(BuildNumber)")'
       Files='GlobalAssemblyInfo.cs'/>
   <Exec Command="attrib -r xunit.installer\App.manifest"/>
 </Target>

This find the AssemblyVersion attribute, and replaces the a.b.c.d version number with a.b.c.BuildNumber. We will usually leave the source checked into the tree with the first three parts of the builder number fixed, and the fourth at zero (f.e., today it's 1.0.2.0).

In your build process, make sure the SetVersionNumber task precedes your build task. At the end, we use our Zip task to zip up the build results so that we have a history of the binaries for every automated build.

Brad Wilson
+1  A: 

See this related question:

How do you do version a Web Application?

Zack Peterson
+5  A: 

@Balloon If you are using TortoiseSVN, you can use the packaged SubWCRev program. It queries a working copy and tells you just the highest revision number. Admittedly, this seems to be a client-side approach to a server-side problem, but since it's a nice command line program, you should be able to capture its output for use fairly easily.

nickf
+2  A: 

If you're not using TortoiseSVN for SubWCRev like I said here, you can use svnversion which does the same thing.

nickf
+2  A: 

To add to Brad Wilson's answer: "You could also get your source control provider to provide the source revision number if you want"

To connect Subversion and MSBuild: MSBuild Community Tasks Project

Jan

jan
+3  A: 

If you're using ASP.Net MVC (as StackOverflow does), I've written an easy to follow 3-step guide on how to automatically get and display the latest SVN revision. The guide was inspired by thinking to myself about this very question! :o)

Andrew
Sounds great, Link broken though!
Cookey
Fixed! thanks for letting me know :o)
Andrew