#!/usr/bin/env bash # Deploy the latest image and sync config files to the server. # # Usage: # ./deploy.sh [user@host] # # The host defaults to DEPLOY_HOST env var, then falls back to reading the # Terraform output from infra/. Pass an explicit host to override both. # # Examples: # ./deploy.sh # ./deploy.sh root@1.2.3.4 # DEPLOY_HOST=root@at-tracker.opensci.dev ./deploy.sh set -euo pipefail # ── Resolve target host ──────────────────────────────────────────────────────── if [[ $# -ge 1 ]]; then SERVER="$1" elif [[ -n "${DEPLOY_HOST:-}" ]]; then SERVER="$DEPLOY_HOST" elif command -v terraform &>/dev/null && [[ -f infra/terraform.tfvars ]]; then IP=$(cd infra && terraform output -raw server_ip 2>/dev/null || true) if [[ -n "$IP" ]]; then SERVER="root@$IP" fi fi if [[ -z "${SERVER:-}" ]]; then echo "Error: cannot determine server host." >&2 echo "Set DEPLOY_HOST, pass it as an argument, or run from a directory with a Terraform state." >&2 exit 1 fi echo "Deploying to $SERVER" # ── Sync config files ────────────────────────────────────────────────────────── # Always copy — idempotent, ensures server matches the repo. echo "→ Syncing docker-compose.yml and Caddyfile..." scp docker-compose.yml Caddyfile "$SERVER:/opt/at-tracker/" # ── Pull new image and restart app ──────────────────────────────────────────── echo "→ Pulling latest image..." ssh "$SERVER" "cd /opt/at-tracker && docker compose pull app" echo "→ Restarting app container..." ssh "$SERVER" "cd /opt/at-tracker && docker compose up -d --no-deps app" echo "Done."