#!/bin/sh set -eu usage() { printf 'Usage: %s \n' "$0" >&2 printf 'Services: braindance chrome-ledger dark-tariff delamain pamsave sondib towercore\n' >&2 } if [ "$#" -ne 1 ]; then usage exit 2 fi service=$1 env_file= case "$service" in braindance) compose_file=braindance/compose.yml ;; chrome-ledger) compose_file=chrome-ledger/compose.yml ;; dark-tariff) compose_file=dark-tariff/compose.yml env_file=dark-tariff/env ;; delamain) compose_file=delamain/docker-compose.yml ;; pamsave) compose_file=pamsave/compose.yml env_file=pamsave/.env ;; sondib) compose_file=sondib/compose.yml ;; towercore) compose_file=towercore/docker-compose.yml ;; *) usage exit 2 ;; esac repo_root=$(git rev-parse --show-toplevel) current_dir=$(pwd -P) repo_root=$(CDPATH= cd -- "$repo_root" && pwd -P) if [ "$current_dir" != "$repo_root" ]; then printf 'Run this helper from the repository root: %s\n' "$repo_root" >&2 exit 1 fi worktree_status=$(git status --porcelain) if [ -n "$worktree_status" ]; then printf 'Refusing to deploy: the VPS worktree is not clean.\n' >&2 exit 1 fi old_head=$(git rev-parse HEAD) print_rollback() { status=$? trap - 0 if [ "$status" -ne 0 ]; then printf 'Redeploy failed. No automatic rollback was performed.\n' >&2 printf 'Rollback: git restore --source=%s --worktree -- %s && ' "$old_head" "$service" >&2 if [ -n "$env_file" ]; then printf 'docker compose --env-file %s -f %s up -d --build\n' "$env_file" "$compose_file" >&2 else printf 'docker compose -f %s up -d --build\n' "$compose_file" >&2 fi fi exit "$status" } trap print_rollback 0 printf 'Pulling origin/main for %s...\n' "$service" git pull --ff-only origin main new_head=$(git rev-parse HEAD) if [ ! -f "$compose_file" ]; then printf 'Compose file not found: %s\n' "$compose_file" >&2 exit 1 fi if [ -n "$env_file" ] && [ ! -f "$env_file" ]; then printf 'Environment file not found: %s\n' "$env_file" >&2 exit 1 fi printf 'Service: %s\n' "$service" printf 'Compose file: %s\n' "$compose_file" printf 'Old commit: %s\n' "$old_head" printf 'New commit: %s\n' "$new_head" set -- docker compose if [ -n "$env_file" ]; then set -- "$@" --env-file "$env_file" fi set -- "$@" -f "$compose_file" "$@" up -d --build "$@" ps