Search code examples
azure-devopsazure-pipelines

Retrieve data from webhook payload


I am able to check several data from webhook payload, but cant other or even to see the full payload. So far I have added those variables which works as expected:

resources:
  [rest of the code]
  webhooks:
  - webhook: Coin
    connection: FromCoin
    filters:
      - path: resource.definition.name
        value: 'Coin'

variables:
  - name: buildNumber
    value: ${{ parameters.Coin.resource.buildNumber }}
  - name: sourceBranch
    value: ${{ parameters.Coin.resource.sourceBranch }}

I get such output:

Triggered by: CI-4-Coin Source Branch Name: refs/heads/release/2.0

For example, when I added:

  - name: targetBranch
    value: ${{ parameters.Coin.resource.targetBranch }}

It shows empty value under the targetBranch in ADO pipelines output. How to retrieve the full list of all available variables which I can consume from webhook payload in different org/project/pipeline in mine pipeline?


Solution

  • When referencing values from a webhook, the syntax is parameters.<WebhookAlias>.<JSONPath>.

    To view the payload, you can use the convertToJson() function.

    resources:
      webhooks:
      - webhook: myWebhookTrigger
        connection: IncomingWebhookServiceConnection
    
    trigger: none
    
    steps:
    - pwsh: |
        write-host $env:payload
      env:
        payload: ${{ convertToJson( parameters.myWebhookTrigger ) }}
    

    enter image description here

    Given an example payload:

    {
        "resource": {
            "message": {
                "title": "Hello, world!",
                "subtitle": "I'm using WebHooks!"
            }
        }
    }
    

    You would reference the values as:

    • parameters.myWebhookTrigger.resource.message.title
    • parameters.myWebhookTrigger.resource.message.subtitle