spencerstewart
spencerstewart

Reputation: 151

Azure DevOps Pipeline Incoming Webhook from GitHub: Json Payload empty in pipeline

Introduction

Hi there, I'm trying to create a webhook from GitHub to trigger an Azure DevOps (ADO) Pipeline with parameters from the webhook request body. The ADO pipeline, however, doesn't seem to be aware of any of info from the request body.

I've set up an Azure DevOps incoming webhook following their documentation: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/resources?view=azure-devops&tabs=example#define-a-webhooks-resource

GitHub Outgoing Webhook is Successful

From GitHub, I've set up the outcoming webhook and the deliveries are successful to Azure DevOps. Note some of info in the payload: Screenshot of webhook delivery from GitHub

Azure DevOps Yaml Pipeline: Incoming Webhook Resource

On the Azure DevOps side, however, it doesn't seem to receive info from the JSON body, however. Here's my simple pipeline, following the documentation's example:

resources:
  webhooks:
    - webhook: CleanUpPREnv          ### Webhook alias
      connection: CleanUpPREnvWebhookConnection    ### Incoming webhook service connection

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    ### JSON payload data is available in the form of ${{ parameters.<WebhookAlias>.<JSONPath>}}
    script: |
      Write-Host ${{ parameters.CleanUpPREnv.before}}
      Write-Host ${{ parameters.CleanUpPREnv.repository.full_name}}
      Write-Host ${{ parameters.CleanUpPREnv.repository}}

Azure DevOps Pipeline doesn't have payload data

The pipeline run writes empty strings on lines 12-14, however: enter image description here

Works with Postman; Other Thoughts

Interestingly, I can get this to work using a Postman request, so I know the fundamentals are working. There's just something I'm missing between GitHub and Azure DevOps that I can't figure out. Any insights would be appreciated... otherwise maybe we do need to migrate to GitHub Actions from Azure DevOps pipelines, lol.

Upvotes: 2

Views: 4489

Answers (2)

Eli-Work
Eli-Work

Reputation: 21

I had an issue with similar symptoms but a different cause.

It turns out that one must use ${{}}, not ${} to access paramaters, because parameters is a YAML variable, not a PowerShell variable.

(Credit to Accessing Incoming WebHook JSON payload in Bash - Azure Pipelines for this 🙂)

Upvotes: 0

spencerstewart
spencerstewart

Reputation: 151

The Azure DevOps (ADO) pipeline that was supposed to be triggered by an incoming webhook was also being triggered by default CI and PR triggers. When the pipeline is run by one of those triggers, it obviously lacks the data expected from the JSON webhook payload (because a webhook didn't trigger that run of the pipeline!).

Adding the following code the pipeline to disable CI and PR triggers prevents this behavior, such that the pipeline ONLY runs when the webhook is triggered and includes desired webhook request data:

trigger: none
pr: none

A full example of an Azure DevOps Yaml Pipeline that is only triggered from an incoming webhook would look like this:

# file: azure-pipelines.yml
trigger: none
pr: none

resources:
  webhooks:
    - webhook: MyWebhookTrigger          
      connection: MyWebhookConnection    

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      Write-Host ${{ parameters.MyWebhookTrigger.repositoryName}} ### JSON payload data is available in the form of ${{ parameters.<WebhookAlias>.<JSONPath>}}
      Write-Host ${{ parameters.MyWebhookTrigger.component.group}}

By disabling non-webhook triggers, you can ensure it only runs from an incoming webhook and includes the expected data from the webhook's request body.

Upvotes: 3

Related Questions