113 lines
3.5 KiB
Bash
113 lines
3.5 KiB
Bash
|
|
#!/bin/bash
|
||
|
|
set -e
|
||
|
|
|
||
|
|
SELF="$0"
|
||
|
|
COMPOSE_CMD=(docker compose -f docker-compose.yml)
|
||
|
|
|
||
|
|
# ── Parse arguments ──────────────────────────────────────────────────
|
||
|
|
|
||
|
|
FLAG=""
|
||
|
|
|
||
|
|
for arg in "$@"; do
|
||
|
|
case "$arg" in
|
||
|
|
--after-pull)
|
||
|
|
FLAG="$arg" ;;
|
||
|
|
*) ;;
|
||
|
|
esac
|
||
|
|
done
|
||
|
|
|
||
|
|
echo "==> Starting deploy script..."
|
||
|
|
|
||
|
|
# ── Self-update check (first run only) ───────────────────────────────
|
||
|
|
|
||
|
|
if [ "$FLAG" != "--after-pull" ]; then
|
||
|
|
echo "==> Checking for updates..."
|
||
|
|
|
||
|
|
ORIGINAL_HASH=$(md5sum "$SELF" | cut -d' ' -f1)
|
||
|
|
|
||
|
|
git pull --rebase --stat
|
||
|
|
|
||
|
|
UPDATED_HASH=$(md5sum "$SELF" | cut -d' ' -f1)
|
||
|
|
|
||
|
|
PASSTHROUGH_ARGS=("--after-pull")
|
||
|
|
|
||
|
|
if [ "$ORIGINAL_HASH" != "$UPDATED_HASH" ]; then
|
||
|
|
echo "==> Script updated — re-running..."
|
||
|
|
echo
|
||
|
|
"$SELF" "${PASSTHROUGH_ARGS[@]}"
|
||
|
|
exit $?
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo "==> No script changes detected."
|
||
|
|
echo
|
||
|
|
"$SELF" "${PASSTHROUGH_ARGS[@]}"
|
||
|
|
exit $?
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo "==> Running deployment steps..."
|
||
|
|
|
||
|
|
# ── Build + deploy ───────────────────────────────────────────────────
|
||
|
|
|
||
|
|
REPO_UID=$(stat -c '%u' . 2>/dev/null || id -u)
|
||
|
|
REPO_GID=$(stat -c '%g' . 2>/dev/null || id -g)
|
||
|
|
|
||
|
|
DEPLOY_UID=${DEPLOY_UID:-$REPO_UID}
|
||
|
|
DEPLOY_GID=${DEPLOY_GID:-$REPO_GID}
|
||
|
|
|
||
|
|
echo "==> Using production compose file only (docker-compose.yml)."
|
||
|
|
echo "==> Deployment user for build container: ${DEPLOY_UID}:${DEPLOY_GID}"
|
||
|
|
|
||
|
|
if [ ! -f .env ]; then
|
||
|
|
if [ -f .env.example ]; then
|
||
|
|
echo "==> .env missing — creating it from .env.example"
|
||
|
|
cp .env.example .env
|
||
|
|
else
|
||
|
|
echo "ERROR: Missing .env file and no .env.example available."
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo "==> Preparing docker data directories..."
|
||
|
|
mkdir -p docker-data/node_modules
|
||
|
|
|
||
|
|
if [ "$(id -u)" -eq 0 ]; then
|
||
|
|
echo "==> Fixing ownership on writable directories (root mode)..."
|
||
|
|
chown -R "${DEPLOY_UID}:${DEPLOY_GID}" docker-data .output 2>/dev/null || true
|
||
|
|
else
|
||
|
|
echo "==> Skipping host chown (not root)."
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Remove stale build output so we never accidentally serve an old build.
|
||
|
|
rm -rf .output
|
||
|
|
|
||
|
|
# Build/rebuild the builder and serve images (only rebuilds when Dockerfiles change).
|
||
|
|
"${COMPOSE_CMD[@]}" --profile build build
|
||
|
|
|
||
|
|
# Run the Nuxt build (installs deps + builds, writes .output to host).
|
||
|
|
# -T disables pseudo-TTY to prevent interactive prompts during deploy.
|
||
|
|
"${COMPOSE_CMD[@]}" --profile build run --rm -T -u "${DEPLOY_UID}:${DEPLOY_GID}" build
|
||
|
|
|
||
|
|
# Verify the build actually produced output.
|
||
|
|
if [ ! -f .output/server/index.mjs ]; then
|
||
|
|
echo "ERROR: Build did not produce .output/server/index.mjs — aborting deploy"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Defense-in-depth: strip sourcemaps and sourcemap references from production artifacts.
|
||
|
|
echo "==> Hardening build output (strip sourcemaps)..."
|
||
|
|
find .output -type f -name '*.map' -delete
|
||
|
|
find .output -type f \( -name '*.js' -o -name '*.mjs' -o -name '*.css' \) \
|
||
|
|
-exec sed -i \
|
||
|
|
-e '/^[[:space:]]*\/\/# sourceMappingURL=.*$/d' \
|
||
|
|
-e '/^[[:space:]]*\/\*# sourceMappingURL=.*\*\/[[:space:]]*$/d' {} +
|
||
|
|
|
||
|
|
if find .output -type f -name '*.map' -print -quit | grep -q .; then
|
||
|
|
echo "ERROR: Sourcemap artifacts still present in .output — aborting deploy"
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
|
||
|
|
# (Re)start the SSR server with the fresh .output.
|
||
|
|
"${COMPOSE_CMD[@]}" up -d --force-recreate frontend
|
||
|
|
|
||
|
|
echo "==> Deployment complete!"
|