我有一个 Azure Pipeline,仅当拉取请求完成时才需要触发。
管道正在使用 Azure Repo。
现在,我在 Azure Pipeline 中有以下触发器设置。
trigger:
branches:
include:
- main
当Pull Request完成后,确实可以触发Pipeline。但是如果我在主分支上进行任何提交,它也会触发管道。
是否有任何方法可以仅在 Pull Request 完成时触发 Pipeline?
我尝试检查变量:
Build.reason
,System.PullRequest
。但它们不能在管道触发器中工作。
恐怕Azure DevOps管道没有现成的方法可以仅在Pull Request完成时触发管道。
是否有任何方法可以仅在 Pull Request 完成时触发 Pipeline?
我建议您可以在 Azure Pipeline 中使用 Webhook 和 Resource Webhook 触发器。
步骤如下:
Step1:在Project Settings -> Service hooks -> Webhook中创建webhook。
例如:
您可以设置事件:Pull Request Update 并设置更改:Status Changed。
然后就可以设置Webhook URL了:
https://dev.azure.com/<ADO Organization>/_apis/public/distributedtask/webhooks/<WebHook Name>?api-version=6.0-preview"
这种情况下,当Pull Request状态发生变化时,就会触发Webhook。
Step2:在Project Settings->Service Connections中创建Incoming Webhook。请参阅此文档:YAML 管道的基于通用 Webhook 的触发器
Step3:您可以禁用 CI 触发器(
trigger: none
)并在 Pipeline 和触发器过滤器中设置 Webhook 触发器:
trigger: none
resources:
webhooks:
- webhook: webhookname ### Webhook alias
connection: serviceconnectionname ### Incoming webhook service connection
filters:
- path: resource.status ### JSON path in the payload
value: completed ### Expected value in the path provided
steps:
- script: echo ${{ parameters.testkevin.resource.status}}
在这种情况下,当 Pull Request 状态发生变化时,会触发 webhook 并发送请求到触发 pipeline。
它将包含拉取请求状态。
如果状态为完成,Webhook触发器将触发Pipeline。
如果直接在主分支上提交,不会触发管道。