-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-scorefrost.sh
More file actions
107 lines (83 loc) · 2.97 KB
/
deploy-scorefrost.sh
File metadata and controls
107 lines (83 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bash
set -euo pipefail
APP_DIR="/home/scorefrost/scorefrost-server"
BRANCH="production"
compose() {
if docker compose version >/dev/null 2>&1; then
docker compose "$@"
elif command -v docker-compose >/dev/null 2>&1; then
docker-compose "$@"
else
echo "ERROR: docker compose / docker-compose not found."
exit 1
fi
}
read_env_var() {
local key="$1"
local value
value="$(grep -E "^${key}=" .env | tail -n 1 | cut -d '=' -f 2- || true)"
value="${value%$'\r'}"
# Strip optional surrounding single or double quotes.
value="${value#\"}"
value="${value%\"}"
value="${value#\'}"
value="${value%\'}"
if [[ -z "$value" ]]; then
echo "ERROR: Missing required $key in .env"
exit 1
fi
printf '%s' "$value"
}
echo "==> Ensuring repo exists..."
if [[ ! -d "$APP_DIR/.git" ]]; then
echo "ERROR: $APP_DIR is not a git repository yet. Clone it first."
exit 1
fi
cd "$APP_DIR"
echo "==> Updating code..."
git fetch origin
git checkout "$BRANCH"
git reset --hard "origin/$BRANCH"
if [[ ! -f ".env" ]]; then
echo "ERROR: .env not found in $APP_DIR"
exit 1
fi
PGUSER_VALUE="$(read_env_var PGUSER)"
PGDB_VALUE="$(read_env_var PGDB)"
EXPECTED_MIGRATION_RAW="$({ ls -1 sql/migrations/*.up.sql 2>/dev/null || true; } | xargs -r -n1 basename | sed -E 's/^([0-9]+)_.*$/\1/' | sort -n | tail -1)"
if [[ -z "$EXPECTED_MIGRATION_RAW" ]]; then
echo "ERROR: Could not determine expected migration version from sql/migrations/*.up.sql"
exit 1
fi
EXPECTED_MIGRATION_NUM=$((10#$EXPECTED_MIGRATION_RAW))
echo "==> Expected schema migration version: $EXPECTED_MIGRATION_NUM"
echo "==> Starting database..."
compose up -d db
echo "==> Waiting for database readiness..."
until compose exec -T db pg_isready -U "$PGUSER_VALUE" -d "$PGDB_VALUE" >/dev/null 2>&1; do
sleep 1
done
echo "==> Running migrations..."
compose run --rm --no-deps migrate
echo "==> Verifying migration state..."
CURRENT_MIGRATION="$(compose exec -T db psql -U "$PGUSER_VALUE" -d "$PGDB_VALUE" -Atc "SELECT version::text FROM schema_migrations LIMIT 1;" 2>/dev/null || true)"
CURRENT_DIRTY="$(compose exec -T db psql -U "$PGUSER_VALUE" -d "$PGDB_VALUE" -Atc "SELECT dirty::text FROM schema_migrations LIMIT 1;" 2>/dev/null || true)"
if [[ -z "$CURRENT_MIGRATION" ]]; then
echo "ERROR: schema_migrations is missing or empty after migration run."
exit 1
fi
if [[ "$CURRENT_DIRTY" != "f" ]]; then
echo "ERROR: schema_migrations is dirty (dirty=$CURRENT_DIRTY, version=$CURRENT_MIGRATION)."
exit 1
fi
CURRENT_MIGRATION_NUM=$((10#$CURRENT_MIGRATION))
if [[ "$CURRENT_MIGRATION_NUM" -ne "$EXPECTED_MIGRATION_NUM" ]]; then
echo "ERROR: schema version mismatch. expected=$EXPECTED_MIGRATION_NUM current=$CURRENT_MIGRATION_NUM"
exit 1
fi
echo "==> Migration verification passed (version=$CURRENT_MIGRATION_NUM)."
echo "==> Rebuilding and restarting API..."
compose up -d --build --remove-orphans api
echo "==> Pruning old images..."
docker image prune -f || true
echo "==> Deployment complete."