简体   繁体   English

使TeamCity将Subversion内部版本号集成到程序集版本中

[英]Making TeamCity integrate the Subversion build number into the assembly version

I want to adjust the output from my TeamCity build configuration of my class library so that the produced dll files have the following version number: 3.5.0.x, where x is the subversion revision number that TeamCity has picked up. 我想调整我的类库的TeamCity构建配置的输出,以使生成的dll文件具有以下版本号:3.5.0.x,其中x是TeamCity选择的Subversion版本号。

I've found that I can use the BUILD_NUMBER environment variable to get x, but unfortunately I don't understand what else I need to do. 我发现我可以使用BUILD_NUMBER环境变量来获取x,但是不幸的是,我不知道我还需要做什么。

The "tutorials" I find all say "You just add this to the script", but they don't say which script, and "this" is usually referring to the AssemblyInfo task from the MSBuild Community Extensions. 我发现所有的“教程”都说“您只是将其添加到脚本中”,但他们没有说明哪个脚本,“ this”通常是指MSBuild社区扩展中的AssemblyInfo任务。

Do I need to build a custom MSBuild script somehow to use this? 我是否需要以某种方式构建自定义MSBuild脚本才能使用它? Is the "script" the same as either the solution file or the C# project file? “脚本”与解决方案文件还是C#项目文件相同?

I don't know much about the MSBuild process at all, except that I can pass a solution file directly to MSBuild, but what I need to add to "the script" is XML, and the solution file decidedly does not look like XML. 除了可以将解决方案文件直接传递给MSBuild之外,我对MSBuild的过程一无所知,但是我需要添加到“脚本”中的是XML,而且解决方案文件肯定看起来不像XML。

So, can anyone point me to a step-by-step guide on how to make this work? 因此,谁能指出我有关如何进行此工作的逐步指南?


This is what I ended up with: 我最终得到的是:

  1. Install the MSBuild Community Tasks 安装MSBuild社区任务
  2. Edit the .csproj file of my core class library, and change the bottom so that it reads: 编辑我的核心类库的.csproj文件,并更改底部,使其显示为:

     <Import Project="$(MSBuildToolsPath)\\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath)\\MSBuildCommunityTasks\\MSBuild.Community.Tasks.Targets" /> <Target Name="BeforeBuild"> <AssemblyInfo Condition=" '$(BUILD_NUMBER)' != '' " CodeLanguage="CS" OutputFile="$(MSBuildProjectDirectory)\\..\\GlobalInfo.cs" AssemblyVersion="3.5.0.0" AssemblyFileVersion="$(BUILD_NUMBER)" /> </Target> <Target Name="AfterBuild"> 

  3. Change all my AssemblyInfo.cs files so that they don't specify either AssemblyVersion or AssemblyFileVersion (in retrospect, I'll look into putting AssemblyVersion back) 更改所有我的AssemblyInfo.cs文件,以使其不指定AssemblyVersion或AssemblyFileVersion(回想起来,我将考虑将AssemblyVersion放回原处)

  4. Added a link to the now global GlobalInfo.cs that is located just outside all the project 添加了到现在位于所有项目外部的全局GlobalInfo.cs的链接
  5. Make sure this file is built once, so that I have a default file in source control 确保此文件生成一次,以便在源代码管理中有一个默认文件

This will now update GlobalInfo.cs only if the environment variable BUILD_NUMBER is set, which it is when I build through TeamCity. 现在,仅当设置了环境变量BUILD_NUMBER时(这是通过TeamCity构建时),这才更新GlobalInfo.cs。

I opted for keeping AssemblyVersion constant, so that references still work, and only update AssemblyFileVersion, so that I can see which build a dll is from. 我选择保持AssemblyVersion不变,以便引用仍然有效,并且仅更新AssemblyFileVersion,以便可以看到dll来自哪个版本。

The CSPROJ file is effectively an MSBuild file. CSPROJ文件实际上是MSBuild文件。

Unload the relevant class project in VS.NET, edit it and uncomment the BeforeBuild target. 在VS.NET中卸载相关的类项目,对其进行编辑并取消对BeforeBuild目标的注释。 Add the FileUpdate MSBuild task from the MSBuild Community Extensions. 从MSBuild社区扩展中添加FileUpdate MSBuild任务。

In your MSBuild file, you can retrieve the BUILD_NUMBER from TeamCity by using the environment variable $(build_vcs_number_1) . 在您的MSBuild文件中,可以使用环境变量$(build_vcs_number_1)从TeamCity检索BUILD_NUMBER。 Note that you may want to create an additional Configuration for 'Production' which' condition you check for, as this will obviously not work when you build locally. 请注意,您可能要为要检查的“生产”条件创建其他配置,因为在本地构建时,这显然不起作用。

Simply use that as input for the FileUpdate task's ReplacementText property. 只需将其用作FileUpdate任务的ReplacementText属性的输入。

Note that if your revisions numbers go above the 65535 mark ( UInt16 ), you cannot use it in the AssemblyVersion attribute. 请注意,如果修订版本号超过65535标记( UInt16 ),则不能在AssemblyVersion属性中使用它。

What I'd suggest you use instead though, is AssemblyInformationalVersion , which is just a string that does not have that limitation. 但是我建议您使用的是AssemblyInformationalVersion ,它只是一个没有此限制的字符串。 Unless of course you are confident that you won't hit this revision upper boundary, but that seems a dodgy way of going about it. 当然,除非您确信不会达到此修订版本的上限,但这似乎是一种狡猾的解决方法。

Alternatively, you can devise a way (abcd being your version number) of using revision div 1000 for c and revision mod 1000 for d. 另外,您可以设计一种方法(使用abcd作为您的版本号)对c使用修订div 1000,对d使用修订mod 1000。

In TeamCity 6.5 this is VERY EASY to achieve using AssemblyInfo patcher . 在TeamCity 6.5中,使用AssemblyInfo修补程序 非常容易实现。

  • Go to the build configuration step 3 "Build Steps"; 转到构建配置步骤3“构建步骤”;
  • Click "Add build feature"; 点击“添加构建功能”;
  • Choose Feature type "AssemblyInfo patcher"; 选择特征类型“ AssemblyInfo修补程序”;
  • Indicate what you would like as version. 指明您想要的版本。 I use: %system.build.number% 我使用: %system.build.number%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM