Skip to content

Commit 2e59a29

Browse files
authored
chore: add clean old release (#239)
1 parent 46103a3 commit 2e59a29

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

.github/workflows/release-aws-s3.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,47 @@ jobs:
192192
echo "Uploading releases.json to s3://$BUCKET/$KEY"
193193
aws s3 cp meta/releases.json "s3://$BUCKET/$KEY" \
194194
--content-type 'application/json' --cache-control 'no-store'
195+
196+
- name: Cleanup old release folders (by last modified)
197+
env:
198+
BUCKET: ${{ env.BUCKET }}
199+
REPO_NAME: ${{ env.REPO_NAME }}
200+
KEEP: ${{ env.MAX_HISTORY }}
201+
run: |
202+
set -euo pipefail
203+
KEEP="${KEEP:-20}"
204+
PREFIX="$REPO_NAME/releases/"
205+
echo "Cleaning up to keep only $KEEP newest folders under s3://$BUCKET/$PREFIX"
206+
207+
# List top-level release prefixes (folders), exclude 'latest/'
208+
mapfile -t FOLDERS < <(aws s3api list-objects-v2 \
209+
--bucket "$BUCKET" \
210+
--prefix "$PREFIX" \
211+
--delimiter '/' \
212+
--query 'CommonPrefixes[].Prefix' \
213+
--output text | tr '\t' '\n' | sed -e "s|^$PREFIX||" | grep -E '.+/' | grep -v '^latest/$')
214+
215+
if [ "${#FOLDERS[@]}" -le "$KEEP" ]; then
216+
echo "Nothing to delete. Found ${#FOLDERS[@]} folders."
217+
exit 0
218+
fi
219+
220+
# Build list: "LastModified folder/" and sort descending by time
221+
TIMED_LIST=$(for f in "${FOLDERS[@]}"; do
222+
lm=$(aws s3api list-objects-v2 --bucket "$BUCKET" --prefix "$PREFIX$f" \
223+
--query "reverse(sort_by(Contents,&LastModified))[:1][0].LastModified" \
224+
--output text)
225+
if [ "$lm" != "None" ] && [ -n "$lm" ]; then
226+
echo "$lm $f"
227+
fi
228+
done | sort -r)
229+
230+
count=0
231+
while read -r lm f; do
232+
[ -z "$f" ] && continue
233+
count=$((count+1))
234+
if [ "$count" -gt "$KEEP" ]; then
235+
echo "Deleting folder s3://$BUCKET/$PREFIX$f (last modified $lm)"
236+
aws s3 rm "s3://$BUCKET/$PREFIX$f" --recursive
237+
fi
238+
done <<< "$TIMED_LIST"

0 commit comments

Comments
 (0)