如何将 SVN 修订号与 ASP.NET 网站同步?

Stack Overflow 底部有一个 subversion 版本号:

Svn 修订版: 679

我想使用这样的自动版本与我的 .NET Web Site/Application,Windows 窗体,WPD 项目/解决方案。

我如何实现这一点?

9398 次浏览

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:$

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?

$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.

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.

@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.

To add to @BradWilson'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

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)