#!/bin/sh set -eu archive=$1 app=/var/www/meweb.arasaka.software backups=/var/backups/meweb.arasaka.software health=http://127.0.0.1:5480/api/info stamp=$(date -u +%Y%m%d%H%M%S) stage=$(mktemp -d) trap 'rm -rf "$stage"' EXIT tar -xzf "$archive" -C "$stage" test -x "$stage/MeWeb.Server" snapshot="" if [ -x "$app/MeWeb.Server" ]; then snapshot="$backups/meweb-$stamp" rsync -a "$app/" "$snapshot/" ls -1dt "$backups"/meweb-* 2>/dev/null | tail -n +4 | xargs -r rm -rf fi sudo systemctl stop meweb || true rsync -a --delete "$stage/" "$app/" sudo systemctl start meweb for attempt in $(seq 1 30); do if curl -fsS "$health" | grep -q "\"commit\":\"$COMMIT\""; then echo "deployed $COMMIT: $(curl -fsS "$health")" exit 0 fi sleep 1 done echo "::error::the new server did not answer with commit $COMMIT" sudo systemctl status meweb --no-pager | tail -20 || true if [ -n "$snapshot" ]; then sudo systemctl stop meweb || true rsync -a --delete "$snapshot/" "$app/" sudo systemctl start meweb echo "rolled back to $snapshot" fi exit 1