在 github 操作中将带有客户端有效负载的请求更正为 运行 workflow_dispatch

Correct request with client-payload to run workflow_dispatch in github action

我用 workflow_dispatch 创建简单的 github 动作。

name: Run Workflow Dispatch

on: 
  workflow_dispatch:
    inputs:
      version:
        description: 'version'     
        required: true
        default: 'latest'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - run: |
        echo "version: ${{ github.event.inputs.version }}"

我通过 curl 创建请求。

curl -X POST \
  -H "Accept: application/vnd.github.everest-preview+json" \
  -H "Authorization: token xxxxxxxxx" \
  https://api.github.com/repos/patsevanton/workflow-dispatch-client-payload/actions/workflows/workflow_dispatch.yml/dispatches \
  --data '{"event_type": "my-dispatch", "client_payload": {"ref": "main"}}'

但我得到错误:

{
  "message": "Invalid request.\n\n\"client_payload\", \"event_type\" are not permitted keys.\n\"ref\" wasn't supplied.",
  "documentation_url": "https://docs.github.com/rest/reference/actions#create-a-workflow-dispatch-event"
}

如何在 github 动作中创建正确的 workflow_dispatch? 如何在 github 操作中创建对 workflow_dispatch 的正确请求?

您似乎混淆了 Github 操作上可用的 2 个工作流触发事件。

workflow_dispatchrepository_dispatch 事件。


创建工作流调度事件

要远程触发 workflow_dispatch 事件,您需要使用以下端点:

https://api.github.com/repos/{owner}/{repository}/actions/workflows/{workflow_id}/dispatches

这里是the related documentation on Github

请注意,您可以使用此 POST 服务的正文,通知最终 inputs


创建存储库调度事件

要远程触发 repository_dispatch 事件,您需要使用以下端点:

https://api.github.com/repos/{owner}/{repository}/dispatches

这里是the related documentation on Github

在这种情况下,您可以使用 client_payload 参数以及 event_type 参数。


结论

在您的情况下,您似乎想要使用 工作流调度端点 存储库调度事件 [=17] 的混合来触发工作流调度事件=](这不是该端点上的可用参数)。

因此,如果您想触发工作流,第一个选项是使用存储库调度事件来触发您的工作流,更新工作流文件中的工作流触发器以使用repository_dispatch 而不是 workflow_dispatch 和您使用 curl 命令调用的端点。

另一个选项是用你的 curl 命令调用另一个端点(与 workflow dispatch 相关的而不是与 repository dispatch 相关的,通知 inputs 参数而不是client_payload 参数。