在批量持续集成 TFS 构建中,字符 C 被添加到变更集编号

Character C being prepended to changeset number in batched continuous integration TFS builds

我在我的构建定义中启用了门控签入。我想知道为什么签入构建成功而以下批量持续集成构建失败。

我的构建定义中有一个 powershell 脚本步骤,它将构建的变更集置于仅采用 int 的 C# 程序集属性中。我发现集成构建失败了,因为在持续集成中,我在 powershell 脚本中使用的 $env:BUILD_SOURCEVERSION 有一个 'C' 预先添加到变更集编号。结果是 [assembly: SourceChangeSet(C123456)] 当然是 C# 编译器不能容忍的,编译失败导致构建失败。

为什么要在前面加上 'C'?这是故意的吗?如果是,目的是什么?

编辑

这是 powershell 脚本代码:

$buildSourceVersion = $env:BUILD_SOURCEVERSION.TrimStart("C")
$SrcPath = $env:BUILD_SOURCESDIRECTORY
$AllVersionFiles = Get-ChildItem $SrcPath AssemblyInfo.cs -recurse

Write-Verbose "Changeset to build:          $buildSourceVersion" -Verbose

foreach ($file in $AllVersionFiles)
{
(Get-Content $file.FullName) |
    %{$_ -replace 'AssemblySourceChangeset\(0\)', "AssemblySourceChangeset($buildSourceVersion)" } |
Set-Content $file.FullName -Force
}

C#属性声明如下:

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
public class AssemblySourceChangesetAttribute : Attribute
{
  public AssemblySourceChangesetAttribute(uint changeset)
  {
    Changeset = changeset;
  }

  public uint Changeset { get; }
}

我的 AssemblyInfo.cs 文件在脚本 运行 之前包含:[assembly: SourceChangeSet(0)].

我的 AssemblyInfo.cs 文件在脚本 运行 之后包含:[assembly: SourceChangeSet(123456)]。然而,在批量持续集成构建的情况下,它是 [assembly: SourceChangeSet(C123456)]

为什么有些事情您必须向 Microsoft 询问。 visualstudio.com$env:BUILD_SOURCEVERSION 的注释说:

The latest version control change that is included in this build.

Git: The commit ID.

TFVC: the changeset.

这让我觉得它可能在那里,因此您可以确定它是 Git 存储库 (C123 = Commit123) 还是 TFVC 存储库 (CS123 = ChangeSet123)。

您可以使用正则表达式删除两者,例如:

$changset = $env:BUILD_SOURCEVERSION -replace '^C|^CS'

前缀"C"表示源版本是变更集。例如 C123 表示要构建的变更集 123。

我可以在 TFS2015 中重现您遇到的 CI 行为,但它已在 TFS2017 中发生更改。在 TFS2017 中,当从 CI 触发构建时,源版本不再有前缀 "C"。所以我建议将您的 TFS 服务器升级到最新版本以避免此问题。