#!/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 [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, app/, app.old/, REVISION}; secrets live only in /srv/tgit//.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 [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"