Search code examples
c#msbuildbuild-automationmsbuildextensionpack

How to determine build origin with MSBuild Script?


I am using MSBuild along with MSBuild Extensions and I am looking for a way to determine what machine my build is being conducted on.

I want to rev part of our version number based on this, so a developer can tell if a build was conducted on a dev machine, a production build box, et. al.

I think using $(COMPUTERNAME) would be a good start, but how might I do the comparison?

Example:

3.2.0.416 -> The 0 would tell us that the build is an unofficial dev build.

3.2.1.417 -> The 1 would tell us that this build was produced on our QA CI box.

Can anyone point me in the right direction? I found this article to be a good starting point (Setting .NET Assembly Version with Jenkins and TFS)


Solution

    1. Create a property based on the MachineName.
    2. Use that property to update the AssemblyInfo.cs file(s) before building.

    I have "SubVersionRevision" below, but just use an alternate syntax to get the TFS version (from the link you have in your original post)........

    The Crux of the solution is : setting and using "MyCustomProperty001"

    <Choose>
    
    
        <When Condition=" '$(Computername)'=='MyDevMachine' ">               
            <PropertyGroup>
                <MyCustomProperty001>0</MyCustomProperty001>
            </PropertyGroup>
        </When>
    
        <When Condition=" '$(Computername)'=='MyQaMachine' ">
    
            <PropertyGroup>
                <MyCustomProperty001>1</MyCustomProperty001>
            </PropertyGroup>
    
        </When>
    
        <Otherwise>
    
            <PropertyGroup>
                <MyCustomProperty001>9</MyCustomProperty001>
    
            </PropertyGroup>        
    
        </Otherwise>
    
    </Choose>
    

    <ItemGroup>
      <AssemblyInfoFiles Include="$(ProjectDir)\**\*AssemblyInfo.cs" />
    </ItemGroup>
    
    <SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(SVNToolPath)">
      <Output TaskParameter="Revision" PropertyName="MySubVersionRevision" />
    </SvnVersion>
    
    
    <FileUpdate Files="@(AssemblyInfoFiles)"
    Regex="AssemblyFileVersion\(&quot;(\d+)\.(\d+)\.(\d+)\.(\d+)"
    ReplacementText="AssemblyFileVersion(&quot;$1.$2.$(MyCustomProperty001).$(SubVersionRevision)" />