Skip to content

Instantly share code, notes, and snippets.

@shakefu
Created November 6, 2023 05:08
Show Gist options
  • Save shakefu/bf492c2518a3e803501129dfbc556b36 to your computer and use it in GitHub Desktop.
Save shakefu/bf492c2518a3e803501129dfbc556b36 to your computer and use it in GitHub Desktop.
GitHub Actions: Cancel a running workflow from within the workflow
- name: Cancel workflow
uses: actions/github-script@v6
if: steps.check.outputs.cancel == 'true'
with:
result-encoding: string
retries: 3
script: |
github.rest.actions.cancelWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId,
});
let count = 180;
const check = () => {
count -= 1;
github.rest.actions.getWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.runId,
}).then((run) => {
if (run.conclusion || run.status == "completed") {
return
}
console.log("Waiting for workflow to cancel")
console.log(run)
if (count > 0) {
setTimeout(check, 1000)
} else {
console.log("Workflow did not cancel in time")
}
})
}
setTimeout(check, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment