Aligning assembly version numbers with TFS Buildnumber

前端 未结 2 2034
小鲜肉
小鲜肉 2020-12-28 19:41

I am wondering if there is a way to synchronize the build numbers (20080213.1) without using the BuildNumberOverrideTarget where I would have to generate my own build number

相关标签:
2条回答
  • 2020-12-28 19:59

    Yes you can. At some point, possibly at AfterGet, you can use the BuildNumber and create a custom task to update the AssemblyInfo.cs files in your source code.

    We've hooked into AfterGet and caused our target to be dependant:

    <Target Name="AfterGet" DependsOnTargets="VersionAssemblies" />
    

    Our VersionAssemblies Target pulls all of the AssemblyInfo.cs files from $(SolutionRoot):

    <CreateItem Include="$(SolutionRoot)\**\AssemblyInfo.cs;">
        <Output TaskParameter="Include" ItemName="AssemblyInfos"/>
     </CreateItem>
    

    checks them out:

    <Exec Command="$(TfCommand) checkout &quot;AssemblyInfo.cs&quot; -r"
              WorkingDirectory="$(MSBuildProjectDirectory)\..\sources" ContinueOnError="true"/>
    

    edits them and replaces the file version with the $(BuildNumber):

    <File.Replace Path="%(AssemblyInfos.FullPath)"
                      NewValue="AssemblyFileVersion(&quot;$(BuildNumber)&quot;)"
                      RegularExpression="AssemblyFileVersion\(\&quot;(\d+.\d+.\d+.\d+)\&quot;\)"
                      IgnoreCase="true"
                      Force="true"/>
    

    and then checks the files back in:

    <Exec Command="$(TfCommand) checkin /override:&quot;Automated&quot; /comment:&quot;Update AssemblyInfo files to version number $(BuildNumber) - $(NoCICheckinComment) &quot; /noprompt &quot;AssemblyInfo.cs&quot; /recursive"
              WorkingDirectory="$(MSBuildProjectDirectory)\..\sources" ContinueOnError="false"/>
    

    For the replacement of the file versions I use the File.Replace task that comes with the Microsoft SDC tasks on CodePlex.

    Also note, that if you have a build that is triggered on a checkin, when checking in the AssemblyInfo.cs files, make sure the comment includes $(NoCICheckinComment) as this causes TFS not to trigger another build otherwise you'll end up in an infinite build loop.

    0 讨论(0)
  • 2020-12-28 20:09

    What you are asking for is very sensible and there are a number of ways to achieve this.

    Personally, when I do this I don't like to check the files in to version control that have the build server generated number in them - it just introduces too many head-aches when merging code across branches but also I like a known version number to be used when a developer does a workstation build vs a proper build server derived assembly to make it really easy to tell them apart.

    For more information on how I like to do it, take a look at the TFS Build Recipies wiki:

    • TFS Build: Assembly Versioning

    or my blog post on the topic

    • Martin Woodward: Aligning Build Numbers with Assembly Versions

    Hope that helps,

    Martin.

    0 讨论(0)
提交回复
热议问题