如何在 C# 构建期间从 tfs 获取构建 ID

How to get Build Id from tfs during the build in C#

要获取日志 link 我需要当前构建的 ID,我试图在我的 C# 代码中使用它,但它没有 return 构建 ID:

var envVars;
envVars = Environment.GetEnvironmentVariables();

解决办法是通过PowerShell脚本把这个var放到txt文件中,然后用C#获取

PS:

    if(!(Test-Path -Path C:\BuildVariables)){
    New-Item -ItemType directory -Path C:\BuildVariables
}

Out-File -FilePath C:\BuildVariables\buildId.txt -Force -InputObject $(Build.Buildid)

C#:

public static string ShowEnvironmentVariables()
{
    string var = File.ReadAllText("C:\BuildVariables\buildId.txt");

    return var;
}

要在使用 C# 构建期间获得 Build.Id,您可以起诉此行:

string buildId = Environment.GetEnvironmentVariable("Build_BuildId",Environment.VariableTarget.Process);

您可以从这个变量中获取构建 ID

$(Build.BuildId)

将它作为参数(完全按照这里写的那样)传递给您正在构建的工具(控制台应用程序?)。

您可以从这里检查其他变量link

https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml