I have an Azure Pipeline that needs to be triggered only when the Pull Request is completed.
The Pipeline is using Azure Repo.
Now, I have the following trigger settings in Azure Pipeline.
trigger:
branches:
include:
- main
When the Pull Request is completed, it indeed can trigger the Pipeline. But if I do any commits on the main branch, it will trigger the Pipeline too.
Are there any methods can trigger the Pipeline only when the Pull Request is completed?
I have tried to check the variables: Build.reason
, System.PullRequest
. But they cannot work in the pipeline trigger.
I am afraid that Azure DevOps pipeline has no out-of-box method can trigger the pipeline only when the Pull Request is completed.
Are there any methods can trigger the Pipeline only when the Pull Request is completed?
I suggest that you can use the Webhook and Resource Webhook trigger in Azure Pipeline.
Here are the steps:
Step1: Create a webhook in Project Settings -> Service hooks -> Webhook.
For example:
You can set the event: Pull Request Update and set the Change: Status Changed.
Then you can set the Webhook URL:
https://dev.azure.com/<ADO Organization>/_apis/public/distributedtask/webhooks/<WebHook Name>?api-version=6.0-preview"
In this case, when the Pull Request state change, it will trigger the Webhook.
Step2: Create an Incoming Webhook in Project Settings-> Service Connections. Refer to this doc: Generic webhook based triggers for YAML pipelines
Step3: You can disable the CI trigger(trigger: none
) and setup the Webhook trigger in Pipeline and the trigger filter:
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}}
In this case, when the Pull Request status change, it will trigger the webhook and send the request to trigger pipeline.
It will contains the Pull Request Status.
If the status is completed, the Webhook trigger will trigger the Pipeline.
If you directly do commits in main branch, it will not trigger the pipeline.