Skip to content

Commit 88baddd

Browse files
authored
chore: add previous build cleanup scripts (#1682)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - Chores - Added automated cleanup of preview builds older than seven days to reduce storage usage; runs on non-release pushes and won’t fail the build if cleanup issues occur. - Introduced a tool to optionally remove all pull request preview builds with confirmation and clear summaries. - Updated CI behavior to cancel in-progress runs only for pull requests; pushes and releases are no longer auto-canceled. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent abc22bd commit 88baddd

File tree

4 files changed

+298
-1
lines changed

4 files changed

+298
-1
lines changed

.github/workflows/build-plugin.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,40 @@ jobs:
183183
```
184184
${{ inputs.BASE_URL }}/tag/${{ inputs.TAG }}/dynamix.unraid.net.plg
185185
```
186+
187+
- name: Clean up old preview builds
188+
if: inputs.RELEASE_CREATED == 'false' && github.event_name == 'push'
189+
continue-on-error: true
190+
env:
191+
AWS_ACCESS_KEY_ID: ${{ secrets.CF_ACCESS_KEY_ID }}
192+
AWS_SECRET_ACCESS_KEY: ${{ secrets.CF_SECRET_ACCESS_KEY }}
193+
AWS_DEFAULT_REGION: auto
194+
run: |
195+
echo "🧹 Cleaning up old preview builds (keeping last 7 days)..."
196+
197+
# Calculate cutoff date (7 days ago)
198+
CUTOFF_DATE=$(date -d "7 days ago" +"%Y.%m.%d")
199+
echo "Deleting builds older than: ${CUTOFF_DATE}"
200+
201+
# List and delete old timestamped .txz files
202+
OLD_FILES=$(aws s3 ls "s3://${{ secrets.CF_BUCKET_PREVIEW }}/unraid-api/" \
203+
--endpoint-url ${{ secrets.CF_ENDPOINT }} --recursive | \
204+
grep -E "dynamix\.unraid\.net-[0-9]{4}\.[0-9]{2}\.[0-9]{2}\.[0-9]{4}\.txz" | \
205+
awk '{print $4}' || true)
206+
207+
DELETED_COUNT=0
208+
if [ -n "$OLD_FILES" ]; then
209+
while IFS= read -r file; do
210+
if [[ $file =~ ([0-9]{4}\.[0-9]{2}\.[0-9]{2})\.[0-9]{4}\.txz ]]; then
211+
FILE_DATE="${BASH_REMATCH[1]}"
212+
if [[ "$FILE_DATE" < "$CUTOFF_DATE" ]]; then
213+
echo "Deleting old build: $(basename "$file")"
214+
aws s3 rm "s3://${{ secrets.CF_BUCKET_PREVIEW }}/${file}" \
215+
--endpoint-url ${{ secrets.CF_ENDPOINT }} || true
216+
((DELETED_COUNT++))
217+
fi
218+
fi
219+
done <<< "$OLD_FILES"
220+
fi
221+
222+
echo "✅ Deleted ${DELETED_COUNT} old builds"

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
concurrency:
1010
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
11-
cancel-in-progress: true
11+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
1212

1313
jobs:
1414
test-api:

scripts/cleanup-old-builds.sh

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#!/bin/bash
2+
3+
# Script to clean up old timestamped builds from Cloudflare R2
4+
# This will remove old .txz files with the pattern dynamix.unraid.net-YYYY.MM.DD.HHMM.txz
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+
BLUE='\033[0;34m'
13+
NC='\033[0m' # No Color
14+
15+
echo -e "${YELLOW}🧹 Cloudflare Old Build Cleanup Script${NC}"
16+
echo "This will delete old timestamped .txz builds from the preview bucket"
17+
echo ""
18+
19+
# Check for required environment variables
20+
if [ -z "$CF_ACCESS_KEY_ID" ] || [ -z "$CF_SECRET_ACCESS_KEY" ] || [ -z "$CF_ENDPOINT" ] || [ -z "$CF_BUCKET_PREVIEW" ]; then
21+
echo -e "${RED}❌ Error: Missing required environment variables${NC}"
22+
echo "Please set the following environment variables:"
23+
echo " - CF_ACCESS_KEY_ID"
24+
echo " - CF_SECRET_ACCESS_KEY"
25+
echo " - CF_ENDPOINT"
26+
echo " - CF_BUCKET_PREVIEW"
27+
exit 1
28+
fi
29+
30+
# Configure AWS CLI for Cloudflare R2
31+
export AWS_ACCESS_KEY_ID="$CF_ACCESS_KEY_ID"
32+
export AWS_SECRET_ACCESS_KEY="$CF_SECRET_ACCESS_KEY"
33+
export AWS_DEFAULT_REGION="auto"
34+
35+
echo "Endpoint: $CF_ENDPOINT"
36+
echo "Bucket: $CF_BUCKET_PREVIEW"
37+
echo ""
38+
39+
# Optional: specify number of days to keep (default: 7)
40+
KEEP_DAYS=${1:-7}
41+
echo -e "${BLUE}Keeping builds from the last ${KEEP_DAYS} days${NC}"
42+
echo ""
43+
44+
# Calculate cutoff date
45+
if [[ "$OSTYPE" == "darwin"* ]]; then
46+
# macOS
47+
CUTOFF_DATE=$(date -v -${KEEP_DAYS}d +"%Y.%m.%d")
48+
else
49+
# Linux
50+
CUTOFF_DATE=$(date -d "${KEEP_DAYS} days ago" +"%Y.%m.%d")
51+
fi
52+
53+
echo "Cutoff date: ${CUTOFF_DATE} (will delete builds older than this)"
54+
echo ""
55+
56+
# List all timestamped TXZ files in the unraid-api directory
57+
echo -e "${YELLOW}📋 Scanning for old builds...${NC}"
58+
59+
# Get all .txz files matching the pattern
60+
ALL_FILES=$(aws s3 ls "s3://${CF_BUCKET_PREVIEW}/unraid-api/" --endpoint-url "$CF_ENDPOINT" --recursive | \
61+
grep -E "dynamix\.unraid\.net-[0-9]{4}\.[0-9]{2}\.[0-9]{2}\.[0-9]{4}\.txz" | \
62+
awk '{print $4}' || true)
63+
64+
if [ -z "$ALL_FILES" ]; then
65+
echo -e "${GREEN}✅ No timestamped builds found${NC}"
66+
exit 0
67+
fi
68+
69+
# Filter files older than cutoff
70+
OLD_FILES=""
71+
KEEP_FILES=""
72+
TOTAL_COUNT=0
73+
OLD_COUNT=0
74+
75+
while IFS= read -r file; do
76+
((TOTAL_COUNT++))
77+
# Extract date from filename (format: YYYY.MM.DD.HHMM)
78+
if [[ $file =~ ([0-9]{4}\.[0-9]{2}\.[0-9]{2})\.[0-9]{4}\.txz ]]; then
79+
FILE_DATE="${BASH_REMATCH[1]}"
80+
81+
# Compare dates (string comparison works for YYYY.MM.DD format)
82+
if [[ "$FILE_DATE" < "$CUTOFF_DATE" ]]; then
83+
OLD_FILES="${OLD_FILES}${file}\n"
84+
((OLD_COUNT++))
85+
else
86+
KEEP_FILES="${KEEP_FILES}${file}\n"
87+
fi
88+
fi
89+
done <<< "$ALL_FILES"
90+
91+
echo "Found ${TOTAL_COUNT} total timestamped builds"
92+
echo "Will delete ${OLD_COUNT} old builds"
93+
echo "Will keep $((TOTAL_COUNT - OLD_COUNT)) recent builds"
94+
echo ""
95+
96+
if [ "$OLD_COUNT" -eq 0 ]; then
97+
echo -e "${GREEN}✅ No old builds to delete${NC}"
98+
exit 0
99+
fi
100+
101+
# Show sample of files to be deleted
102+
echo -e "${YELLOW}Sample of files to be deleted:${NC}"
103+
echo -e "$OLD_FILES" | head -5
104+
if [ "$OLD_COUNT" -gt 5 ]; then
105+
echo "... and $((OLD_COUNT - 5)) more"
106+
fi
107+
echo ""
108+
109+
# Confirmation prompt
110+
read -p "Are you sure you want to delete these ${OLD_COUNT} old builds? (yes/no): " -r
111+
echo ""
112+
113+
if [[ ! $REPLY =~ ^[Yy]es$ ]]; then
114+
echo -e "${YELLOW}⚠️ Cleanup cancelled${NC}"
115+
exit 0
116+
fi
117+
118+
# Delete old files
119+
DELETED=0
120+
FAILED=0
121+
122+
echo -e "${YELLOW}🗑️ Deleting old builds...${NC}"
123+
while IFS= read -r file; do
124+
if [ -n "$file" ]; then
125+
echo -n "Deleting $(basename "$file")... "
126+
127+
if aws s3 rm "s3://${CF_BUCKET_PREVIEW}/${file}" \
128+
--endpoint-url "$CF_ENDPOINT" \
129+
>/dev/null 2>&1; then
130+
echo -e "${GREEN}${NC}"
131+
((DELETED++))
132+
else
133+
echo -e "${RED}${NC}"
134+
((FAILED++))
135+
fi
136+
fi
137+
done <<< "$(echo -e "$OLD_FILES")"
138+
139+
echo ""
140+
echo -e "${GREEN}🎉 Cleanup complete!${NC}"
141+
echo " - Deleted: $DELETED old build(s)"
142+
if [ $FAILED -gt 0 ]; then
143+
echo -e " - Failed: ${RED}$FAILED${NC} build(s)"
144+
fi
145+
146+
# Show remaining recent builds
147+
echo ""
148+
echo -e "${BLUE}📦 Recent builds kept:${NC}"
149+
echo -e "$KEEP_FILES" | head -5
150+
KEEP_COUNT=$(echo -e "$KEEP_FILES" | grep -c . || echo 0)
151+
if [ "$KEEP_COUNT" -gt 5 ]; then
152+
echo "... and $((KEEP_COUNT - 5)) more"
153+
fi

scripts/cleanup-pr-builds.sh

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/bin/bash
2+
3+
# Script to delete all PR builds from Cloudflare R2
4+
# This will remove all artifacts under unraid-api/tag/PR* paths
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+
echo -e "${YELLOW}🧹 Cloudflare PR Build Cleanup Script${NC}"
15+
echo "This will delete all PR builds from the preview bucket"
16+
echo ""
17+
18+
# Check for required environment variables
19+
if [ -z "$CF_ACCESS_KEY_ID" ] || [ -z "$CF_SECRET_ACCESS_KEY" ] || [ -z "$CF_ENDPOINT" ] || [ -z "$CF_BUCKET_PREVIEW" ]; then
20+
echo -e "${RED}❌ Error: Missing required environment variables${NC}"
21+
echo "Please set the following environment variables:"
22+
echo " - CF_ACCESS_KEY_ID"
23+
echo " - CF_SECRET_ACCESS_KEY"
24+
echo " - CF_ENDPOINT"
25+
echo " - CF_BUCKET_PREVIEW"
26+
echo ""
27+
echo "You can source them from your .env file or export them manually:"
28+
echo " export CF_ACCESS_KEY_ID='your-key-id'"
29+
echo " export CF_SECRET_ACCESS_KEY='your-secret-key'"
30+
echo " export CF_ENDPOINT='your-endpoint'"
31+
echo " export CF_BUCKET_PREVIEW='your-bucket'"
32+
exit 1
33+
fi
34+
35+
# Configure AWS CLI for Cloudflare R2
36+
export AWS_ACCESS_KEY_ID="$CF_ACCESS_KEY_ID"
37+
export AWS_SECRET_ACCESS_KEY="$CF_SECRET_ACCESS_KEY"
38+
export AWS_DEFAULT_REGION="auto"
39+
40+
echo "Endpoint: $CF_ENDPOINT"
41+
echo "Bucket: $CF_BUCKET_PREVIEW"
42+
echo ""
43+
44+
# List all PR directories
45+
echo -e "${YELLOW}📋 Listing all PR builds...${NC}"
46+
PR_DIRS=$(aws s3 ls "s3://${CF_BUCKET_PREVIEW}/unraid-api/tag/" --endpoint-url "$CF_ENDPOINT" 2>/dev/null | grep "PRE PR" | awk '{print $2}' || true)
47+
48+
if [ -z "$PR_DIRS" ]; then
49+
echo -e "${GREEN}✅ No PR builds found to clean up${NC}"
50+
exit 0
51+
fi
52+
53+
# Count PR builds
54+
PR_COUNT=$(echo "$PR_DIRS" | wc -l | tr -d ' ')
55+
echo -e "Found ${YELLOW}${PR_COUNT}${NC} PR build(s):"
56+
echo "$PR_DIRS"
57+
echo ""
58+
59+
# Confirmation prompt
60+
read -p "Are you sure you want to delete ALL these PR builds? (yes/no): " -r
61+
echo ""
62+
63+
if [[ ! $REPLY =~ ^[Yy]es$ ]]; then
64+
echo -e "${YELLOW}⚠️ Cleanup cancelled${NC}"
65+
exit 0
66+
fi
67+
68+
# Delete each PR directory
69+
DELETED=0
70+
FAILED=0
71+
72+
for PR_DIR in $PR_DIRS; do
73+
PR_NUM=${PR_DIR%/} # Remove trailing slash
74+
echo -n "Deleting $PR_NUM... "
75+
76+
if aws s3 rm "s3://${CF_BUCKET_PREVIEW}/unraid-api/tag/${PR_NUM}" \
77+
--recursive \
78+
--endpoint-url "$CF_ENDPOINT" \
79+
>/dev/null 2>&1; then
80+
echo -e "${GREEN}${NC}"
81+
((DELETED++))
82+
else
83+
echo -e "${RED}${NC}"
84+
((FAILED++))
85+
fi
86+
done
87+
88+
echo ""
89+
echo -e "${GREEN}🎉 Cleanup complete!${NC}"
90+
echo " - Deleted: $DELETED PR build(s)"
91+
if [ $FAILED -gt 0 ]; then
92+
echo -e " - Failed: ${RED}$FAILED${NC} PR build(s)"
93+
fi
94+
95+
# Optional: List remaining items to verify
96+
echo ""
97+
echo -e "${YELLOW}📋 Verifying cleanup...${NC}"
98+
REMAINING=$(aws s3 ls "s3://${CF_BUCKET_PREVIEW}/unraid-api/tag/" --endpoint-url "$CF_ENDPOINT" 2>/dev/null | grep -c "PRE PR" || true)
99+
# Ensure REMAINING is a valid number
100+
REMAINING=${REMAINING:-0}
101+
echo "Remaining PR builds: $REMAINING"
102+
103+
if [ "$REMAINING" -eq 0 ]; then
104+
echo -e "${GREEN}✅ All PR builds successfully removed${NC}"
105+
else
106+
echo -e "${YELLOW}⚠️ Some PR builds may still exist${NC}"
107+
fi

0 commit comments

Comments
 (0)