-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·142 lines (122 loc) · 5.49 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·142 lines (122 loc) · 5.49 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env bash
# ============================================================================
# Déploiement de KerbalDesk sur kspboard.forgenet.fr
#
# build npm → pm2 (serveur statique local) → nginx (reverse proxy)
# → certbot (HTTPS Let's Encrypt, obligatoire pour installer la PWA)
#
# Usage (sur le serveur, depuis la racine du dépôt) :
# sudo ./deploy.sh déploiement complet
# sudo SKIP_CERTBOT=1 ./deploy.sh sans certificat (DNS pas encore prêt)
# ./deploy.sh app rebuild + reload pm2 uniquement (sans root)
#
# Variables surchargables : DOMAIN, PORT, CERTBOT_EMAIL, APP_NAME
# ============================================================================
set -euo pipefail
DOMAIN="${DOMAIN:-kspboard.forgenet.fr}"
PORT="${PORT:-4180}"
CERTBOT_EMAIL="${CERTBOT_EMAIL:-nightfury@nationquest.fr}"
APP_NAME="${APP_NAME:-kspboard}"
APP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MODE="${1:-full}"
log() { printf '\033[1;34m[deploy]\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33m[deploy]\033[0m %s\n' "$*"; }
die() { printf '\033[1;31m[deploy]\033[0m %s\n' "$*" >&2; exit 1; }
# ----------------------------------------------------------------------------
# 1. Prérequis et build
# ----------------------------------------------------------------------------
command -v node >/dev/null || die "Node.js est requis (>= 20). Installe-le puis relance."
NODE_MAJOR="$(node -p 'process.versions.node.split(".")[0]')"
[ "$NODE_MAJOR" -ge 20 ] || die "Node.js >= 20 requis (trouvé : $(node -v))."
cd "$APP_DIR"
log "Installation des dépendances…"
npm ci --no-audit --no-fund 2>/dev/null || npm install --no-audit --no-fund
log "Build de production…"
npm run build
# ----------------------------------------------------------------------------
# 2. pm2 : serveur statique local (127.0.0.1:$PORT)
# ----------------------------------------------------------------------------
if ! command -v pm2 >/dev/null; then
log "Installation de pm2…"
npm install -g pm2
fi
log "Démarrage/rechargement de l'application pm2 « $APP_NAME »…"
PORT="$PORT" pm2 startOrReload deploy/ecosystem.config.cjs --update-env
pm2 save
if [ "$(id -u)" -eq 0 ] && command -v systemctl >/dev/null; then
# Démarrage automatique de pm2 au boot (idempotent).
pm2 startup systemd -u "${SUDO_USER:-root}" --hp "$(eval echo "~${SUDO_USER:-root}")" >/dev/null || true
fi
if [ "$MODE" = "app" ]; then
log "Mode « app » : nginx/certbot non touchés. Application servie sur 127.0.0.1:$PORT."
exit 0
fi
# ----------------------------------------------------------------------------
# 3. nginx : reverse proxy $DOMAIN → 127.0.0.1:$PORT
# ----------------------------------------------------------------------------
[ "$(id -u)" -eq 0 ] || die "La configuration nginx/certbot demande root : relance avec sudo (ou « ./deploy.sh app » pour l'app seule)."
if ! command -v nginx >/dev/null; then
if command -v apt-get >/dev/null; then
log "Installation de nginx…"
apt-get update -qq && apt-get install -y -qq nginx
else
die "nginx introuvable et pas de gestionnaire apt : installe-le manuellement puis relance."
fi
fi
if [ -d /etc/nginx/sites-available ]; then
NGINX_CONF="/etc/nginx/sites-available/$DOMAIN"
NGINX_LINK="/etc/nginx/sites-enabled/$DOMAIN"
else
NGINX_CONF="/etc/nginx/conf.d/$DOMAIN.conf"
NGINX_LINK=""
fi
log "Écriture de la configuration nginx ($NGINX_CONF)…"
cat > "$NGINX_CONF" <<EOF
server {
listen 80;
listen [::]:80;
server_name $DOMAIN;
# Compression des assets texte (le JS de l'app principalement).
gzip on;
gzip_types text/css text/javascript application/javascript application/json
application/manifest+json image/svg+xml;
gzip_min_length 1024;
location / {
proxy_pass http://127.0.0.1:$PORT;
proxy_http_version 1.1;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
EOF
if [ -n "$NGINX_LINK" ] && [ ! -e "$NGINX_LINK" ]; then
ln -s "$NGINX_CONF" "$NGINX_LINK"
fi
nginx -t
systemctl reload nginx 2>/dev/null || service nginx reload
log "nginx recharge : http://$DOMAIN → 127.0.0.1:$PORT"
# ----------------------------------------------------------------------------
# 4. certbot : HTTPS Let's Encrypt (+ redirection 80 → 443)
# ----------------------------------------------------------------------------
if [ "${SKIP_CERTBOT:-0}" = "1" ]; then
warn "SKIP_CERTBOT=1 : pas de certificat. La PWA ne sera installable qu'en HTTPS."
exit 0
fi
if ! command -v certbot >/dev/null; then
if command -v apt-get >/dev/null; then
log "Installation de certbot…"
apt-get install -y -qq certbot python3-certbot-nginx
else
die "certbot introuvable : installe certbot + python3-certbot-nginx puis relance."
fi
fi
log "Obtention/renouvellement du certificat pour $DOMAIN…"
certbot --nginx -d "$DOMAIN" --redirect --agree-tos -m "$CERTBOT_EMAIL" -n
log "──────────────────────────────────────────────"
log "Déploiement terminé : https://$DOMAIN"
log " pm2 : pm2 status $APP_NAME | pm2 logs $APP_NAME"
log " nginx : $NGINX_CONF"
log " màj app : git pull && sudo ./deploy.sh (ou ./deploy.sh app)"
log "──────────────────────────────────────────────"