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
+12
View File
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -euo pipefail
repo_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
env_file=${MERCHANTFLOW_ENV_FILE:-/opt/merchantflow/production.env}
if [[ ! -r "$env_file" ]]; then
echo "Missing production configuration: $env_file (run deploy/init-env.sh first)" >&2
exit 1
fi
# Use an explicit file so the local development .env is never loaded by Compose.
exec docker compose --project-name "${MERCHANTFLOW_PROJECT:-merchantflow}" \
--env-file "$env_file" -f "$repo_dir/compose.production.yaml" "$@"
+24
View File
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -euo pipefail
umask 077
env_file=${1:-/opt/merchantflow/production.env}
if [[ -e "$env_file" ]]; then
echo "Refusing to overwrite existing configuration: $env_file" >&2
exit 1
fi
command -v openssl >/dev/null
password=$(openssl rand -hex 32)
mkdir -p "$(dirname "$env_file")"
# noclobber also protects against a concurrent initialization.
set -o noclobber
cat > "$env_file" <<EOF
POSTGRES_PASSWORD=$password
DATABASE_URL=postgresql://merchantflow:$password@db:5432/merchantflow
APP_ORIGIN=https://merchantflow.prvt.you
NODE_ENV=production
API_HOST=0.0.0.0
API_PORT=3001
EOF
unset password
echo "Created $env_file with private permissions. Keep it outside Git."
+7
View File
@@ -0,0 +1,7 @@
# Reference only. Run init-env.sh to generate the actual file outside the repo.
POSTGRES_PASSWORD=replace-with-generated-hex-password
DATABASE_URL=postgresql://merchantflow:replace-with-generated-hex-password@db:5432/merchantflow
APP_ORIGIN=https://merchantflow.prvt.you
NODE_ENV=production
API_HOST=0.0.0.0
API_PORT=3001
+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"
+27
View File
@@ -0,0 +1,27 @@
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
server_tokens off;
location = /healthz {
access_log off;
default_type text/plain;
return 200 "ok\n";
}
# Host Nginx routes API traffic directly to the API container.
location = /api { return 404; }
location ^~ /api/ { return 404; }
location /assets/ {
try_files $uri =404;
add_header Cache-Control "public, max-age=31536000, immutable";
}
location / {
try_files $uri $uri/ /index.html;
add_header Cache-Control "no-cache" always;
}
}