From 5036cf0133d9c4d7a6b50f43317693631a3241cc Mon Sep 17 00:00:00 2001 From: asepharyana Date: Sun, 5 Jul 2026 06:36:37 +0700 Subject: [PATCH] refactor(deploy): comprehensive deploy.sh with build + hotpatch --- .gitlab-ci.yml | 6 +- deploy.sh | 164 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 117 insertions(+), 53 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e1bf2fa..255027c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,8 +4,10 @@ # # Deploy manually from your dev machine: # export VPS_HOST=... VPS_USER=... VPS_SSH_KEY=... GITLAB_TOKEN=... -# ./deploy.sh # deploy :latest -# ./deploy.sh # deploy specific commit +# ./deploy.sh # build + deploy all +# ./deploy.sh --backend # backend only +# ./deploy.sh --frontend # frontend only +# ./deploy.sh --no-build # skip build, just copy files # # ────────────────────────────────────────────────────────────────────────────── diff --git a/deploy.sh b/deploy.sh index 50e8ca1..d3780b6 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,75 +1,137 @@ #!/bin/bash -# ─── Bete Manual Deploy Script ─────────────────────────────────────────────── -# Pulls Docker images from GitLab Container Registry and restarts containers. +# ─── Bete Deploy Script ────────────────────────────────────────────────────── +# Builds + deploys frontend (WASM) and backend (TypeScript) to VPS containers. # # Usage: -# ./deploy.sh # deploy :latest -# ./deploy.sh # deploy specific commit SHA -# ./deploy.sh --help # show this help +# ./deploy.sh # build + deploy all +# ./deploy.sh --frontend # build + deploy frontend only +# ./deploy.sh --backend # build + deploy backend only +# ./deploy.sh --no-build # deploy without rebuilding +# ./deploy.sh --help # show help # -# Required env: -# VPS_HOST — VPS IP/hostname -# VPS_USER — SSH user -# VPS_SSH_KEY — path to SSH private key -# GITLAB_TOKEN — GitLab Personal Access Token (for docker login) +# Required env (or auto-fetched from GitLab CI vars via glab): +# VPS_HOST — VPS IP/hostname (default: from GitLab) +# VPS_USER — SSH user (default: from GitLab) +# VPS_SSH_KEY — path to SSH private key (default: from GitLab) +# GITLAB_TOKEN — GitLab PAT (default: $GITLAB_TOKEN) # # Optional env: -# APP_DIR — app directory on VPS (default: /opt/imphenbot) -# 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) +# ADMIN_PASSWORD — for testing the API after deploy # ────────────────────────────────────────────────────────────────────────────── set -eu -if [ "${1:-}" = "--help" ] || [ "${1:-}" = "-h" ]; then - sed -n '2,/^$/ s/^# //p' "$0" - exit 0 -fi - # ── Config ──────────────────────────────────────────────────────────────────── -IMAGE_TAG="${1:-latest}" -APP_DIR="${APP_DIR:-/opt/imphenbot}" -COMPOSE_FILE="${COMPOSE_FILE:-infra/docker/docker-compose.yml}" -REGISTRY="${REGISTRY:-registry.gitlab.com}" -GITLAB_USER="${GITLAB_USER:-gitlab-ci-token}" +APP_DIR="/opt/imphenbot" +COMPOSE_FILE="infra/docker/docker-compose.yml" +FRONTEND_DIR="services/frontend/frontend" +BACKEND_DIR="services/backend" +DIST_FRONTEND="${FRONTEND_DIR}/dist" +DIST_BACKEND="${BACKEND_DIR}/dist/modules/messages" -# ── Required vars ───────────────────────────────────────────────────────────── -: "${VPS_HOST:?Required: VPS_HOST}" -: "${VPS_USER:?Required: VPS_USER}" -: "${VPS_SSH_KEY:?Required: VPS_SSH_KEY}" -: "${GITLAB_TOKEN:?Required: GITLAB_TOKEN}" +# ── Parse args ──────────────────────────────────────────────────────────────── +DO_FRONTEND=true +DO_BACKEND=true +DO_BUILD=true -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 ──────────────────────────────────────────────── -vps() { - ssh -i "$VPS_SSH_KEY" -o StrictHostKeyChecking=accept-new "$SSH_DEST" "$@" +# ── Auto-fetch credentials from GitLab if not in env ───────────────────────── +fetch_ci_var() { + 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 ───────────────────────────────────────────────────────────────────── -echo "→ Deploying tag: ${IMAGE_TAG}" -echo "→ Target: ${SSH_DEST}:${APP_DIR}" -echo "" +if [ -z "${VPS_HOST:-}" ]; then VPS_HOST=$(fetch_ci_var VPS_HOST); fi +if [ -z "${VPS_USER:-}" ]; then VPS_USER=$(fetch_ci_var VPS_USERNAME); fi +if [ -z "${VPS_SSH_KEY:-}" ]; then + 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..." -scp -i "$VPS_SSH_KEY" -o StrictHostKeyChecking=accept-new \ - "$COMPOSE_FILE" "${SSH_DEST}:${APP_DIR}/${COMPOSE_FILE}" +# ── Validate ────────────────────────────────────────────────────────────────── +: "${VPS_HOST:?VPS_HOST not set — export it or run 'glab auth login' first}" +: "${VPS_USER:?VPS_USER not set}" +: "${VPS_SSH_KEY:?VPS_SSH_KEY not set}" -echo "→ Logging in to GitLab Container Registry..." -vps "echo '${GITLAB_TOKEN}' | docker login ${REGISTRY} -u '${GITLAB_USER}' --password-stdin" +SSH_DEST="${VPS_USER}@${VPS_HOST}" +SSH_OPTS="-i $VPS_SSH_KEY -o StrictHostKeyChecking=accept-new" -echo "→ Pulling images (tag: ${IMAGE_TAG})..." -vps "cd ${APP_DIR} && IMAGE_TAG=${IMAGE_TAG} docker compose -f ${COMPOSE_FILE} pull" +# ── Helpers ─────────────────────────────────────────────────────────────────── +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..." -vps "cd ${APP_DIR} && IMAGE_TAG=${IMAGE_TAG} docker compose -f ${COMPOSE_FILE} up -d --remove-orphans" +# ── 1. Build ────────────────────────────────────────────────────────────────── +if $DO_BUILD; then + REPO_ROOT=$(cd "$(dirname "$0")" && pwd) + cd "$REPO_ROOT" -echo "→ Restarting proxy (nginx upstream DNS refresh)..." -vps "cd ${APP_DIR} && IMAGE_TAG=${IMAGE_TAG} docker compose -f ${COMPOSE_FILE} restart proxy" + if $DO_BACKEND; then + 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..." -vps "docker image prune -f" + if $DO_FRONTEND; then + 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 "✓ Deploy complete (tag: ${IMAGE_TAG})" +echo "✓ Deploy complete"