-
Notifications
You must be signed in to change notification settings - Fork 6
76 lines (62 loc) · 2.71 KB
/
Copy pathcleanup-packages.yml
File metadata and controls
76 lines (62 loc) · 2.71 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
name: Delete Old Container Images
on:
workflow_dispatch: # Allow manual run (USE WITH CAUTION: This script deletes underlying images of multi-arch builds causing manifest errors)
# schedule:
# - cron: "0 0 * * 0" # Run weekly on Sunday at midnight
jobs:
clean-ghcr:
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Bulk Delete Untagged Images
env:
# Use GH_TOKEN explicitly for the CLI to avoid ambiguity
GH_TOKEN: ${{ secrets.GHCR_DELETE_PAT }}
run: |
# Disable immediate exit to handle errors manually
set +e
PKG_NAMES=("meshrf" "meshrf-rf-engine" "meshrf-opentopodata")
OWNER="${{ github.repository_owner }}"
echo "Checking owner type for: $OWNER"
TYPE=$(gh api "users/$OWNER" --jq '.type' 2>/dev/null || echo "Organization")
echo "Owner Type: $TYPE"
if [[ "$TYPE" == "Organization" ]]; then
ENDPOINT="orgs/$OWNER"
else
ENDPOINT="users/$OWNER"
fi
echo "Using endpoint base: $ENDPOINT"
for PKG in "${PKG_NAMES[@]}"; do
echo "----------------------------------------"
echo "Cleaning package: $PKG"
echo "----------------------------------------"
for batch in {1..10}; do
echo "Processing Batch $batch..."
# Fetch IDs
IDS=$(gh api "$ENDPOINT/packages/container/$PKG/versions?per_page=100" \
--jq '.[] | select(.metadata.container.tags | length == 0) | .id' 2>/dev/null)
if [ -z "$IDS" ]; then
echo "✅ No more untagged versions found for $PKG."
break
fi
count=0
for ID in $IDS; do
echo "Attempting to delete version ID: $ID"
# Capture output and exit code
OUTPUT=$(gh api --method DELETE "$ENDPOINT/packages/container/$PKG/versions/$ID" 2>&1)
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo "❌ Failed to delete $ID. Exit Code: $EXIT_CODE"
echo " Error Output: $OUTPUT"
echo " Troubleshooting: Ensure GHCR_DELETE_PAT has 'delete:packages' scope."
else
echo " Deleted successfully."
((count++))
fi
done
echo "batch finished: Processed $count successful deletions."
done
done
# Always succeed the step so we can see the logs
exit 0