sai*_*eni 20 github github-api github-enterprise github-api-v3 github-actions
我正在触发通过 github 的 Rest api 运行的工作流程。但 github 不会在响应正文中发送任何数据 (204)。如何获取触发请求的运行 ID?我知道getRunsListapi,它会返回工作流 id 的运行,然后我可以获得最新的运行,但是当几乎同时提交两个请求时,这可能会导致问题。
Ber*_*tel 22
目前无法在调度响应本身中获取与调度 API 调用关联的 run_id,但如果您可以稍微编辑一下工作流文件,则有一种方法可以找到它。
您需要使用input如下方式调度工作流程:
curl "https://api.github.com/repos/$OWNER/$REPO/actions/workflows/$WORKFLOW/dispatches" -s \
-H "Authorization: Token $TOKEN" \
-d '{
"ref":"master",
"inputs":{
"id":"12345678"
}
}'
Run Code Online (Sandbox Code Playgroud)
input还可以使用可选的(此处命名)编辑工作流程 yaml 文件id。另外,将其作为第一个作业,该作业具有与输入值同名的单个步骤id(这就是我们使用 API 取回 id 的方式!):
name: ID Example
on:
workflow_dispatch:
inputs:
id:
description: 'run identifier'
required: false
jobs:
id:
name: Workflow ID Provider
runs-on: ubuntu-latest
steps:
- name: ${{github.event.inputs.id}}
run: echo run identifier ${{ inputs.id }}
Run Code Online (Sandbox Code Playgroud)
这里的技巧是使用name: ${{github.event.inputs.id}}
https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
那么流程如下:
在本例中使用随机值运行调度 API 调用以及input命名id
POST https://api.github.com/repos/$OWNER/$REPO/actions/workflows/$WORKFLOW/dispatches
Run Code Online (Sandbox Code Playgroud)
在循环中获取自现在负 5 分钟以来创建的运行(增量是为了避免任何计时问题):
GET https://api.github.com/repos/$OWNER/$REPO/actions/runs?created=>$run_date_filter
Run Code Online (Sandbox Code Playgroud)
在运行 API 响应中,您将得到一个jobs_url您将调用的:
GET https://api.github.com/repos/$OWNER/$REPO/actions/runs/[RUN_ID]/jobs
Run Code Online (Sandbox Code Playgroud)
上面的作业 API 调用返回作业列表,因为您已将作业声明id为第一个作业,所以它将位于第一个位置。它还为您提供了步骤的steps说明。name像这样的东西:
POST https://api.github.com/repos/$OWNER/$REPO/actions/workflows/$WORKFLOW/dispatches
Run Code Online (Sandbox Code Playgroud)
该步骤name的id目的是返回您的输入值,因此您可以安全地确认这次运行是由您的调度调用触发的
这是此流程在python中的实现,它将返回工作流程运行 ID:
GET https://api.github.com/repos/$OWNER/$REPO/actions/runs?created=>$run_date_filter
Run Code Online (Sandbox Code Playgroud)
样本输出
$ python3 github_action_dispatch_runid.py
dispatch workflow status: 204 | workflow identifier: Z7YPF6DD1YP2PTM
get jobs_url https://api.github.com/repos/OWNER/REPO/actions/runs/1321463229/jobs
get jobs_url https://api.github.com/repos/OWNER/REPO/actions/runs/1321463229/jobs
get jobs_url https://api.github.com/repos/OWNER/REPO/actions/runs/1321463229/jobs
get jobs_url https://api.github.com/repos/OWNER/REPO/actions/runs/1321475221/jobs
waiting for steps to be executed...
get jobs_url https://api.github.com/repos/OWNER/REPO/actions/runs/1321463229/jobs
get jobs_url https://api.github.com/repos/OWNER/REPO/actions/runs/1321475221/jobs
waiting for steps to be executed...
get jobs_url https://api.github.com/repos/OWNER/REPO/actions/runs/1321463229/jobs
get jobs_url https://api.github.com/repos/OWNER/REPO/actions/runs/1321475221/jobs
waiting for steps to be executed...
get jobs_url https://api.github.com/repos/OWNER/REPO/actions/runs/1321463229/jobs
get jobs_url https://api.github.com/repos/OWNER/REPO/actions/runs/1321475221/jobs
get jobs_url https://api.github.com/repos/OWNER/REPO/actions/runs/1321463229/jobs
workflow_id: 1321475221
Run Code Online (Sandbox Code Playgroud)
如果有一种方法可以通过 API 检索工作流输入,那就更容易了,但目前还没有办法做到这一点
请注意,在工作流文件中,我使用${{github.event.inputs.id}}because${{inputs.id}}不起作用。inputs当我们使用它作为步骤名称时,它似乎没有被评估
| 归档时间: |
|
| 查看次数: |
9713 次 |
| 最近记录: |