#!/usr/bin/env bash # Prod runs as Podman Quadlet containers; staging is still host systemd units # (Phase 3). The two paths share only the Caddy snippet. set -euo pipefail TARGET="prod" for arg in "$@"; do case "$arg" in --target=prod|--target=staging) TARGET="${arg#--target=}" ;; *) echo "Unknown arg: $arg (use --target=prod or --target=staging)" >&2; exit 1 ;; esac done VPS="${VPS:-ovh-vps}" DEPLOY_LOG="/var/log/lichen-deploy.log" TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") log() { echo "[$TIMESTAMP] ($TARGET) $1"; } if [ "$TARGET" = "staging" ]; then APP_DIR="/opt/lichen-staging" DATA_DIR="/var/lib/lichen-staging" PREV_DIR="/opt/lichen-staging-prev" HEALTH_URL="https://staging.lichen.wiki/" log "==> Building assets" bun run build log "==> Backing up DB and previous deploy on VPS" ssh "$VPS" " sudo cp ${DATA_DIR}/lichen.db ${DATA_DIR}/lichen-pre-deploy.db 2>/dev/null || true sudo rm -rf ${PREV_DIR} sudo cp -a ${APP_DIR} ${PREV_DIR} 2>/dev/null || true echo '${TIMESTAMP} staging deploy started' | sudo tee -a ${DEPLOY_LOG} > /dev/null " log "==> Syncing to VPS" rsync -avz --delete \ --exclude node_modules --exclude '*.db' --exclude '*.db-shm' --exclude '*.db-wal' \ --exclude data/ --exclude .git \ --rsync-path="sudo rsync" \ ./ "${VPS}:${APP_DIR}/" log "==> Installing deps" ssh "$VPS" " sudo chown -R lichen:lichen ${APP_DIR} sudo -u lichen bash -c 'cd ${APP_DIR} && bun install --production' " log "==> Updating config files" ssh "$VPS" " sudo mkdir -p /etc/caddy/conf.d sudo cp ${APP_DIR}/deploy/Caddyfile.snippet /etc/caddy/conf.d/lichen.caddy sudo cp ${APP_DIR}/deploy/lichen-appview-staging.service /etc/systemd/system/ sudo cp ${APP_DIR}/deploy/lichen-firehose-staging.service /etc/systemd/system/ sudo systemctl daemon-reload " log "==> Restarting services" ssh "$VPS" "sudo systemctl reload caddy && sudo systemctl restart lichen-appview-staging lichen-firehose-staging" else APP_DIR="/opt/lichen" HEALTH_URL="https://lichen.wiki/" # The DB lives in the podman volume now, not /var/lib/lichen. log "==> Snapshotting DBs from the volume" ssh "$VPS" " MP=\$(sudo podman volume inspect systemd-lichen-data --format '{{.Mountpoint}}') sudo mkdir -p /var/backups/lichen-predeploy sudo sqlite3 \"file:\$MP/lichen.db?mode=ro\" \".backup /var/backups/lichen-predeploy/lichen-${TIMESTAMP}.db\" sudo sqlite3 \"file:\$MP/registry.db?mode=ro\" \".backup /var/backups/lichen-predeploy/registry-${TIMESTAMP}.db\" echo '${TIMESTAMP} prod deploy started' | sudo tee -a ${DEPLOY_LOG} > /dev/null " # /opt/lichen is only the image build context now — the app runs from the image. # Assets are built inside the Containerfile, so no local `bun run build`. log "==> Syncing build context to VPS" rsync -avz --delete \ --exclude node_modules --exclude '*.db' --exclude '*.db-shm' --exclude '*.db-wal' \ --exclude data/ --exclude .git \ --rsync-path="sudo rsync" \ ./ "${VPS}:${APP_DIR}/" log "==> Installing quadlet units + building image" ssh "$VPS" bash -euo pipefail </dev/null || true sudo podman build -t lichen:latest -f Containerfile . EOF log "==> Installing vhost + reloading caddy" ssh "$VPS" " sudo mkdir -p /etc/caddy/conf.d sudo cp ${APP_DIR}/deploy/Caddyfile.snippet /etc/caddy/conf.d/lichen.caddy sudo systemctl reload caddy " log "==> Restarting services" ssh "$VPS" "sudo systemctl restart lichen-appview lichen-firehose lichen-litestream" fi log "==> Health check (waiting 5s for startup)" sleep 5 STATUS=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$HEALTH_URL") if [ "$STATUS" = "200" ]; then log "==> Health check passed (HTTP $STATUS)" ssh "$VPS" "echo '${TIMESTAMP} ${TARGET} deploy succeeded (HTTP ${STATUS})' | sudo tee -a ${DEPLOY_LOG} > /dev/null" else log "!!! Health check FAILED (HTTP $STATUS) — rolling back" if [ "$TARGET" = "staging" ]; then ssh "$VPS" " sudo rm -rf ${APP_DIR} sudo mv ${PREV_DIR} ${APP_DIR} sudo cp ${DATA_DIR}/lichen-pre-deploy.db ${DATA_DIR}/lichen.db sudo systemctl restart lichen-appview-staging lichen-firehose-staging " else # Image rollback only. The DB is deliberately left alone: Litestream is # replicating it live, and restoring a file under it corrupts the replica # chain. Restore from /var/backups/lichen-predeploy by hand if ever needed. ssh "$VPS" " sudo podman tag localhost/lichen:previous localhost/lichen:latest 2>/dev/null || true sudo systemctl restart lichen-appview lichen-firehose " fi ssh "$VPS" "echo '${TIMESTAMP} ${TARGET} deploy FAILED (HTTP ${STATUS}), rolled back' | sudo tee -a ${DEPLOY_LOG} > /dev/null" log "==> Rolled back. Logs: ssh ${VPS} 'sudo journalctl -u lichen-appview -n 50'" exit 1 fi log "==> Done!"