Repository navigation
Expand file tree
/
Copy pathscript.sh
More file actions
executable file
·93 lines (80 loc) · 2.08 KB
/
Copy pathscript.sh
File metadata and controls
executable file
·93 lines (80 loc) · 2.08 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
#!/bin/bash
# Deploy the personal site (Next.js in web/).
# Run on the server after `git pull` (same idea as ~/project/wele/script.sh).
#
# Usage:
# ./script.sh # npm install (if needed), build, pm2 restart
# ./script.sh --restart # skip build, just restart pm2
# ./script.sh --no-install # skip npm ci/install
#
# Process: caobo171.web on port 41717 (see web/ecosystem.config.js)
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
WEB_DIR="$SCRIPT_DIR/web"
APP_NAME="caobo171.web"
PORT="${PORT:-41717}"
HEALTH_URL="http://127.0.0.1:${PORT}/api/health"
BUILD=true
RUN_INSTALL=true
for arg in "$@"; do
case "$arg" in
--restart) BUILD=false ;;
--no-install) RUN_INSTALL=false ;;
-h|--help)
sed -n '2,12p' "$0"
exit 0
;;
*)
echo "Unknown argument: $arg (options: --restart, --no-install)"
exit 1
;;
esac
done
if [ ! -d "$WEB_DIR" ]; then
echo "ERROR: web/ not found at $WEB_DIR"
exit 1
fi
cd "$WEB_DIR"
echo "========== DEPLOYING caobo171.web =========="
echo "(build=$BUILD install=$RUN_INSTALL port=$PORT)"
echo ""
if [ "$RUN_INSTALL" = true ]; then
echo "===== [1/3] Dependencies ====="
if [ -f package-lock.json ]; then
npm ci
else
npm install
fi
echo ""
else
echo "===== [1/3] Dependencies (skipped) ====="
echo ""
fi
if [ "$BUILD" = true ]; then
echo "===== [2/3] Build ====="
nice -n 10 env NODE_OPTIONS="--max-old-space-size=2048" npm run build
echo ""
else
echo "===== [2/3] Build (skipped — --restart) ====="
echo ""
fi
echo "===== [3/3] PM2 restart ====="
if ! command -v pm2 >/dev/null 2>&1; then
echo "ERROR: pm2 not found. Install with: npm i -g pm2"
exit 1
fi
# Prefer startOrRestart (restart --env is unreliable on some PM2 versions)
pm2 startOrRestart ecosystem.config.js --only "$APP_NAME" --env production
pm2 save 2>/dev/null || true
echo ""
echo "===== Health check ====="
echo -n "$HEALTH_URL ... "
if curl -fsS --max-time 8 "$HEALTH_URL"; then
echo
else
echo "FAILED — check: pm2 logs $APP_NAME"
exit 1
fi
echo ""
echo "========== DEPLOY COMPLETE =========="
pm2 list