azure-devopsazure-pipelines

Is there a way with Azure Devops Pipeline to trigger on pull request completion?


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:


Solution

  • 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:

    1. Go to "Project Settings" > "Service connections" to create an Incoming WebHook service connection.

      • WebHook Name: A custom name of the webhook. For example, CompletePR.
      • Service connection name: A custom name of the service connection. For example, IWHSC_CompletePR.

      enter image description here

    2. Go to "Project Settings" > "Service Hooks" to create a Web Hooks type subscription.

      • On the 'Service' section, select "Web Hooks" as the service.

        enter image description here

      • 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.
        • Other options can keep as default.

        enter image description here

      • 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
        

        enter image description here

    3. 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'
      
      . . .
      
    4. Result.

      enter image description here