github - 使用 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
accept
:(application/vnd.github.v3+json
根据 GitHub 推荐)owner
: GitHub 组织的名称repo
:存储库的名称workflow_id
:使用 GitHub Action YAML 文件的完整文件名(包括结尾,.yml
),因为我不确定在哪里可以找到 ID。ref
:main
,因为我认为这指的是相关的存储库分支(?),并且 repo 只有一个main
分支,并且 Action 驻留在那里
使用文档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
- 我是否需要以不同方式配置此过程的 GitHub 端?
- 我的
curl
说法有错吗? - 有什么方法可以检查“问题[atic] JSON”以找出发生了什么?
解决方案
按照此说明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
推荐阅读
- mysql - 如何在laravel中访问具有两个外键的表的数据
- python - 将熊猫数据框从一个jupyter笔记本复制到另一个的最佳方法是什么
- sql-server - ASP.NET 应用程序使用唯一的活动目录凭据连接到远程 SQL Server
- android - 完全透明的状态栏在某些设备上不起作用
- c# - QnA Api 返回响应代码 200 OK,无论 KB 中是否存在匹配项
- c# - 我可以创建 C# 独立应用程序而不是在 Windows 中安装而不运行吗?
- java - 哪些版本的 Atlassian 插件 SDK 支持哪些版本的 confluence?
- sql - 如何按家庭计算孩子的数量并将其放在oracle中的父行中
- python - 如何在 django 中保存一个模型的 3 种形式?
- javascript - 将 STEP 文件导入到 three.js / 在 JS 中将 STEP 转换为 STL?