我希望能够根据条件取消当前运行的
workflow
。
我尝试使用this这样的动作
- name: cancel workflow if none of files we look for changes has not changed
if: steps.changed-files.outputs.any_changed != 'true'
uses: styfle/[email protected]
with:
workflow_id: ${{ github.workflow_id }}
access_token: ${{ github.token }}
但是从日志中可以看出:
Found token: yes
Found workflow_id: ["12345567"]
Found 3 runs total.
Found 0 runs to cancel.
它仅取消其他同时运行的工作流程。
有一个简单的方法可以解决这个问题还是我应该求助于 GH API?
编辑:我已经在应该取消其余工作流程的步骤中尝试过此操作
gh api --method POST -H "Accept: application/vnd.github+json" https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/cancel
但是它只是返回一个
{}
并且执行继续......
这是解决这个问题的一种方法
- name: cancel run if none of files we look for changes has changed
if: steps.changed-files.outputs.any_changed != 'true'
uses: andymckay/[email protected]
# needed since the run is not cancelled immediately
- name: wait for run cancellation
if: steps.changed-files.outputs.any_changed != 'true'
shell: bash
run: while true; do echo "Waiting for job to be cancelled"; sleep 5; done
actions/github-script
操作和预先验证的 octokit
客户端调用 GitHub REST API 来实现此目的的方法。
- uses: actions/github-script@v6
with:
script: |
const delay = ms => new Promise(res => setTimeout(res, ms));
github.rest.actions.cancelWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId
});
while (true) {
core.info('Waiting for workflow to cancel ...');
await delay(5000);
}