Files
Project_Velocity/infrastructure/desineuron_ingress/deploy_velocity_site.sh
2026-04-20 01:43:39 +05:30

104 lines
3.1 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
APP_ROOT="${APP_ROOT:-/opt/desineuron-velocity-site}"
REPO_URL="${REPO_URL:-https://git.desineuron.in/sagnik/Project_Velocity.git}"
BRANCH="${BRANCH:-main}"
REPO_DIR="${REPO_DIR:-$APP_ROOT/repo}"
APP_DIR="${APP_DIR:-$REPO_DIR/app}"
BUILD_DIR="${BUILD_DIR:-$APP_DIR/dist}"
SERVE_ROOT="${SERVE_ROOT:-/var/www/velocity.desineuron.in}"
CURRENT_DIR="${CURRENT_DIR:-$SERVE_ROOT/current}"
STATE_DIR="${STATE_DIR:-$APP_ROOT/state}"
REVISION_FILE="${REVISION_FILE:-$STATE_DIR/current_revision.txt}"
PERSISTENT_VIDEO_DIR="${PERSISTENT_VIDEO_DIR:-$APP_ROOT/shared/videos}"
BACKEND_SERVICE="${BACKEND_SERVICE:-desineuron-velocity-backend}"
BACKEND_HEALTH_URL="${BACKEND_HEALTH_URL:-http://127.0.0.1:8001/health}"
BACKEND_HEALTH_TIMEOUT_S="${BACKEND_HEALTH_TIMEOUT_S:-60}"
RUN_BACKEND_RESTART="${RUN_BACKEND_RESTART:-1}"
RUN_DB_MIGRATIONS="${RUN_DB_MIGRATIONS:-0}"
DB_MIGRATION_CMD="${DB_MIGRATION_CMD:-}"
mkdir -p "$APP_ROOT" "$STATE_DIR" "$SERVE_ROOT" "$PERSISTENT_VIDEO_DIR"
if ! command -v git >/dev/null 2>&1; then
echo "git is required" >&2
exit 1
fi
if ! command -v npm >/dev/null 2>&1; then
echo "npm is required" >&2
exit 1
fi
if ! command -v curl >/dev/null 2>&1; then
echo "curl is required" >&2
exit 1
fi
if [ ! -d "$REPO_DIR/.git" ]; then
git clone "$REPO_URL" "$REPO_DIR"
fi
git -C "$REPO_DIR" fetch --all --prune
REMOTE_REVISION="$(git -C "$REPO_DIR" rev-parse "origin/$BRANCH")"
CURRENT_REVISION=""
if [ -f "$REVISION_FILE" ]; then
CURRENT_REVISION="$(tr -d '\r\n' < "$REVISION_FILE")"
fi
if [ -n "$CURRENT_REVISION" ] && [ "$CURRENT_REVISION" = "$REMOTE_REVISION" ]; then
date -u +"%Y-%m-%dT%H:%M:%SZ" > "$STATE_DIR/last_check_utc.txt"
echo "No new revision on origin/$BRANCH. Current revision: $CURRENT_REVISION"
exit 0
fi
git -C "$REPO_DIR" checkout "$BRANCH"
git -C "$REPO_DIR" reset --hard "origin/$BRANCH"
pushd "$APP_DIR" >/dev/null
if [ -f package-lock.json ]; then
npm ci
else
npm install
fi
npm run build
popd >/dev/null
rm -rf "$CURRENT_DIR"
mkdir -p "$CURRENT_DIR"
cp -a "$BUILD_DIR"/. "$CURRENT_DIR"/
if [ -d "$PERSISTENT_VIDEO_DIR" ] && [ "$(find "$PERSISTENT_VIDEO_DIR" -maxdepth 1 -type f | wc -l)" -gt 0 ]; then
mkdir -p "$CURRENT_DIR/videos"
cp -a "$PERSISTENT_VIDEO_DIR"/. "$CURRENT_DIR/videos"/
fi
if [ "$RUN_DB_MIGRATIONS" = "1" ] && [ -n "$DB_MIGRATION_CMD" ]; then
echo "Running DB migration command..."
bash -lc "$DB_MIGRATION_CMD"
fi
if [ "$RUN_BACKEND_RESTART" = "1" ]; then
echo "Restarting backend service: $BACKEND_SERVICE"
systemctl restart "$BACKEND_SERVICE"
fi
echo "Waiting for backend health: $BACKEND_HEALTH_URL"
deadline=$(( $(date +%s) + BACKEND_HEALTH_TIMEOUT_S ))
until curl -fsS "$BACKEND_HEALTH_URL" >/dev/null 2>&1; do
if [ "$(date +%s)" -ge "$deadline" ]; then
echo "Backend health check failed for $BACKEND_HEALTH_URL" >&2
if command -v journalctl >/dev/null 2>&1; then
journalctl -u "$BACKEND_SERVICE" -n 80 --no-pager || true
fi
exit 1
fi
sleep 2
done
printf '%s\n' "$REMOTE_REVISION" > "$REVISION_FILE"
date -u +"%Y-%m-%dT%H:%M:%SZ" > "$STATE_DIR/last_deploy_utc.txt"
echo "Deployed revision $(cat "$REVISION_FILE") to $CURRENT_DIR"