如何从 ADO webhook 调用的 PowerShell 脚本中获取数据?

问题描述 投票:0回答:1

我对 PowerShell 脚本编写非常陌生, 所以我处于这样的场景:

  1. 一旦工作项在 ADO 中更新,servicehook 就会通过 REST api 使用工作项数据自动触发管道。

现在我必须接收来自该 powershell 脚本中的服务挂钩的内容。我怎样才能做到这一点?

It it the serviceHook Request 我如何才能访问 powershell 脚本内容中的数据?

我尝试执行从谷歌找到的一些代码,但它不起作用

azure-devops azure-powershell
1个回答
0
投票

从您的屏幕截图来看,您正在使用 Rest API:Runs - Run Pipeline 来触发管道。

这种情况下相当于手动触发pipeline,所以我们无法通过脚本获取webhook中的信息。

为了满足您的需求,您需要更改为使用Incoming webhook来触发Pipeline,然后我们可以通过pipeline参数直接获取webhook中的数据。

请参阅此文档来设置传入 Webhook:YAML 管道的基于通用 Webhook 的触发器

第 1 步:使用以下 API 调用创建 Webhook:

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

Step2:在项目设置 -> 服务连接中创建传入 webhook 服务连接。

Step3:您可以使用 Pipeline 中的 Incoming webhook 来触发管道。

例如:

resources:
  webhooks:
    - webhook: webhook         
      connection: webhookserviceconnectionname

pool:
  vmImage: ubuntu-20.04

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

在这种情况下,您可以使用参数来获取webhook数据。

例如:

管道参数格式:

${{ parameters.webhook.resource.revision.id}}
${{ parameters.webhook.resource.revision.fields['System.AreaPath'] }}
© www.soinside.com 2019 - 2024. All rights reserved.