Add containerized staging deployment
Validate and deploy staging / validate (push) Successful in 27s
Validate and deploy staging / deploy (push) Successful in 52s

This commit is contained in:
JB
2026-07-26 10:31:24 -04:00
parent 32e10c710b
commit 548d19d9ea
25 changed files with 1692 additions and 21 deletions
+29
View File
@@ -0,0 +1,29 @@
#!/bin/sh
set -eu
bundle_path=${1:-}
deployment_directory=${2:-/opt/shd-build-stage}
if [ -z "$bundle_path" ] || [ ! -f "$bundle_path" ]; then
echo "Usage: bootstrap-staging.sh <bootstrap-bundle.json> [deployment-directory]" >&2
exit 2
fi
compose_file="$deployment_directory/compose.staging.yml"
deploy_environment="$deployment_directory/.env.deploy"
compose() {
docker compose --env-file "$deploy_environment" -f "$compose_file" "$@"
}
restart_services() {
compose up -d --no-build app scheduler
}
trap restart_services EXIT
echo "Stopping staging before the one-time database bootstrap."
compose stop scheduler app || true
compose run --rm --no-deps \
-e ALLOW_STAGING_BOOTSTRAP=true \
-v "$bundle_path:/import/staging-bootstrap.json:ro" \
app node scripts/stage-bootstrap-import.mjs \
/import/staging-bootstrap.json --confirm-empty-staging-replace
echo "Staging bootstrap completed."
+56
View File
@@ -0,0 +1,56 @@
name: shd-build-stage
services:
app:
image: ${SHD_IMAGE:-shd-build-stage:local}
restart: unless-stopped
init: true
env_file:
- .env.staging.runtime
environment:
HOST: 0.0.0.0
PORT: 3000
BODY_SIZE_LIMIT: 6M
SKIP_DEFAULT_USERS: "true"
TDR_DATABASE_PATH: /app/data/shd-build-staging.db
TDR_AVATAR_STORAGE_PATH: /app/data/avatars
TDR_GAME_ICON_STORAGE_PATH: /app/data/game-icons
ports:
- "127.0.0.1:${SHD_STAGE_PORT:-5569}:3000"
volumes:
- shd_stage_data:/app/data
healthcheck:
test:
- CMD
- node
- -e
- fetch('http://127.0.0.1:3000/api/health/database').then(r=>{if(!r.ok)process.exit(1)}).catch(()=>process.exit(1))
interval: 15s
timeout: 5s
retries: 8
start_period: 30s
stop_grace_period: 30s
scheduler:
image: ${SHD_IMAGE:-shd-build-stage:local}
restart: unless-stopped
init: true
command:
- scheduler
env_file:
- .env.staging.runtime
environment:
SKIP_DEFAULT_USERS: "true"
TDR_DATABASE_PATH: /app/data/shd-build-staging.db
TDR_AVATAR_STORAGE_PATH: /app/data/avatars
TDR_GAME_ICON_STORAGE_PATH: /app/data/game-icons
volumes:
- shd_stage_data:/app/data
depends_on:
app:
condition: service_healthy
stop_grace_period: 30s
volumes:
shd_stage_data:
name: shd-build-stage-data
+18
View File
@@ -0,0 +1,18 @@
#!/bin/sh
set -eu
if [ "${1:-}" = "serve" ]; then
node scripts/db-migrate.mjs
exec node build
fi
if [ "${1:-}" = "scheduler" ]; then
while true; do
node scripts/system-notification-dispatch.mjs
node scripts/oversight-notification-dispatch.mjs
node scripts/notification-digest.mjs
sleep 60
done
fi
exec "$@"
+51
View File
@@ -0,0 +1,51 @@
#!/bin/sh
set -eu
deployment_directory=${1:-/opt/shd-build-stage}
source_directory=${2:-}
candidate_image=${3:-}
if [ -z "$source_directory" ] || [ ! -f "$source_directory/Dockerfile" ] || [ -z "$candidate_image" ]; then
echo "Usage: deploy-staging.sh <deployment-directory> <source-directory> <candidate-image>" >&2
exit 2
fi
compose_file="$deployment_directory/compose.staging.yml"
deploy_environment="$deployment_directory/.env.deploy"
compose() {
docker compose --env-file "$deploy_environment" -f "$compose_file" "$@"
}
echo "Building the candidate image while the current staging service remains online."
docker build --label org.opencontainers.image.title=shd-build -t "$candidate_image" "$source_directory"
echo "Stopping SHD staging processes before accessing the embedded database."
compose stop scheduler app || true
echo "Creating a timestamped database backup when a staging database exists."
compose run --rm --no-deps app sh -c \
'if [ -f "$TDR_DATABASE_PATH" ]; then node scripts/db-backup.mjs; else echo "No staging database exists yet; skipping backup."; fi'
echo "Applying additive schema migrations."
compose run --rm --no-deps app node scripts/db-migrate.mjs
echo "Starting the application and notification scheduler."
compose up -d --no-build --remove-orphans app scheduler
container_id=$(compose ps -q app)
attempt=0
while [ "$attempt" -lt 30 ]; do
status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container_id")
if [ "$status" = "healthy" ]; then
echo "SHD staging deployment is healthy."
exit 0
fi
if [ "$status" = "unhealthy" ] || [ "$status" = "exited" ] || [ "$status" = "dead" ]; then
break
fi
attempt=$((attempt + 1))
sleep 2
done
echo "SHD staging failed its health check."
compose logs --tail=150 app
exit 1