1+ #! /bin/bash
2+
3+ # Script to cancel all running GitHub Actions workflows
4+ # Requires GitHub CLI (gh) to be installed and authenticated
5+
6+ set -e
7+
8+ # Colors for output
9+ RED=' \033[0;31m'
10+ GREEN=' \033[0;32m'
11+ YELLOW=' \033[1;33m'
12+ NC=' \033[0m' # No Color
13+
14+ # Function to print colored output
15+ print_status () {
16+ echo -e " ${GREEN} [INFO]${NC} $1 "
17+ }
18+
19+ print_warning () {
20+ echo -e " ${YELLOW} [WARN]${NC} $1 "
21+ }
22+
23+ print_error () {
24+ echo -e " ${RED} [ERROR]${NC} $1 "
25+ }
26+
27+ # Check if gh CLI is installed
28+ if ! command -v gh & > /dev/null; then
29+ print_error " GitHub CLI (gh) is not installed. Please install it first:"
30+ echo " brew install gh"
31+ echo " Or visit: https://cli.github.com/"
32+ exit 1
33+ fi
34+
35+ # Check if authenticated
36+ if ! gh auth status & > /dev/null; then
37+ print_error " GitHub CLI is not authenticated. Please run:"
38+ echo " gh auth login"
39+ exit 1
40+ fi
41+
42+ # Get repository info
43+ REPO=$( gh repo view --json owner,name -q ' .owner.login + "/" + .name' )
44+ if [[ -z " $REPO " ]]; then
45+ print_error " Could not determine repository. Make sure you're in a GitHub repository."
46+ exit 1
47+ fi
48+
49+ print_status " Working with repository: $REPO "
50+
51+ # Get all active workflows (both queued and in-progress)
52+ print_status " Fetching active workflows (queued and in-progress)..."
53+ QUEUED_WORKFLOWS=$( gh run list --status queued --json databaseId,displayTitle,headBranch,status --limit 100)
54+ IN_PROGRESS_WORKFLOWS=$( gh run list --status in_progress --json databaseId,displayTitle,headBranch,status --limit 100)
55+
56+ # Combine both lists
57+ ALL_WORKFLOWS=$( echo " $QUEUED_WORKFLOWS $IN_PROGRESS_WORKFLOWS " | jq -s ' add | unique_by(.databaseId)' )
58+
59+ if [[ " $ALL_WORKFLOWS " == " []" ]]; then
60+ print_status " No active workflows found."
61+ exit 0
62+ fi
63+
64+ # Parse and display active workflows
65+ echo
66+ print_warning " Found active workflows:"
67+ echo " $ALL_WORKFLOWS " | jq -r ' .[] | " - \(.displayTitle) (Branch: \(.headBranch), Status: \(.status), ID: \(.databaseId))"'
68+
69+ echo
70+ read -p " Do you want to cancel ALL these workflows? (y/N): " -n 1 -r
71+ echo
72+
73+ if [[ ! $REPLY =~ ^[Yy]$ ]]; then
74+ print_status " Cancelled by user."
75+ exit 0
76+ fi
77+
78+ # Cancel each workflow
79+ print_status " Cancelling workflows..."
80+ CANCELLED_COUNT=0
81+ FAILED_COUNT=0
82+
83+ while IFS= read -r WORKFLOW_ID; do
84+ if [[ -n " $WORKFLOW_ID " ]]; then
85+ print_status " Cancelling workflow ID: $WORKFLOW_ID "
86+ if gh run cancel " $WORKFLOW_ID " 2> /dev/null; then
87+ (( CANCELLED_COUNT++ ))
88+ else
89+ print_error " Failed to cancel workflow ID: $WORKFLOW_ID "
90+ (( FAILED_COUNT++ ))
91+ fi
92+ fi
93+ done < <( echo " $ALL_WORKFLOWS " | jq -r ' .[].databaseId' )
94+
95+ echo
96+ print_status " Summary:"
97+ echo " - Cancelled: $CANCELLED_COUNT workflows"
98+ if [[ $FAILED_COUNT -gt 0 ]]; then
99+ echo " - Failed: $FAILED_COUNT workflows"
100+ fi
101+
102+ print_status " Done!"
103+
104+ # Optional: Show remaining active workflows
105+ echo
106+ print_status " Checking for any remaining active workflows..."
107+ REMAINING_QUEUED=$( gh run list --status queued --json databaseId --limit 10)
108+ REMAINING_IN_PROGRESS=$( gh run list --status in_progress --json databaseId --limit 10)
109+ REMAINING_ALL=$( echo " $REMAINING_QUEUED $REMAINING_IN_PROGRESS " | jq -s ' add | unique_by(.databaseId)' )
110+
111+ if [[ " $REMAINING_ALL " == " []" ]]; then
112+ print_status " All workflows successfully cancelled."
113+ else
114+ REMAINING_COUNT=$( echo " $REMAINING_ALL " | jq ' . | length' )
115+ print_warning " Still $REMAINING_COUNT workflows active (may take a moment to update status)"
116+ fi
0 commit comments