Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Team City environment variable to override a project property

I have a C# project property called Version defined as

<Version Condition="$(Version)==''">1.2.3.4<Version>

1.2.3.4 is the default value.

I have a Team City system property, also called Version, set up to override. So in the custom run dialog in Team City, I can specify a value for Version and that value gets used. This works fine.

If I leave the parameter blank in Team City, however, the default value is still overwritten with blank (null?). If I delete the Team City parameter, the default value is used.

Is the condition incorrect? How can I set up the Team City property to be blank, and only override if I enter some value?

like image 286
jbemt48 Avatar asked May 25 '11 19:05

jbemt48


2 Answers

Updated answer after OP's comment:

From docs:

MSBuild allows you to set properties from the command line using the /property or /p command line switch. Property values received from the command line override property values set in the project file and property values inherited from environment variables.

So you can just set a property $(VersionTC) in TeamCity configuration and check if that property is empty or not and set version

<Version>$(VersionTC)<Version>
<Version Condition="'$(VersionTC)'==''">1.2.3.4<Version>

( so you set Version to VersionTC first. Then see if it empty and set the default )

Have a look at this blog post explaining all this.


Try something like below:

<Version Condition=" '$(Version)'=='' ">1.2.3.4<Version>

Note the ' ' (single quotes) around $(Version)

like image 110
manojlds Avatar answered Oct 19 '22 07:10

manojlds


Team City is probably still passing the parameter on the command line, just with a blank value, as in,

/p:Version=""

or something similar. The symptom you are seeing is due to how MSBuild deals with overridden properties. When specified on a command line, a property will take that value whether or not it is also declared in a static (global in the file, not inside a target) PropertyGroup declaration. So your declaration of Version, with the Condition being checked for teh empty string, is being skipped entirely.

One way around this is to move your PropertyGroup containing the declaration of $(Version), with its Condition, inside the target where it is first used. MSBuild will allow overwriting the value of a command line property from a "dynamic" property created at runtime from within a target.

If you run this command line...

> msbuild My.proj /t:Ver /p:Version=""

...and have this target...

<Target Name="Ver">
  <PropertyGroup>
    <Version Condition="'$(Version)' == ''">1.2.3.4<Version>
  </PropertyGroup>
  <Message Text="Version: '$(Version)'" />
</Target>

... you will get Version showing 1.2.3.4, whereas with the PropertyGroup outside the target, it will retain the empty value.

like image 4
Brian Kretzler Avatar answered Oct 19 '22 06:10

Brian Kretzler