Search code examples
c#powershelltfs-2015

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


I have enabled gated check-in in my build definition. I was wondering why the check-in builds succeeds whereas the following batched continuous integration build fails.

I have a powershell script step in my build definition which places the changeset the build is build from in an C# Assembly attribute which takes only an int. I figured out that the integration build fails, because the $env:BUILD_SOURCEVERSION I use in my powershell script has a 'C' prepended to the changeset number when in continuous Integration. The consequence is that [assembly: SourceChangeSet(C123456)] of course is not tolerated by the C# compiler and the compilation fails which makes the build fail.

Why is that 'C' prepended? Is this on purpose and if so, what is the purpose?

EDIT

This is the powershell script code:

$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
}

The C# attribute is declared as follows:

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

  public uint Changeset { get; }
}

My AssemblyInfo.cs file contains before script run: [assembly: SourceChangeSet(0)].

My AssemblyInfo.cs file contains after script run: [assembly: SourceChangeSet(123456)]. However in the case of an batched continuous integration build it is [assembly: SourceChangeSet(C123456)].


Solution

  • The prefix "C" means the Source Version is changeset. For example C123 means changeset 123 to build. enter image description here

    I can reproduce the behavior you meet with CI in TFS2015 but it has been changed in TFS2017. In TFS2017, the source version does not have a prefix "C" anymore when then build is triggered from CI. So I would recommend to upgrade your TFS Server to the latest version to avoid this issue.