#!/usr/bin/env bash 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="lichen" 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/" APPVIEW_UNIT="lichen-appview-staging" FIREHOSE_UNIT="lichen-firehose-staging" else APP_DIR="/opt/lichen" DATA_DIR="/var/lib/lichen" PREV_DIR="/opt/lichen-prev" HEALTH_URL="https://lichen.wiki/" APPVIEW_UNIT="lichen-appview" FIREHOSE_UNIT="lichen-firehose" fi DEPLOY_LOG="/var/log/lichen-deploy.log" TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") log() { echo "[$TIMESTAMP] ($TARGET) $1"; } 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} ${TARGET} 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 sudo cp ${APP_DIR}/deploy/Caddyfile /etc/caddy/Caddyfile sudo cp ${APP_DIR}/deploy/caddy.service /etc/systemd/system/ sudo cp ${APP_DIR}/deploy/lichen-appview.service /etc/systemd/system/ sudo cp ${APP_DIR}/deploy/lichen-firehose.service /etc/systemd/system/ 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 cp ${APP_DIR}/deploy/litestream.yml /etc/litestream.yml sudo mkdir -p /etc/systemd/system/litestream.service.d printf '[Service]\nEnvironmentFile=/etc/lichen/env\n' | sudo tee /etc/systemd/system/litestream.service.d/env.conf > /dev/null sudo systemctl daemon-reload " log "==> Restarting services" if [ "$TARGET" = "staging" ]; then ssh "$VPS" "sudo systemctl reload caddy && sudo systemctl restart ${APPVIEW_UNIT} ${FIREHOSE_UNIT}" else ssh "$VPS" "sudo systemctl restart caddy ${APPVIEW_UNIT} ${FIREHOSE_UNIT} litestream" fi log "==> Health check (waiting 3s for startup)" sleep 3 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)" log "!!! Rolling back to previous deploy" 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 ${APPVIEW_UNIT} ${FIREHOSE_UNIT} echo '${TIMESTAMP} ${TARGET} deploy FAILED (HTTP ${STATUS}), rolled back' | sudo tee -a ${DEPLOY_LOG} > /dev/null " log "==> Rolled back. Check logs: ssh ${VPS} 'sudo journalctl -u ${APPVIEW_UNIT} -n 50'" exit 1 fi log "==> Done!"