deploy frontend from the ec2 at the end of the pipeline

This commit is contained in:
Joe Lothan 2026-05-25 23:21:50 -04:00
parent 8d62832c1d
commit 8e3907505f
4 changed files with 60 additions and 10 deletions

View file

@ -60,16 +60,25 @@ echo "Total bundles: $TOTAL_BUNDLES"
echo "S3 bucket: $SITE_BUCKET"
echo ""
# Inject TOTAL_BUNDLES into index.html (in a temp copy)
# Build into temp directory
TMPDIR=$(mktemp -d)
cp "$FRONTEND_DIR/index.html" "$TMPDIR/index.html"
cp "$FRONTEND_DIR/site.js" "$TMPDIR/site.js"
cp "$FRONTEND_DIR/bot.html" "$TMPDIR/bot.html"
cp "$FRONTEND_DIR/about.html" "$TMPDIR/about.html"
# Inject TOTAL_BUNDLES
sed -i "s/const TOTAL_BUNDLES = [0-9]*/const TOTAL_BUNDLES = ${TOTAL_BUNDLES}/" "$TMPDIR/index.html"
echo "Injected TOTAL_BUNDLES = $TOTAL_BUNDLES"
# Minify JS (strip comments + whitespace, keep variable names)
if command -v esbuild &>/dev/null; then
esbuild "$TMPDIR/site.js" --minify --outfile="$TMPDIR/site.js" --allow-overwrite
echo "Minified site.js"
else
echo "Warning: esbuild not found, deploying unminified JS"
fi
# Upload
echo "Uploading to s3://$SITE_BUCKET/..."
aws s3 cp "$TMPDIR/index.html" "s3://$SITE_BUCKET/" --content-type "text/html"
@ -80,6 +89,28 @@ 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)
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
done
echo "Deleted $STALE_COUNT stale bundles"
else
echo "No stale bundles found"
fi
# Invalidate CloudFront
if ! $SKIP_INVALIDATION; then
if [ -z "$DIST_ID" ]; then