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

问题描述 投票:0回答:2

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

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

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

基本上仅在不是拉取请求时才运行此阶段。但是,当我手动触发管道时,azure Devop 决定跳过此操作。所以我添加了构建原因和 pullrequestId 作为变量然后输出它。

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

输出为:

Manual

$(System.PullRequest.PullRequestId)

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

那么除了 PullRequestId 之外,buildreason 是区别 PR 或非 PR 运行的可靠条件吗?

azure-devops azure-pipelines
2个回答
3
投票

那么除了 PullRequestId 之外,buildreason 是区别 PR 或非 PR 运行的可靠条件吗?

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

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

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

未填充时该变量不存在。因此,在您的情况下,

variables['System.PullRequest.PullRequestId']
的值为
'System.PullRequest.PullRequestId'


0
投票

如果其他人想知道确切的值,应该与 = '' 进行比较

- task: PowerShell@2
displayName: Pull Request variables
condition: ne(variables['System.PullRequest.PullRequestId'], '')
inputs:
  targetType: 'inline'
  script: |
    Write-Host "Pull Request Id: $($env:System_PullRequest_PullRequestId)"
© www.soinside.com 2019 - 2024. All rights reserved.