Added deployment files

This commit is contained in:
Oleksandr Prokhorenko
2026-09-24 23:09:44 +03:00
parent 7f3a56a4ca
commit c345a8e91f
14 changed files with 402 additions and 5 deletions
+55
View File
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
set -euo pipefail
umask 077
seed_demo=false
if [[ $# -gt 1 || ( $# -eq 1 && $1 != --seed-demo ) ]]; then
echo "Usage: bash deploy/start.sh [--seed-demo]" >&2
exit 1
fi
if [[ ${1:-} == --seed-demo ]]; then seed_demo=true; fi
repo_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
env_file=${MERCHANTFLOW_ENV_FILE:-/opt/merchantflow/production.env}
state_dir=$(dirname "$env_file")
compose() { bash "$repo_dir/deploy/compose.sh" "$@"; }
command -v flock >/dev/null || { echo 'Run this script on the Linux VPS (flock required).' >&2; exit 1; }
[[ -r "$env_file" ]] || { echo "Missing $env_file; run init-env.sh first." >&2; exit 1; }
exec 9>"$state_dir/deploy.lock"
flock -n 9 || { echo 'Another deployment is running.' >&2; exit 1; }
compose config --quiet
command -v ss >/dev/null || { echo 'Install iproute2 for the port availability check.' >&2; exit 1; }
for pair in api:13001 web:18080; do
service=${pair%:*}
port=${pair#*:}
if [[ -n $(ss -H -ltn "sport = :$port") && -z $(compose ps --status running -q "$service") ]]; then
echo "Port $port is already occupied by another service. Resolve this before deployment." >&2
exit 1
fi
done
# Build before touching running services. Migration/seed commands never publish API ports.
compose build api web
compose up -d --wait --wait-timeout 120 db
mkdir -p "$state_dir/backups"
backup="$state_dir/backups/$(date -u +%Y%m%dT%H%M%SZ).dump"
compose exec -T db pg_dump -U merchantflow -d merchantflow -Fc > "$backup.tmp"
mv "$backup.tmp" "$backup"
compose run --rm --no-deps api pnpm --filter @merchantflow/database migrate
if $seed_demo; then
user_count=$(compose exec -T db psql -U merchantflow -d merchantflow -Atc 'SELECT count(*) FROM "User"')
if [[ "$user_count" != 0 ]]; then
echo 'Database already has users. Rerun without --seed-demo; existing data was not reseeded.' >&2
exit 1
fi
compose run --rm --no-deps api node packages/database/dist/seed.js
fi
if ! compose up -d --wait --wait-timeout 180 api web; then
echo "Startup failed. Inspect: bash deploy/compose.sh logs --tail=100 api web" >&2
echo "Database backup: $backup. No database rollback was attempted." >&2
exit 1
fi
echo 'App containers are healthy. Host Nginx can use 127.0.0.1:18080 and 127.0.0.1:13001.'
echo "Pre-migration database backup: $backup"