如何从Azure构建管道(CI)中的预定义变量获取拉取请求号

时间:2020-03-20 09:09:07

标签: azure powershell azure-devops continuous-integration

我需要启动CI的PR编号。我正在使用System.PullRequest.PullRequestNumber,但是在运行CI时会显示空字符串。

enter image description here

写主机“ PR号为:-” $ env:System.PullRequest.PullRequestNumber

我没有通过保存和排队来运行此配置项。遵循PR的完整过程。

3 个答案:

答案 0 :(得分:1)

如何从Azure构建管道(CI)中的预定义变量获取拉取请求编号

首先,就像文档System variables的状态一样:

enter image description here

变量System.PullRequest.PullRequestNumber是从 GitHub 填充的。我们应该使用System.PullRequest.PullRequestId

因此,我们可以使用语法$(System.PullRequest.PullRequestId)来获取值:

Write-Host "PR ID is:-" $(System.PullRequest.PullRequestId)

enter image description here

此外,基于Understand variable syntax文档

当变量变成环境变量时,变量名 变成大写,句号变成下划线。例如, 变量any.variable变为$ ANY_VARIABLE

因此,如果您获得环境变量$env:System_PullRequest_PullRequestId而不是$(System.PullRequest.PullRequestId)

因此,脚本应为:

Write-Host "PR ID is:-" $env:System_PullRequest_PullRequestId

这就是System.PullRequest.PullRequestId对您不起作用的原因。

希望这会有所帮助。

答案 1 :(得分:0)

尝试使用此:

写主机“ PR号为:-” $(System.PullRequest.PullRequestId)

编辑

根据documentation,System.PullRequest.PullRequestNumber是从GitHub填充的,您确定需要吗?

和System.PullRequest.PullRequestId:

导致此构建的请求请求的ID。例如:17。 (仅当构建由于Git而运行时才初始化此变量 公关受分支机构政策的影响。)

答案 2 :(得分:0)

$(System.PullRequest.PullRequestId)仅在构建由于Git PR受分支策略影响而运行时才被初始化。根据{{​​3}},该变量也仅在经典管道(不是YAML)中可用。

您可以从以下位置提取PullRequestId

"$(RELEASE.ARTIFACTS.*YOURARTIFACTNAME*.SOURCEBRANCH)"
相关问题