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

问题描述

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

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

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

基本上只有在它不是拉取请求时才运行这个阶段。然而,当我手动触发管道时,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 之外,构建原因是 PR 或非 PR 运行之间存在差异的可靠条件吗?

解决方法

那么,除了 PullRequestId 之外,构建原因是 PR 或非 PR 运行之间存在差异的可靠条件吗?

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

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

文档说 PullRequestId 只会在它是拉取请求时被填充,但我真的不知道它不填充时的值是什么。

未填充时该变量不存在。因此,在您的情况下,variables['System.PullRequest.PullRequestId'] 的值为 'System.PullRequest.PullRequestId'