hg revision number as environment variable


Does TeamCity define a variable (like %build.vcs.number%) that is the hg revision number? It doesnt appear to be on the list.

I have tried adding my own environment variable under 'Build Parameters'

Name:   env.hg_revision_number
Value:   hg id -n

and am referencing it however TeamCity interprets it literally and outputs 'hg id -n' instead of running that command and putting the result into that variable.

Eg, when I use %env.hg_revision_number% the value output is  'hg id -n' instead of '27' or whatever the revision number is.

How can I acheive this?

Comment actions Permalink

Hi Wallace

In Case of Mercurial build.vcs.number parameter contains a hash.
Custom variables can contain static values or refer to other properties, but the server does not execute external commands here.
You could run hg inside the build - if checkout is performed on agent side, and the agent has a complete working copy.
Keep in mind these revision numbers are not reliable, and can be changed if you switch VCS root to another host.


Comment actions Permalink

Hi Michael, Thanks for your response.

Firstly, I'm not sure what you mean by this:
>In Case of Mercurial build.vcs.number parameter contains a hash.

As you've said the custom variables can only contains static values, HOWEVER At some stage TeamCity must do some sort of execution to put the actual revision number inside the build.vcs.number variable.

So I guess what i'm asking is given TeamCity can inject dynamic properties (build.vcs.number) is there some way to have our own?

We are doing server-side checkout.  I am aware of the short-comings of Hg revision numbers with respect to being unique only to a particular instance of the repo.


Comment actions Permalink


Yes, the parameters are resolved when a build starts, but there is no easy way to add a custom action there.

If you explain a business goal behind the questionб we could suggest alternate approaches to achieve them.


Comment actions Permalink

Hi Michael,

We want the revision number to go in the AssemblyInfo information of our compiled C# assembly.

We used to have


which for SVN worked because the VCS number for SVN is numeric. eg, 2.0.345.1012

Now the switch to Mercurial you get something like:


where the latter bit can't go in to AssmeblyInfo attribute as it must be numeric.

We are storing the Hg VCS number in the 'AssemblyProduct` attribute now, but still want to populate AssemblyInfo with




Please sign in to leave a comment.