我有一个在
push
或 pull_request
事件上触发的 GitHub Action 工作流程。我有第二个 GitHub Action,它是在 workflow_dispatch
事件上触发的。我想从第一个动作触发第二个动作...
在我的操作 yaml 文件中,我有
permissions:
actions: write
我也有这样的步骤
- name: Run second action
run: |
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$org/$repo/actions/workflows/$workflow_id/dispatches \
-d "{\"ref\": \"${{ github.ref }}\"}"
这使用 API 来针对第二个操作调用
workflow_dispatch
事件。
我遇到的问题是将正确的
ref
值传递给 POST 正文中的操作。我收到错误"message": "No ref found for: refs/pull/1392/merge"
。
对于获得
ref
的正确值有什么建议吗?结帐步骤好像越来越refs/remotes/pull/1392/merge
?
我在https://github.com/benc-uk/workflow-dispatch#ref的文档中找到了答案。我需要通过 API 调用工作流程,如下所示:
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$org/$repo/actions/workflows/$workflow_id/dispatches \
-d "{\"ref\": \"${{ github.event.pull_request.head.ref || github.ref }}\"}" \
--fail-with-body
拉取请求中的
ref
值为 github.event.pull_request.head.ref
。