How to "cancel run" for all scheduled github actions at once?

Viewed 1589

Is there an option to cancel runs for all scheduled github actions in one repository at once. To always go here and cancel runs is a lot of clicking. Thanks

enter image description here

5 Answers

You can use Github Actions API to list workflow runs and cancel workflows runs.

The following script use and to :

  • get ids of workflow runs with status queued or in_progress
  • cancel these workflows runs

To run the script below you will need a personnal access token with repo scope :

token=YOUR_TOKEN
repo=your_user/your_repo

ids=$(curl -s -H "Authorization: token $token" \
     https://api.github.com/repos/$repo/actions/runs | \
     jq '.workflow_runs[] | select([.status] | inside(["in_progress", "queued"])) | .id')
set -- $ids
for i; do curl \
     -H "Authorization: token $token" \
     -X POST "https://api.github.com/repos/$repo/actions/runs/$i/cancel"; done

I created this small script to manually cancel all unfinished jobs for a given branch or pull request:

Example:

  # Set up
  export GITHUB_TOKEN=394ba3b48494ab8f930fbc93
  export GITHUB_REPOSITORY=apache/incubator-superset

  # Cancel previous jobs for a PR
  ./cancel_github_workflows.py 1042

  # Cancel previous jobs for a branch
  ./cancel_github_workflows.py my-branch

  # Cancel all jobs including the last ones, this is useful
  # when you have closed a PR or deleted a branch and want
  # to cancel all its jobs.
  ./cancel_github_workflows.py 1024 --include-last

gh run list + powershell:

gh run list --limit 5000 --json status,databaseId -q ".[] | select (.status == \`"queued\`" ) | .databaseId" | ForEach-Object -Process { gh run cancel $_ }

Gets first 5000 workflows, filters it by "status" == "queued" and passes it to "gh run cancel"

Here is my GNU one-liner for cancelling all current runs:

$ gh run list --json databaseId -q '.[].databaseId' \
| xargs -d '\n' -n1 gh run cancel
Related