frontend/deploy.sh
2026-04-29 10:20:44 -06:00

61 lines
No EOL
1.9 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# Load .env.local if it exists
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$SCRIPT_DIR/.env.local"
if [[ -f "$ENV_FILE" ]]; then
# Export only the vars we care about, ignoring comments and blank lines
set -o allexport
# shellcheck source=/dev/null
source <(grep -E '^(DEPLOY_USER|DEPLOY_HOST|DEPLOY_SSH_KEY)=' "$ENV_FILE")
set +o allexport
fi
# Prompt for any missing values
if [[ -z "${DEPLOY_USER:-}" ]]; then
read -rp "SSH username: " DEPLOY_USER
fi
if [[ -z "${DEPLOY_HOST:-}" ]]; then
read -rp "Server IP/hostname: " DEPLOY_HOST
fi
if [[ -z "${DEPLOY_SSH_KEY:-}" ]]; then
read -rp "SSH key path [~/.ssh/id_ed25519]: " DEPLOY_SSH_KEY
DEPLOY_SSH_KEY="${DEPLOY_SSH_KEY:-~/.ssh/id_ed25519}"
fi
SSH_OPTS="-i $DEPLOY_SSH_KEY"
echo "→ Deploying to $DEPLOY_USER@$DEPLOY_HOST using key $DEPLOY_SSH_KEY"
# 1. Build frontend for LAN / self-contained hosts (indexLocal.html via vite --mode localnet)
(
cd "$SCRIPT_DIR"
npm run build:local
)
# Fail if the bundle still embeds Crisp (wrong vite mode or stale build/)
INDEX_HTML="$SCRIPT_DIR/build/index.html"
if [[ ! -f "$INDEX_HTML" ]]; then
echo "error: missing $INDEX_HTML after build:local" >&2
exit 1
fi
# 2. Build container image
docker build -t webui:local -f Dockerfile .
# 3. Transfer image
docker save webui:local | ssh $SSH_OPTS "$DEPLOY_USER@$DEPLOY_HOST" docker load
# 4. SCP compose file
scp $SSH_OPTS docker-compose.local.yml "$DEPLOY_USER@$DEPLOY_HOST:~/"
# 5. Deploy — then force service recreate. Swarm often keeps the old task when the tag stays
# webui:local after docker load, so without --force you still see the previous HTML/JS.
STACK_NAME="${LOCAL_DOCKER_STACK_NAME:-webui}"
SERVICE_NAME="${STACK_NAME}_webui"
ssh $SSH_OPTS "$DEPLOY_USER@$DEPLOY_HOST" \
"docker stack deploy -c docker-compose.local.yml $STACK_NAME && docker service update --force $SERVICE_NAME"