diff --git a/scripts/cancel-all-ci-runs.sh b/scripts/cancel-all-ci-runs.sh index 187c53f6..8ccbec24 100755 --- a/scripts/cancel-all-ci-runs.sh +++ b/scripts/cancel-all-ci-runs.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail # Check if gh is installed if ! command -v gh &> /dev/null @@ -7,6 +8,18 @@ then exit 1 fi +# Allow override with REPO=owner/repo +REPO="${REPO:-}" +if [ -z "$REPO" ]; then + # try to discover the repo from the current git folder + REPO="$(gh repo view --json nameWithOwner -q .nameWithOwner 2>/dev/null || true)" + if [ -z "$REPO" ]; then + echo "Unable to determine repository. Either run this inside the repo or set REPO=owner/repo" + exit 1 + fi +fi + +echo "Repo: $REPO" echo "Fetching active runs..." # Function to cancel runs by status @@ -14,14 +27,13 @@ cancel_runs() { local status=$1 echo "Cancelling $status runs..." # Use --limit 1000 to capture up to 1000 runs (default is 30) - ids=$(gh run list --status "$status" --limit 1000 --json databaseId -q '.[].databaseId') + ids=$(gh run list --repo "$REPO" --status "$status" --limit 1000 --json databaseId -q '.[].databaseId') if [ -z "$ids" ]; then echo "No $status runs found." else - # Pass IDs to xargs to batch cancel commands - # gh run cancel accepts multiple arguments - echo "$ids" | xargs gh run cancel + # Call gh run cancel once per id (some gh versions accept only one id per call) + printf '%s\n' "$ids" | xargs -r -n1 -I{} gh run cancel --repo "$REPO" {} fi }