azure-devopsautomationazure-pipelinesdevopsazure-boards

Trigger Azure Pipelines based on Azure Boards events


I'm looking into creating some automation based on events occurring on azure DevOps boards. I have the base integration between GitHub and ADO that gives me commit/PR links based on commit message and PR title, but that's not enough...

Ultimately what I want is to listen on events coming from my boards (e.g. item creation, item moved etc.) and trigger azure pipeline based on said event with all the data associated with the work item for further processing. One would think that these two should be a perfect match, but I didn't find a simple solution that doesn't involve going through a 3rd party. The docs didn't do it any justice either... has anyone faced this issue before?

I find it very peculiar, that a webhook for jenkins does exist while direct trigger to azure pipeline doesn't, not a webhook for github actions. I most definitely missing something here.


Solution

  • You can refer the Generic webhook based triggers for YAML pipelines to trigger Azure Pipelines based on Azure Boards events.

    Here are the detailed steps:

    1. Create an "Incoming Webhook" service connection in Project settings > Service connections service connection

      Incoming Webhooks service connection

    2. Create a Service hook web hook with work item related event in Project settings > Service Hooks. I choose the work item created event in the example.

      web hook

      The Request Url is

      "https://dev.azure.com/<ADO Organization>/_apis/public/distributedtask/webhooks/<WebHook Name>?api-version=6.0-preview"
      

      The WebHook Name in the URL is the one you set in the service connection.

      WebHook Name

    3. Create a pipeline with the resource type webhooks. The pipeline will be triggered when the work item created webhook event is received by the Incoming Webhook service connection and you can consume the payload data in the form of variables in your jobs.

    Sample YAML:

    trigger: none
    resources:
      webhooks:
        - webhook: MyWebhookTrigger         ### Webhook name
          connection: MyWebhookConnection    ### Incoming webhook service connection name
    
    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        ### JSON payload data is available in the form of ${{ parameters.<WebhookAlias>.<JSONPath>}}
        script: |
          Write-Host "message:  ${{ parameters.MyWebhookTrigger.message.text}}"
    
    - task:your other tasks
    
    1. Test result:

      result

    Update:

    To print the whole payload of the webhook, you can add the following PowerShell task in the pipeline.

    - powershell: |    
        $jsonParams = '${{ convertToJson(parameters.MyWebhookTrigger) }}'
        Write-Host $jsonParams
      displayName: 'Print JSON payload data'
    

    Result:

    Result