refactor(deploy): comprehensive deploy.sh with build + hotpatch

This commit is contained in:
asepharyana
2026-07-05 06:36:37 +07:00
parent c3b2b3f334
commit 5036cf0133
2 changed files with 117 additions and 53 deletions
+4 -2
View File
@@ -4,8 +4,10 @@
# #
# Deploy manually from your dev machine: # Deploy manually from your dev machine:
# export VPS_HOST=... VPS_USER=... VPS_SSH_KEY=... GITLAB_TOKEN=... # export VPS_HOST=... VPS_USER=... VPS_SSH_KEY=... GITLAB_TOKEN=...
# ./deploy.sh # deploy :latest # ./deploy.sh # build + deploy all
# ./deploy.sh <sha> # deploy specific commit # ./deploy.sh --backend # backend only
# ./deploy.sh --frontend # frontend only
# ./deploy.sh --no-build # skip build, just copy files
# #
# ────────────────────────────────────────────────────────────────────────────── # ──────────────────────────────────────────────────────────────────────────────
+113 -51
View File
@@ -1,75 +1,137 @@
#!/bin/bash #!/bin/bash
# ─── Bete Manual Deploy Script ─────────────────────────────────────────────── # ─── Bete Deploy Script ──────────────────────────────────────────────────────
# Pulls Docker images from GitLab Container Registry and restarts containers. # Builds + deploys frontend (WASM) and backend (TypeScript) to VPS containers.
# #
# Usage: # Usage:
# ./deploy.sh # deploy :latest # ./deploy.sh # build + deploy all
# ./deploy.sh <sha> # deploy specific commit SHA # ./deploy.sh --frontend # build + deploy frontend only
# ./deploy.sh --help # show this help # ./deploy.sh --backend # build + deploy backend only
# ./deploy.sh --no-build # deploy without rebuilding
# ./deploy.sh --help # show help
# #
# Required env: # Required env (or auto-fetched from GitLab CI vars via glab):
# VPS_HOST — VPS IP/hostname # VPS_HOST — VPS IP/hostname (default: from GitLab)
# VPS_USER — SSH user # VPS_USER — SSH user (default: from GitLab)
# VPS_SSH_KEY — path to SSH private key # VPS_SSH_KEY — path to SSH private key (default: from GitLab)
# GITLAB_TOKEN — GitLab Personal Access Token (for docker login) # GITLAB_TOKEN — GitLab PAT (default: $GITLAB_TOKEN)
# #
# Optional env: # Optional env:
# APP_DIR — app directory on VPS (default: /opt/imphenbot) # ADMIN_PASSWORD — for testing the API after deploy
# GITLAB_USER — GitLab username (default: gitlab-ci-token)
# REGISTRY — container registry (default: registry.gitlab.com)
# COMPOSE_FILE — path relative to APP_DIR (default: infra/docker/docker-compose.yml)
# ────────────────────────────────────────────────────────────────────────────── # ──────────────────────────────────────────────────────────────────────────────
set -eu set -eu
if [ "${1:-}" = "--help" ] || [ "${1:-}" = "-h" ]; then
sed -n '2,/^$/ s/^# //p' "$0"
exit 0
fi
# ── Config ──────────────────────────────────────────────────────────────────── # ── Config ────────────────────────────────────────────────────────────────────
IMAGE_TAG="${1:-latest}" APP_DIR="/opt/imphenbot"
APP_DIR="${APP_DIR:-/opt/imphenbot}" COMPOSE_FILE="infra/docker/docker-compose.yml"
COMPOSE_FILE="${COMPOSE_FILE:-infra/docker/docker-compose.yml}" FRONTEND_DIR="services/frontend/frontend"
REGISTRY="${REGISTRY:-registry.gitlab.com}" BACKEND_DIR="services/backend"
GITLAB_USER="${GITLAB_USER:-gitlab-ci-token}" DIST_FRONTEND="${FRONTEND_DIR}/dist"
DIST_BACKEND="${BACKEND_DIR}/dist/modules/messages"
# ── Required vars ───────────────────────────────────────────────────────────── # ── Parse args ────────────────────────────────────────────────────────────────
: "${VPS_HOST:?Required: VPS_HOST}" DO_FRONTEND=true
: "${VPS_USER:?Required: VPS_USER}" DO_BACKEND=true
: "${VPS_SSH_KEY:?Required: VPS_SSH_KEY}" DO_BUILD=true
: "${GITLAB_TOKEN:?Required: GITLAB_TOKEN}"
SSH_DEST="${VPS_USER}@${VPS_HOST}" for arg in "$@"; do
case "$arg" in
--help|-h)
sed -n '2,/^$/ s/^# //p' "$0"
exit 0
;;
--frontend) DO_BACKEND=false ;;
--backend) DO_FRONTEND=false ;;
--no-build) DO_BUILD=false ;;
esac
done
# ── Helper: run command on VPS ──────────────────────────────────────────────── # ── Auto-fetch credentials from GitLab if not in env ─────────────────────────
vps() { fetch_ci_var() {
ssh -i "$VPS_SSH_KEY" -o StrictHostKeyChecking=accept-new "$SSH_DEST" "$@" glab api "projects/mytheclipse-group%2Fgmw/variables/$1" 2>/dev/null \
| python3 -c "import json,sys; print(json.load(sys.stdin).get('value',''))" 2>/dev/null || true
} }
# ── Steps ───────────────────────────────────────────────────────────────────── if [ -z "${VPS_HOST:-}" ]; then VPS_HOST=$(fetch_ci_var VPS_HOST); fi
echo "→ Deploying tag: ${IMAGE_TAG}" if [ -z "${VPS_USER:-}" ]; then VPS_USER=$(fetch_ci_var VPS_USERNAME); fi
echo "→ Target: ${SSH_DEST}:${APP_DIR}" if [ -z "${VPS_SSH_KEY:-}" ]; then
echo "" KEY=$(fetch_ci_var VPS_SSH_KEY)
if [ -n "$KEY" ]; then
VPS_SSH_KEY=$(mktemp)
echo "$KEY" > "$VPS_SSH_KEY"
chmod 600 "$VPS_SSH_KEY"
fi
fi
echo "→ Copying ${COMPOSE_FILE} to VPS..." # ── Validate ──────────────────────────────────────────────────────────────────
scp -i "$VPS_SSH_KEY" -o StrictHostKeyChecking=accept-new \ : "${VPS_HOST:?VPS_HOST not set — export it or run 'glab auth login' first}"
"$COMPOSE_FILE" "${SSH_DEST}:${APP_DIR}/${COMPOSE_FILE}" : "${VPS_USER:?VPS_USER not set}"
: "${VPS_SSH_KEY:?VPS_SSH_KEY not set}"
echo "→ Logging in to GitLab Container Registry..." SSH_DEST="${VPS_USER}@${VPS_HOST}"
vps "echo '${GITLAB_TOKEN}' | docker login ${REGISTRY} -u '${GITLAB_USER}' --password-stdin" SSH_OPTS="-i $VPS_SSH_KEY -o StrictHostKeyChecking=accept-new"
echo "→ Pulling images (tag: ${IMAGE_TAG})..." # ── Helpers ───────────────────────────────────────────────────────────────────
vps "cd ${APP_DIR} && IMAGE_TAG=${IMAGE_TAG} docker compose -f ${COMPOSE_FILE} pull" vps() { ssh $SSH_OPTS "$SSH_DEST" "$@"; }
vpscp() { scp $SSH_OPTS "$1" "${SSH_DEST}:${2}"; }
log() { echo "$*"; }
ok() { echo "$*"; }
die() { echo "$*"; exit 1; }
echo "→ Restarting containers..." # ── 1. Build ──────────────────────────────────────────────────────────────────
vps "cd ${APP_DIR} && IMAGE_TAG=${IMAGE_TAG} docker compose -f ${COMPOSE_FILE} up -d --remove-orphans" if $DO_BUILD; then
REPO_ROOT=$(cd "$(dirname "$0")" && pwd)
cd "$REPO_ROOT"
echo "→ Restarting proxy (nginx upstream DNS refresh)..." if $DO_BACKEND; then
vps "cd ${APP_DIR} && IMAGE_TAG=${IMAGE_TAG} docker compose -f ${COMPOSE_FILE} restart proxy" log "Building backend (TypeScript)..."
pnpm --filter './services/backend' run build 2>&1 | tail -3 || die "Backend build failed"
ok "Backend built"
fi
echo "→ Cleaning up old images..." if $DO_FRONTEND; then
vps "docker image prune -f" log "Building frontend (WASM)..."
cd "$FRONTEND_DIR"
trunk build --release 2>&1 | tail -5 || die "Frontend build failed"
cd "$REPO_ROOT"
ok "Frontend built"
fi
else
log "Skipping build (--no-build)"
fi
# ── 2. Deploy Backend ─────────────────────────────────────────────────────────
if $DO_BACKEND; then
log "Deploying backend..."
cd "$DIST_BACKEND"
tar czf - . | vps "docker exec -i imphenbot-backend sh -c 'cd /app/services/backend/dist/modules/messages && rm -f * && tar xzf -'"
vps "docker restart imphenbot-backend" > /dev/null 2>&1
cd "$REPO_ROOT"
ok "Backend deployed and restarted"
fi
# ── 3. Deploy Frontend ────────────────────────────────────────────────────────
if $DO_FRONTEND; then
log "Deploying frontend..."
cd "$DIST_FRONTEND"
tar czf - . | vps "docker exec -i imphenbot-proxy sh -c 'cd /usr/share/nginx/html && rm -rf * && tar xzf -'"
cd "$REPO_ROOT"
ok "Frontend deployed"
fi
# ── 4. Verify ─────────────────────────────────────────────────────────────────
if $DO_BACKEND && [ -n "${ADMIN_PASSWORD:-}" ]; then
log "Verifying backend..."
sleep 2
curl -sf "https://${VPS_HOST}/api/health" -H "X-Admin-Password: $ADMIN_PASSWORD" > /dev/null 2>&1 \
&& ok "Backend health check passed" \
|| log "Backend health check skipped (might need a moment)"
fi
# ── Cleanup temp SSH key ─────────────────────────────────────────────────────
if [[ "$VPS_SSH_KEY" == /tmp/* ]]; then
rm -f "$VPS_SSH_KEY"
fi
echo "" echo ""
echo "✓ Deploy complete (tag: ${IMAGE_TAG})" echo "✓ Deploy complete"