删除 GitHub Action Workflows 删除流水线
首先安装 gh
工具,并且登录和授权。
#!/usr/bin/env bash
set -euo pipefail
if [ -z "$GH_TOKEN" ]; then
printf "\033[1;33mError: GH_TOKEN must be set\033[0m\n"
exit 1
fi
ORG_NAME="${1:-}"
REPO_NAME="${2:-}"
if [ -z "$REPO_NAME" ]; then
# 使用 IFS 分割 ORG_NAME,并提取 ORG_NAME 和 REPO_NAME
IFS='/' read -r ORG_NAME REPO_NAME <<< "$ORG_NAME"
fi
if [ -z "$ORG_NAME" ] || [ -z "$REPO_NAME" ]; then
printf "\033[1;33mError: Repository not found.\033[0m\n"
exit 1
fi
repo="$ORG_NAME/$REPO_NAME"
url="repos/$repo/actions/runs"
total_deleted=0
delete_id() {
local id=$1
local result=""
if gh api -X DELETE "$url/$id" --silent; then
result="✅: Deleted '$id'"
total_deleted=$((total_deleted + 1))
else
result="❌: Failed '$id'"
echo "An error occurred while deleting ID '$id'. Press Enter to exit."
echo "Total IDs deleted: $total_deleted"
read -n 1 -s -r -p ""
exit 1
fi
printf "%s\n" "$result"
}
while true; do
total_ids=$(gh api "$url" | jq '.workflow_runs | length')
if [[ $total_ids -eq 0 ]]; then
echo "No more IDs to delete. Press Enter to exit."
echo "Total IDs deleted: $total_deleted"
read -n 1 -s -r -p ""
break
fi
gh api "$url" |
jq -r '.workflow_runs[].id' |
while read -r id; do
delete_id "$id"
done
sleep 10
done