首页 > 解决方案 > 使用 POST 请求触发 Github Action (Github REST API)

问题描述

我有一个托管在 GitHub 组织中的私有 GitHub 存储库。该存储库包含一个带有选项的 GitHub 操作workflow_dispatch(参见GitHub 文档)。

动作 YAML 文件的摘录:

on:
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

我可以从 GitHub 操作选项卡成功触发此操作。

但是,我希望能够通过POST对 GitHub API 的请求来触发此操作。这应该可以使用 GitHub API 实现。相关 API 端点似乎/repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches文档中所述。

该文档进一步指出:

您必须使用具有 repo 范围的访问令牌进行身份验证才能使用此端点。

因此,在“开发人员设置”→“个人访问令牌”下的我的个人帐户设置中,我创建了一个令牌并授予对所有“回购”项目以及“工作流程”项目的访问权限。

我已经通过使用以及PostmanPOST发出请求来测试触发 GitHub 操作。为此,我根据 GitHub 文档使用以下参数:curl

使用文档curl中的示例(但添加身份验证):

curl -X POST -H "Accept: application/vnd.github.v3+json" \
  -u GITHUBUSERNAME:GITHUBPERSONALACCESSTOKEN \
  https://api.github.com/repos/ORGNAME/REPONAME/actions/workflows/YMLFILE/dispatches \
  -d '{"ref":"main"}'

我得到以下结果:

{
  "message": "Problems parsing JSON",
  "documentation_url": "https://docs.github.com/rest/reference/actions#create-a-workflow-dispatch-event"
}

我还测试了从PostmanPOST发出请求(通过导入上述语句)。这会产生完全相同的结果。curl

  1. 我是否需要以不同方式配置此过程的 GitHub 端?
  2. 我的curl说法有错吗?
  3. 有什么方法可以检查“问题[atic] JSON”以找出发生了什么?

标签: githubcurlgithub-actionsrest

解决方案


按照此说明https://goobar.dev/manually-trigger-a-github-actions-workflow/你可能大部分都是正确的

尝试在 LINUX 上运行它 curl -H "Accept: application/vnd.github+json" -H "Authorization: token your-token" --request POST --data '{"event_type": "do-something"}' https://api.github.com/repos/yourname/yourrepo/dispatches

在 Windows 上:cURL POST 命令在 Windows 命令提示符上不起作用,因为使用了单引号,请参阅https://github.com/spring-guides/gs-accessing-data-rest/issues/11


推荐阅读