在 Azure Devops 构建管道步骤之间传递输出变量,例如 7 digit git hash

Passing output variables between Azure Devops build pipeline steps, for example 7 digit git hash

我试图在我的 Azure Devops 构建管道构建的工件名称中包含 7-digit git 哈希。在我的构建代理工作中,我有一个包含以下行的内联 powershell 脚本:

$commitId = "$(Build.SourceVersion)"
$shortCommitId = ("$commitId").SubString(0,7)

在 "Output variables" 下的 Powershell 脚本选项中,我添加了一个引用名称:ps.

然后在我的 Powershell 步骤之后的发布工件步骤中,我将工件名称设置为:

$(ApplicationName)_Rel_$(ps.shortCommitId)_$(build.buildnumber)

管道完成后的实际结果运行是:

MyApplication_Rel_$(ps.shortCommitId)_20190918.1

如何在步骤之间传递变量 shortCommitId,使其成为工件名称的一部分? MyApplication_Rel_04f53f_20190918.18.

只需在后续步骤中添加另一行创建变量即可:

Write-Host "##vso[task.setvariable variable=shortCommitId;]$shortCommitId"

在 Publish Artifacts 任务中使用变量 $(shortCommitId):

$(ApplicationName)_Rel_$(shortCommitId)_$(build.buildnumber)

如果您想将其用作输出变量,另一种选择是添加 isOutput=true

Write-Host "##vso[task.setvariable variable=shortCommitId;isOutput=true]$shortCommitId"

现在您可以使用 $(ps.shortCommitId)