使用 POST 请求触发 Github 操作(Github REST API)

kro*_*c97 9 rest curl github github-actions

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

\n

操作 YAML 文件摘录:

\n
on:\n  # Allows you to run this workflow manually from the Actions tab\n  workflow_dispatch:\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

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

\n

该文件进一步指出:

\n
\n

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

\n
\n

因此,在我的个人帐户设置中的“开发人员设置”\xe2\x86\x92“个人访问令牌”下,\n我创建了一个令牌并授予对所有“存储库”项以及“工作流”项的访问权限。

\n

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

\n
    \n
  • accept:(application/vnd.github.v3+json根据 GitHub 推荐)
  • \n
  • owner: GitHub \xc3\xb5rganization 的名称
  • \n
  • repo: 存储库名称
  • \n
  • workflow_id:使用 GitHub Action YAML 文件的完整文件名(包括结尾.yml),因为我不确定在哪里可以找到 ID。
  • \n
  • ref: main,因为我认为这是指相关的存储库分支(?),并且存储库只有一个main分支并且操作驻留在那里
  • \n
\n

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

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

我得到以下结果:

\n
{\n  "message": "Problems parsing JSON",\n  "documentation_url": "https://docs.github.com/rest/reference/actions#create-a-workflow-dispatch-event"\n}\n
Run Code Online (Sandbox Code Playgroud)\n

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

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

小智 10

按照此说明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