Tobi
Tobi

Reputation: 1902

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

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. enter image description here

Upvotes: 14

Views: 6849

Answers (7)

VonC
VonC

Reputation: 1327184

You also have, in Oct. 2021, gh run cancel

This comes from the GitHub CLI gh 2.2.0 and feature request 3775 (Support canceling workflow runs), implemented in PR 3833 and commit 06c06c8

gh run cancel [<run-id>] [flags]

gh run list can list your workflow runs ID first.

You can use this to cancel all existing run, as noted by Jean-Michaël Celerier and Enthusiastic:

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

# to delete them
`gh run list --json databaseId -q '.[].databaseId' | xargs -d '\n' -n1 gh run delete`

Upvotes: 2

sebres
sebres

Reputation: 820

since edit is impossible - edit queue is persistently full...
Here is a bit rewritten answer of @bertrand-martel.

Because the API uses pagination (default page = 1, per_page = 30), in original answer the first curl could return nothing, if queued/in_progress runs are outside of first 30 rows.


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?status=queued&per_page=100" | \
         jq '.workflow_runs[] | .id'; \
      curl -s -H "Authorization: token $token" \
         "https://api.github.com/repos/$repo/actions/runs?status=in_progress&per_page=100" | \
         jq '.workflow_runs[] | .id')
    set -- $ids
    for i; do curl \
         -H "Authorization: token $token" \
         -X POST "https://api.github.com/repos/$repo/actions/runs/$i/cancel"; done

Note: the API uses pagination, and first curs would return up to 100 queued and 100 in-progress runs as ids, so one have to repeat it (or extend the script using page=N parameter) if more flows need to be canceled.
See REST API endpoints for workflows - GitHub Docs

Upvotes: 0

ThomasEdwin
ThomasEdwin

Reputation: 2145

macOS oneliner (inspired by other answers):

for id in $(gh run list --limit 5000 --jq ".[] | select (.status == \"queued\" ) | .databaseId" --json databaseId,status); do gh run cancel $id; done

# List 5000 runs, filters them by "status == queued", and passes them to "gh run cancel"

Notes:

  1. Install gh cli first by brew install gh and gh auth login
  2. You need to run this in your local repository.

Upvotes: 9

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

Upvotes: 6

Nikita Fedkin
Nikita Fedkin

Reputation: 91

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"

Upvotes: 8

Polor Beer
Polor Beer

Reputation: 2012

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

Upvotes: 2

Bertrand Martel
Bertrand Martel

Reputation: 45432

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

Upvotes: 6

Related Questions