diff --git a/pipeline/06_frontend/deploy.sh b/pipeline/06_frontend/deploy.sh index 0f78ede..750db9a 100755 --- a/pipeline/06_frontend/deploy.sh +++ b/pipeline/06_frontend/deploy.sh @@ -90,25 +90,20 @@ echo "Uploaded 4 files" rm -rf "$TMPDIR" # Clean up stale bundles from previous runs -echo "Cleaning stale bundles above $TOTAL_BUNDLES..." -STALE=$(aws s3api list-objects-v2 --bucket "$SITE_BUCKET" --prefix "tabs/" --query "Contents[].Key" --output text \ - | tr '\t' '\n' \ - | while read -r key; do - num=$(echo "$key" | grep -oP '\d+' || true) - if [ -n "$num" ] && [ "$((10#$num))" -ge "$TOTAL_BUNDLES" ]; then - echo "$key" - fi - done) +# Read old TOTAL_BUNDLES from the live index.html before we overwrite it +OLD_TOTAL=$(aws s3 cp "s3://$SITE_BUCKET/index.html" - 2>/dev/null \ + | grep -oP 'TOTAL_BUNDLES = \K[0-9]+' || echo "0") -if [ -n "$STALE" ]; then - STALE_COUNT=$(echo "$STALE" | wc -l) - echo "Deleting $STALE_COUNT stale bundles..." - echo "$STALE" | while read -r key; do - aws s3 rm "s3://$SITE_BUCKET/$key" --quiet +if [ "$OLD_TOTAL" -gt "$TOTAL_BUNDLES" ]; then + STALE_COUNT=$((OLD_TOTAL - TOTAL_BUNDLES)) + echo "Deleting $STALE_COUNT stale bundles ($TOTAL_BUNDLES to $((OLD_TOTAL - 1)))..." + for i in $(seq "$TOTAL_BUNDLES" "$((OLD_TOTAL - 1))"); do + PADDED=$(printf "%04d" "$i") + aws s3 rm "s3://$SITE_BUCKET/tabs/${PADDED}.json" --quiet done echo "Deleted $STALE_COUNT stale bundles" else - echo "No stale bundles found" + echo "No stale bundles to clean (old=$OLD_TOTAL, new=$TOTAL_BUNDLES)" fi # Invalidate CloudFront