- infra/deploy/deploy.sh <dev|prod> [ref]: git archive → scp → remote.sh на сервере; руками и из CI, хост — DEPLOY_HOST (любой Docker-хост); каждый шаг с ретраями - remote.sh: атомарная замена app/ (app.old для отката), compose -p tgit-<env> up --build, ожидание healthy, REVISION, подсказка отката при неудаче; безопасно перезапускать - окружение без /srv/tgit/<env>/.env пропускается (prod — когда будет свой токен) - .gitea/workflows/deploy.yml: push в dev/main → deploy; guard «актуален ли пуш», ключ через env Refs #12 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
35 lines
1.5 KiB
Bash
Executable File
35 lines
1.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Deploy a git ref of the bot to a server. Used by hand and by CD (.gitea/workflows/deploy.yml).
|
|
# infra/deploy/deploy.sh <dev|prod> [git-ref] default ref: HEAD
|
|
# Env: DEPLOY_HOST (default tgit-deploy@72.56.247.166 — the vobot host; any Docker host works)
|
|
# DEPLOY_SSH_OPTS (extra ssh/scp options, e.g. -i key)
|
|
# Server layout: /srv/tgit/<env>/{.env, app/, app.old/, REVISION}; secrets live only in /srv/tgit/<env>/.env.
|
|
# The host drops some ssh connections — every remote step is retried, and each one is safe to repeat.
|
|
set -euo pipefail
|
|
ENV=${1:?usage: deploy.sh <dev|prod> [ref]}
|
|
REF=${2:-HEAD}
|
|
case "$ENV" in dev | prod) ;; *) echo "env must be dev or prod" >&2; exit 2 ;; esac
|
|
HOST=${DEPLOY_HOST:-tgit-deploy@72.56.247.166}
|
|
OPTS=${DEPLOY_SSH_OPTS:-}
|
|
DIR=/srv/tgit/$ENV
|
|
SHA=$(git rev-parse --short "$REF")
|
|
TGZ=$(mktemp --suffix=.tgz)
|
|
trap 'rm -f "$TGZ"' EXIT
|
|
git archive --format=tar.gz -o "$TGZ" "$REF"
|
|
|
|
retry() { local n=0; until "$@"; do n=$((n + 1)); [ $n -ge 5 ] && return 1; echo "retry $n: $1" >&2; sleep $((n * 5)); done; }
|
|
|
|
# shellcheck disable=SC2086
|
|
if ! retry ssh $OPTS "$HOST" "test -f $DIR/.env"; then
|
|
echo "::notice::$HOST:$DIR/.env not found — environment '$ENV' is not configured, skipping deploy"
|
|
exit 0
|
|
fi
|
|
|
|
echo "deploy $REF ($SHA) -> $HOST:$DIR"
|
|
# shellcheck disable=SC2086
|
|
retry scp -q $OPTS "$TGZ" "$HOST:$DIR/release-$SHA.tgz"
|
|
# shellcheck disable=SC2086
|
|
retry scp -q $OPTS infra/deploy/remote.sh "$HOST:$DIR/remote.sh"
|
|
# shellcheck disable=SC2086
|
|
retry ssh $OPTS "$HOST" "bash $DIR/remote.sh $ENV $SHA"
|