如何在 azure devop 管道中设置拉取请求和非拉取请求阶段的条件?

how to set condition for pull request and non-pullrequest stage in azure devop pipeline?

我正在学习使用 azure devop 的 yaml 管道。

在一个阶段,我把这个条件放在

condition: and(succeeded(), eq(variables['System.PullRequest.PullRequestId'], 'Null')
variables:
  prId: $(System.PullRequest.PullRequestId)
  reason: $(Build.Reason)

基本上只有在非pull request的时候才会运行这个阶段。但是,当我手动触发管道时,azure devop 决定跳过它。所以我添加了 build reason 和 pullrequestId 作为变量然后输出它。

write-host "------------------------------"
write-host '$(reason)'
write-host "------------------------------"
write-host '$(prId)' 

输出为:

Manual

$(System.PullRequest.PullRequestId)

我在想当它不是由 PullRequest 触发时,$(System.PullRequest.PullRequestId) 应该是 null 或空字符串。看起来并非如此。文档说 PullRequestId 只有在它是拉取请求时才会被填充,但我真的不知道它没有被填充时的值是什么。至少看起来不是 Null。

除了 PullRequestId 之外,buildreason 是否是区分 PR 或非 PR 运行 的可靠条件?

So is buildreason the reliable condition to differ between a PR or non-PR run other than PullRequestId?

是的。您可以使用以下脚本:

condition: and(succeeded(), ne(variables['Build.Reason'], 'PullRequest'))

The document say PullRequestId will only be populated when it is a pull request, but I don't really know what the value is when it is not populated.

变量未填充时不存在。所以在你的例子中,variables['System.PullRequest.PullRequestId'] 的值是 'System.PullRequest.PullRequestId'.