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

发布于 2024-08-17 22:16:38 字数 1433 浏览 5 评论 0原文

我想要调整类库的 TeamCity 构建配置的输出,以便生成的 dll 文件具有以下版本号:3.5.0.x,其中 x 是 TeamCity 已选取的颠覆修订版号。

我发现我可以使用 BUILD_NUMBER 环境变量来获取 x,但不幸的是我不明白我还需要做什么。

我发现“教程”都说“您只需将其添加到脚本中”,但他们没有说哪个脚本,并且“this”通常指的是 MSBuild 社区扩展中的 AssemblyInfo 任务。

我是否需要以某种方式构建自定义 MSBuild 脚本才能使用它? “脚本”与解决方案文件或 C# 项目文件相同吗?

我对MSBuild过程根本不了解,除了我可以将解决方案文件直接传递给MSBuild,但我需要添加到“脚本”的是XML,而解决方案文件显然不像XML。

那么,有人可以向我指出如何进行这项工作的分步指南吗?


这就是我最终得到的结果:

  1. 安装 MSBuild 社区任务
  2. 编辑我的核心的 .csproj 文件类库,并更改底部,使其显示为:

    <导入项目=“$(MSBuildToolsPath)\Microsoft.CSharp.targets”/>
    <导入项目=“$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets”/>
    <目标名称=“BeforeBuild”>
      
    
    <目标名称=“AfterBuild”>
    

  3. 更改我的所有 AssemblyInfo.cs 文件,以便它们不指定 AssemblyVersion 或 AssemblyFileVersion(回想起来,我会考虑将 AssemblyVersion 放回去)

  4. 添加了一个指向当前全局 GlobalInfo.cs 的链接,该链接位于所有项目之外
  5. 确保此文件构建一次,以便我有一个源代码管理中的默认文件

现在,仅当设置了环境变量 BUILD_NUMBER(这是我通过 TeamCity 构建时)时,才会更新 GlobalInfo.cs。

我选择保持 AssemblyVersion 不变,以便引用仍然有效,并且仅更新 AssemblyFileVersion,以便我可以看到 dll 来自哪个版本。

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.

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.

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.

Do I need to build a custom MSBuild script somehow to use this? Is the "script" the same as either the solution file or the C# project file?

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.

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
  2. Edit the .csproj file of my core class library, and change the bottom so that it reads:

    <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)

  4. Added a link to the now global GlobalInfo.cs that is located just outside all the project
  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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪异←思 2024-08-24 22:16:38

CSPROJ 文件实际上是一个 MSBuild 文件。

在 VS.NET 中卸载相关的类项目,对其进行编辑并取消注释 BeforeBuild 目标。从 MSBuild 社区扩展添加 FileUpdate MSBuild 任务。

在 MSBuild 文件中,您可以使用环境变量 $(build_vcs_number_1) 从 TeamCity 检索 BUILD_NUMBER。请注意,您可能希望为您检查的“生产”条件创建一个额外的配置,因为当您在本地构建时,这显然不起作用。

只需将其用作 FileUpdate 任务的 ReplacementText 属性的输入即可。

请注意,如果您的修订号超过 65535 标记 (UInt16),则无法在 AssemblyVersion 属性中使用它。

不过,我建议您使用 AssemblyInformationalVersion,它只是一个没有该限制的字符串。当然,除非您有信心不会达到此修订上限,但这似乎是一种狡猾的方法。

或者,您可以设计一种方法(abcd 是您的版本号),对 c 使用修订版 div 1000,对 d 使用修订版 mod 1000。

The CSPROJ file is effectively an MSBuild file.

Unload the relevant class project in VS.NET, edit it and uncomment the BeforeBuild target. Add the FileUpdate MSBuild task from the MSBuild Community Extensions.

In your MSBuild file, you can retrieve the BUILD_NUMBER from TeamCity by using the environment variable $(build_vcs_number_1). 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.

Note that if your revisions numbers go above the 65535 mark (UInt16), you cannot use it in the AssemblyVersion attribute.

What I'd suggest you use instead though, is AssemblyInformationalVersion, which is just a string that does not have that limitation. 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 (a.b.c.d being your version number) of using revision div 1000 for c and revision mod 1000 for d.

小…楫夜泊 2024-08-24 22:16:38

在 TeamCity 6.5 中,使用 AssemblyInfo patcher 可以非常容易实现这一点。

  • 进入构建配置步骤3“构建步骤”;
  • 点击“添加构建功能”;
  • 选择功能类型“AssemblyInfo patcher”;
  • 指明您想要的版本。我使用:%system.build.number%

In TeamCity 6.5 this is VERY EASY to achieve using AssemblyInfo patcher.

  • Go to the build configuration step 3 "Build Steps";
  • Click "Add build feature";
  • Choose Feature type "AssemblyInfo patcher";
  • Indicate what you would like as version. I use: %system.build.number%
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文