Files
bssapp/deploy/deploy.sh
2026-02-17 13:13:25 +03:00

83 lines
2.1 KiB
Bash

#!/bin/bash
set -euo pipefail
export NODE_OPTIONS="--max_old_space_size=4096"
export CI="true"
export npm_config_progress="false"
export npm_config_loglevel="warn"
export FORCE_COLOR="0"
LOG_FILE="/var/log/bssapp_deploy.log"
APP_DIR="/opt/bssapp"
LOCK_FILE="/tmp/bssapp_deploy.lock"
build_api_binary() {
if ! command -v go >/dev/null 2>&1; then
echo "go command not found; cannot build backend binary."
return 1
fi
cd "$APP_DIR/svc"
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-w -s" -o "$APP_DIR/svc/bssapp" ./main.go
chmod +x "$APP_DIR/svc/bssapp"
}
run_deploy() {
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
echo "[$(date '+%F %T')] Deploy already running. Skipping new request."
return 0
fi
echo "=============================="
echo "[DEPLOY START] $(date '+%F %T')"
echo "=============================="
cd "$APP_DIR"
echo "== GIT SYNC =="
git fetch origin
git reset --hard origin/master
git clean -fdx -e .env -e mail.env -e svc/.env -e svc/mail.env -e svc/bssapp
echo "== BUILD UI =="
cd "$APP_DIR/ui"
npm ci --no-audit --no-fund --include=optional
# npm scriptlerinde PATH onceligi nedeniyle node_modules/.bin/sass kullanilir.
# Bu dosya pure JS olursa --embedded hata verir; global Dart Sass binary'sine bagliyoruz.
SASS_BIN="$(command -v sass || true)"
if [[ -z "$SASS_BIN" ]]; then
echo "sass command not found in PATH."
return 1
fi
"$SASS_BIN" --embedded --version >/dev/null 2>&1 || {
echo "Global sass supports --embedded is not available."
return 1
}
ln -sf "$SASS_BIN" "$APP_DIR/ui/node_modules/.bin/sass"
"$APP_DIR/ui/node_modules/.bin/sass" --embedded --version >/dev/null 2>&1 || {
echo "node_modules/.bin/sass still does not support --embedded."
return 1
}
npm run build
echo "== BUILD API =="
build_api_binary
echo "== RESTART SERVICE =="
systemctl restart bssapp
echo "[DEPLOY FINISHED] $(date '+%F %T')"
}
if [[ "${1:-}" == "--run" ]]; then
run_deploy >>"$LOG_FILE" 2>&1
exit 0
fi
# Fully detach webhook-triggered process to avoid EPIPE from closed request sockets.
nohup /bin/bash "$0" --run </dev/null >/dev/null 2>&1 &
exit 0