I have a pipeline that needs to run on a cron on main
, but also trigger when a pull request is merged into main
. There is a trigger for this, however, I would need the pull request number to pass as a variable for a script to run against.
Are there any more options? besides a webhook to trigger on pull request completion, but also access the pull request number? Without relying on a git commit messages.
I have explored the following, however, I would prefer the pipeline configuration to contain this logic:
You can use Webhook triggers to trigger the pipeline when a PR merge to the main
branch is completed.
Below are the detailed steps of configuration:
Go to "Project Settings" > "Service connections" to create an Incoming WebHook service connection.
CompletePR
.IWHSC_CompletePR
.Go to "Project Settings" > "Service Hooks" to create a Web Hooks
type subscription.
On the 'Service' section, select "Web Hooks
" as the service.
On the 'Trigger' section,
Trigger on this type of event
is "Pull request merge attempted
".Repository
is your Git repository.Target branch
is main
.Merge result
is Merge Successful
.On the 'Action' section, set the value of URL
to be the following one. Other options can keep as default.
https://dev.azure.com/{organization}/_apis/public/distributedtask/webhooks/{webhookName}?api-version=6.0-preview
Replace {organization} with the actual name of your Azure DevOps organization. The {webhookName}
should be same as the WebHook Name
set on above Incoming WebHook service connection. For example,
https://dev.azure.com/myOrg/_apis/public/distributedtask/webhooks/CompletePR?api-version=6.0-preview
Set the pipeline with a Webhook trigger like as below.
# azure-pipelines.yml
. . .
resources:
webhooks:
- webhook: CompletePR # The name should be same as the webhook name set above.
connection: IWHSC_CompletePR # The name of Incoming WebHook service connection.
filters:
- path: resource.status
value: completed # Set the pipeline to be triggered when the PR merge is completed.
# Set some global variables to store the information of completed PR.
# Any tasks in this pipeline can call these variables.
variables:
prId: ${{ parameters.CompletePR.resource.pullRequestId }}
prTitle: ${{ parameters.CompletePR.resource.title }}
prStatus: ${{ parameters.CompletePR.resource.status }}
sourceBranch: ${{ parameters.CompletePR.resource.sourceRefName }}
targetBranch: ${{ parameters.CompletePR.resource.targetRefName }}
commitId: ${{ parameters.CompletePR.resource.lastMergeCommit.commitId }}
commitComment: ${{ parameters.CompletePR.resource.lastMergeCommit.comment }}
steps:
- bash: |
echo "prId = $(prId)"
echo "prTitle = $(prTitle)"
echo "prStatus = $(prStatus)"
echo "sourceBranch = $(sourceBranch)"
echo "targetBranch = $(targetBranch)"
echo "commitId = $(commitId)"
echo "commitComment = $(commitComment)"
displayName: 'Pull Request Information'
. . .
Result.