forked from koalaphils/docker-php
-
Notifications
You must be signed in to change notification settings - Fork 0
127 lines (109 loc) · 4.31 KB
/
Copy pathgithub-cache-cleanup.yml
File metadata and controls
127 lines (109 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
name: Purge All GitHub Caches
on:
workflow_dispatch:
workflow_call:
permissions:
actions: write
contents: read
jobs:
purge-cache:
runs-on: ubuntu-latest
steps:
- name: Verify GH CLI
run: |
set -euo pipefail
if ! command -v gh >/dev/null 2>&1; then
echo "gh CLI not found. Installing..."
curl -fsSL https://github.com/cli/cli/releases/latest/download/gh_$(uname -s)_amd64.tar.gz -o /tmp/gh.tar.gz
tar -xzf /tmp/gh.tar.gz -C /tmp
sudo /tmp/gh_*/bin/gh --version || true
fi
gh --version
- name: Purge all caches
if: github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' || github.run_attempt > 1
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
echo "========================================="
echo "🗑️ Purging All GitHub Caches"
echo "Repository: $GITHUB_REPOSITORY"
echo "Started at: $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
echo "========================================="
TOTAL_DELETED=0
TOTAL_FAILED=0
ITERATION=1
MAX_ITERATIONS=50
# Helper: delete single cache id with retries
delete_cache() {
local id="$1"
local attempt=1
local max_attempts=3
while [ "$attempt" -le "$max_attempts" ]; do
if gh api --method DELETE "repos/$GITHUB_REPOSITORY/actions/caches/$id" --silent >/dev/null 2>&1; then
return 0
fi
sleep $((attempt * 2))
attempt=$((attempt + 1))
done
return 1
}
while true; do
echo "-----------------------------------------"
echo "🔄 Iteration $ITERATION"
echo "-----------------------------------------"
# Fetch all cache ids using pagination; output one id per line
if ! OUTPUT=$(gh api --paginate "repos/$GITHUB_REPOSITORY/actions/caches" --jq '.actions_caches[].id' 2>&1); then
echo "❌ Failed to list caches"
echo "Output: $OUTPUT"
exit 1
fi
# Read into array safely
mapfile -t CACHE_IDS < <(printf '%s\n' "$OUTPUT")
# Trim and filter empties
FILTERED=()
for id in "${CACHE_IDS[@]}"; do
id_trimmed="$(printf '%s' "$id" | tr -d '[:space:]')"
if [ -n "$id_trimmed" ]; then
FILTERED+=("$id_trimmed")
fi
done
CACHE_IDS=("${FILTERED[@]}")
if [ ${#CACHE_IDS[@]} -eq 0 ]; then
echo "No more caches found. Done!"
break
fi
echo "Found ${#CACHE_IDS[@]} cache(s) to delete"
echo ""
COUNTER=0
for cache_id in "${CACHE_IDS[@]}"; do
COUNTER=$((COUNTER+1))
echo "[$COUNTER/${#CACHE_IDS[@]}] Deleting cache ID: $cache_id"
if delete_cache "$cache_id"; then
echo " ✅ Deleted $cache_id"
TOTAL_DELETED=$((TOTAL_DELETED+1))
else
echo " ❌ Failed to delete $cache_id"
TOTAL_FAILED=$((TOTAL_FAILED+1))
fi
done
echo ""
echo "Iteration $ITERATION summary: deleted $COUNTER this pass"
echo "Total deleted so far: $TOTAL_DELETED"
echo "Total failed so far: $TOTAL_FAILED"
echo ""
ITERATION=$((ITERATION+1))
if [ "$ITERATION" -gt "$MAX_ITERATIONS" ]; then
echo "⚠️ Reached max iterations ($MAX_ITERATIONS). Stopping."
break
fi
# Small pause to avoid hitting rate limits
sleep 1
done
echo ""
echo "========================================="
echo "✅ Purge Completed!"
echo "Ended at: $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
echo "Total deleted: $TOTAL_DELETED"
echo "Total failed: $TOTAL_FAILED"
echo "========================================="