I need a pipeline to trigger when PR is completed/abandoned
In this configuration, it triggers all the time (creation/completion)
trigger:
branches:
include:
- repository-creation-request-*
- repository-creation-requests
paths:
include:
- Repository_creation_request_*.txt
I tried this but now it does not trigger at all
pr:
branches:
include:
- repository-creation-request-*
- repository-creation-requests
paths:
include:
- Repository_creation_request_*.txt
trigger: none # Disable automatic triggers for commits
I need a pipeline to trigger when PR is completed/abandoned
In this configuration, it triggers all the time (creation/completion)
trigger:
branches:
include:
- repository-creation-request-*
- repository-creation-requests
paths:
include:
- Repository_creation_request_*.txt
I tried this but now it does not trigger at all
pr:
branches:
include:
- repository-creation-request-*
- repository-creation-requests
paths:
include:
- Repository_creation_request_*.txt
trigger: none # Disable automatic triggers for commits
Share
Improve this question
edited Mar 31 at 10:50
jonrsharpe
122k30 gold badges268 silver badges475 bronze badges
asked Mar 31 at 10:41
pf12345678910pf12345678910
4744 silver badges11 bronze badges
1
|
1 Answer
Reset to default 0I need a pipeline to trigger when PR is completed/abandoned
To achieve this, you can use webhook to trigger your pipeline and set the event of the webhook to pull request updated. Refer to the steps below.
1.Create a webhook.
Go to Project Settings -> Service Hooks -> Click plus button -> Web Hooks -> Next
Configure your webhook as shown below:
Trigger on this type of event: Pull request updated
Repository: Select your target repo
Target branch: Target branch of your PR
Change: Status changed
URL:
https://dev.azure/<OrgName>/_apis/public/distributedtask/webhooks/<WebHookName>?api-version=6.0-preview
2.Create an incoming webhook service connection.
Go to Project Settings -> Service Connections -> New Service Connection -> Select Incoming WebHook.
WebHook Name: The name of the webhook you created at step1.
Service Connection Name: Assign a new to your service connection.
3.Add webhook resource in your pipeline.
We can add filters on the webhook resource as shown below.
resources:
webhooks:
- webhook: PRWebhook # Your webhook name
connection: PRWebHookSC # Your Incoming webhook service connection name
filters:
- path: resource.status
value: completed
However, filter doesn't support expression. So that we can't trigger the pipeline when the status of PR is completed OR abandoned. To resolve the issue, there are two workarounds.
Workaround1: Trigger the pipeline whenever the status of the PR changes but fail the pipeline if the status is neither completed or abandoned.
trigger: - none resources: webhooks: - webhook: PRWebhook connection: PRWebHookSC pool: vmImage: ubuntu-latest jobs: - ${{if in(parameters.PRWebhook.resource.status,'completed','abandoned')}}: - job: jobA steps: - script: echo Hello, world! displayName: 'Run a one-line script' # ... your steps - ${{else}}: - job: jobB # Fail the pipeline if the status of PR is neither completed or abandoned steps: - script: exit 1 displayName: 'Fail the pipeline'
Result:
Workaround2: Create two identical pipelines, using the same webhook resource but with different filters.
One sets
resource.status
tocompleted
, another setsresource.status
toabandoned
.resources: webhooks: - webhook: PRWebhook # Your webhook name connection: PRWebHookSC # Your Incoming webhook service connection name filters: - path: resource.status value: completed
The pipelines will be triggered only when the PR status changes to completed or abandoned, but they run separately.
Update:
how do I pass the branch to be merged with master/main
You can use ${{parameters.<WebhookAlias>.resource.targetRefName}}
. All JSON payload data in the webhook is available in the form of ${{ parameters.<WebhookAlias>.<JSONPath>}}
. Navigate to the webhook you have created -> History -> Event. Then you can check the JSON payload data in the webhook.
For example,
- ${{if in(parameters.PRWebhook.resource.status,'completed','abandoned')}}:
- job: jobA
steps:
- script: |
echo 'The value of sourceRefName is ${{parameters.PRWebhook.resource.sourceRefName}}'
echo 'The value of targetRefName is ${{parameters.PRWebhook.resource.targetRefName}}'
displayName: 'echo payload data '
Result:
${{parameters.<WebhookAlias>.resource.targetRefName}}
. All JSON payload data in the webhook is available in the form of${{ parameters.<WebhookAlias>.<JSONPath>}}
. See my update in the answer for the details. – Ziyang Liu-MSFT Commented 2 days ago