-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·117 lines (96 loc) · 4.85 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·117 lines (96 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
set -euo pipefail
# VectorMBE Deploy Script
#
# Deploys the frontend to the Docker Compose stack. Caddy serves the UI as
# static files from a read-only bind mount ($DEPLOY_DIR/www -> /var/www/vectormbe)
# and reverse-proxies /api/* to the vectormbe container, so shipping the
# frontend is a file sync -- no container restart or image rebuild is needed.
#
# Rebuilding the backend image is a separate operation; see docker-build-push.sh.
#
# Usage:
# ./deploy.sh # build and deploy
# ./deploy.sh --dry-run # build and report what would change, write nothing
#
# Override the stack location with VECTORMBE_DEPLOY_DIR if it is not the default.
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
UI_DIR="$REPO_ROOT/ui"
DEPLOY_DIR="${VECTORMBE_DEPLOY_DIR:-/home/node/vectormbe-deploy}"
WWW_DIR="$DEPLOY_DIR/www"
HEALTH_URL="${VECTORMBE_HEALTH_URL:-http://127.0.0.1}"
DRY_RUN=0
[ "${1:-}" = "--dry-run" ] && DRY_RUN=1
die() { echo "[deploy] ERROR: $*" >&2; exit 1; }
# --- Preflight -------------------------------------------------------------
# Fail loudly on a wrong path rather than silently shipping nothing. The
# previous version of this script hardcoded a home directory that did not exist
# on the production host, so it could never have run there.
[ -d "$UI_DIR" ] || die "UI source not found at $UI_DIR"
[ -d "$WWW_DIR" ] || die "web root not found at $WWW_DIR (set VECTORMBE_DEPLOY_DIR)"
if ! docker ps --format '{{.Names}}' 2>/dev/null | grep -q '^vectormbe-caddy$'; then
echo "[deploy] WARNING: vectormbe-caddy is not running; files will be staged but not served."
fi
# --- Build -----------------------------------------------------------------
echo "[deploy] Building frontend from $UI_DIR ..."
cd "$UI_DIR"
npm run build
[ -f dist/index.html ] || die "build produced no dist/index.html"
ENTRY="$(grep -o 'assets/index-[A-Za-z0-9_-]*\.js' dist/index.html | head -1)"
[ -n "$ENTRY" ] || die "could not determine entry bundle from dist/index.html"
echo "[deploy] Built entry bundle: $ENTRY"
# --- env.runtime.js guard --------------------------------------------------
# Caddy serves env.runtime.js no-store because a stale or wrong copy points the
# UI at a dead API host and the app appears dead. The build emits its own copy
# (from public/env.runtime.js), which may differ from what production runs, so
# the live file always wins.
LIVE_ENV="$WWW_DIR/env.runtime.js"
PRESERVE_ENV=0
if [ -f "$LIVE_ENV" ]; then
if ! diff -q "$LIVE_ENV" dist/env.runtime.js >/dev/null 2>&1; then
echo "[deploy] NOTE: built env.runtime.js differs from production; preserving the live copy."
PRESERVE_ENV=1
fi
fi
if [ "$DRY_RUN" = "1" ]; then
echo "[deploy] --dry-run: would sync dist/ -> $WWW_DIR (entry $ENTRY)"
[ "$PRESERVE_ENV" = "1" ] && echo "[deploy] --dry-run: would preserve live env.runtime.js"
exit 0
fi
# --- Backup ----------------------------------------------------------------
BACKUP="/tmp/vectormbe-www-backup-$(date +%Y%m%d-%H%M%S).tar.gz"
echo "[deploy] Backing up current web root to $BACKUP ..."
tar czf "$BACKUP" -C "$DEPLOY_DIR" www
[ "$PRESERVE_ENV" = "1" ] && cp "$LIVE_ENV" /tmp/vectormbe-env.runtime.js.keep
# --- Deploy ----------------------------------------------------------------
# Copy rather than rsync --delete: hashed asset filenames cannot collide, and
# leaving old chunks in place keeps clients mid-session from 404ing on a chunk
# that disappeared underneath them. Prune stale assets separately once a build
# is confirmed good.
echo "[deploy] Syncing dist/ -> $WWW_DIR ..."
cp -r dist/* "$WWW_DIR/"
if [ "$PRESERVE_ENV" = "1" ]; then
cp /tmp/vectormbe-env.runtime.js.keep "$LIVE_ENV"
rm -f /tmp/vectormbe-env.runtime.js.keep
echo "[deploy] Restored production env.runtime.js"
fi
# --- Verify ----------------------------------------------------------------
# Verify what is actually SERVED, not merely what was copied.
echo "[deploy] Verifying ..."
fail=0
code="$(curl -s -o /dev/null -w '%{http_code}' "$HEALTH_URL/")"
[ "$code" = "200" ] || { echo "[deploy] index.html -> $code"; fail=1; }
code="$(curl -s -o /dev/null -w '%{http_code}' "$HEALTH_URL/$ENTRY")"
[ "$code" = "200" ] || { echo "[deploy] $ENTRY -> $code"; fail=1; }
served="$(curl -s "$HEALTH_URL/" | grep -o 'assets/index-[A-Za-z0-9_-]*\.js' | head -1)"
[ "$served" = "$ENTRY" ] || { echo "[deploy] served HTML references '$served', expected '$ENTRY'"; fail=1; }
code="$(curl -s -o /dev/null -w '%{http_code}' "$HEALTH_URL/api/openapi.json")"
[ "$code" = "200" ] || echo "[deploy] WARNING: /api/openapi.json -> $code (frontend deployed; check the backend container)"
if [ "$fail" != "0" ]; then
echo "[deploy] FAILED verification. Roll back with:"
echo " tar xzf $BACKUP -C $DEPLOY_DIR"
exit 1
fi
echo "[deploy] Done. Serving $ENTRY"
echo "[deploy] Backup: $BACKUP"
echo "[deploy] Rollback: tar xzf $BACKUP -C $DEPLOY_DIR"