#!/usr/bin/env bash # Runs on the server: unpack release-.tgz, swap app/ atomically, compose up, wait for healthy. # bash /srv/tgit//remote.sh (uploaded by deploy.sh; safe to re-run) set -euo pipefail ENV=$1 SHA=$2 DIR=/srv/tgit/$ENV PROJECT=tgit-$ENV cd "$DIR" if [ -f "release-$SHA.tgz" ]; then rm -rf app.new && mkdir app.new && tar -xzf "release-$SHA.tgz" -C app.new && rm -f "release-$SHA.tgz" cp .env app.new/.env rm -rf app.old [ -d app ] && mv app app.old mv app.new app fi cd app # Labels on the bot container, shown on its «Статус» card export TGIT_SHA=$SHA TGIT_DEPLOYED_AT TGIT_DEPLOYED_AT=$(date -u +%Y-%m-%dT%H:%M:%SZ) docker compose -p "$PROJECT" up -d --build --remove-orphans /dev/null || true) [ "$status" = healthy ] && break sleep 3 done if [ "$status" != healthy ]; then echo "bot is '$status' after 2 min — last logs:" >&2 docker logs --tail 50 "$PROJECT-bot-1" >&2 || true echo "rollback: cd $DIR && rm -rf app && mv app.old app && cd app && docker compose -p $PROJECT up -d --build" >&2 exit 1 fi echo "$SHA" > ../REVISION docker image prune -f >/dev/null echo "bot healthy @ $SHA ($ENV)"