codex changes

This commit is contained in:
Jake Kasper
2025-09-02 07:02:34 -05:00
parent e75be978fa
commit 4853eff749
6 changed files with 171 additions and 42 deletions

89
scripts/deploy.sh Normal file
View File

@@ -0,0 +1,89 @@
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'USAGE'
Usage: scripts/deploy.sh -r <user@host> -d </remote/repo/path> [-b <branch>] [-m]
Options:
-r Remote SSH host in the form user@host (or host if configured in SSH config)
-d Absolute path to the repo on the remote host
-b Git branch to deploy (default: main)
-m Run DB migrations via Flyway after building images
Environment overrides:
TT_REMOTE_HOST, TT_REMOTE_PATH, TT_BRANCH, TT_RUN_MIGRATIONS (0/1)
Examples:
scripts/deploy.sh -r ubuntu@myserver -d /opt/apps/turftracker -b main -m
TT_REMOTE_HOST=ubuntu@myserver TT_REMOTE_PATH=/opt/apps/turftracker scripts/deploy.sh
USAGE
}
REMOTE_HOST="${TT_REMOTE_HOST:-}"
REMOTE_PATH="${TT_REMOTE_PATH:-}"
BRANCH="${TT_BRANCH:-main}"
RUN_MIGRATIONS="${TT_RUN_MIGRATIONS:-0}"
while getopts ":r:d:b:mh" opt; do
case $opt in
r) REMOTE_HOST="$OPTARG" ;;
d) REMOTE_PATH="$OPTARG" ;;
b) BRANCH="$OPTARG" ;;
m) RUN_MIGRATIONS="1" ;;
h) usage; exit 0 ;;
\?) echo "Invalid option: -$OPTARG" >&2; usage; exit 2 ;;
:) echo "Option -$OPTARG requires an argument." >&2; usage; exit 2 ;;
esac
done
if [[ -z "$REMOTE_HOST" || -z "$REMOTE_PATH" ]]; then
echo "Error: remote host and remote path are required" >&2
usage
exit 2
fi
echo "Deploying to $REMOTE_HOST:$REMOTE_PATH (branch: $BRANCH, migrations: $RUN_MIGRATIONS)"
# Compose wrapper on remote: prefer v2 (`docker compose`), fallback to v1 (`docker-compose`)
read -r -d '' REMOTE_SCRIPT <<'EOS'
set -euo pipefail
COMPOSE_CMD="docker compose"
if ! $COMPOSE_CMD version >/dev/null 2>&1; then
if command -v docker-compose >/dev/null 2>&1; then
COMPOSE_CMD="docker-compose"
else
echo "docker compose or docker-compose not found on remote host" >&2
exit 1
fi
fi
cd "$REPO_PATH"
echo "[remote] Using compose: $COMPOSE_CMD"
echo "[remote] Updating repo to branch $BRANCH"
git fetch --all --prune
git checkout "$BRANCH"
git pull --ff-only origin "$BRANCH"
echo "[remote] Building services: backend, frontend"
$COMPOSE_CMD build backend frontend
if [[ "${RUN_MIGRATIONS}" == "1" ]]; then
echo "[remote] Running Flyway migrations"
# Ensure DB is reachable (optional: add a wait loop if needed)
$COMPOSE_CMD run --rm flyway -locations=filesystem:/migrations migrate
fi
echo "[remote] Restarting services"
$COMPOSE_CMD up -d backend frontend
echo "[remote] Deployment complete"
EOS
ssh -o BatchMode=yes "$REMOTE_HOST" \
REPO_PATH="$REMOTE_PATH" BRANCH="$BRANCH" RUN_MIGRATIONS="$RUN_MIGRATIONS" \
"bash -s" <<< "$REMOTE_SCRIPT"
echo "Done."