Something went wrong. Try again.
message board on atproto atchan.net
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134#!/usr/bin/env bash
set -euo pipefailcd "$(dirname "$0")"
log() { printf '\n-> %s\n' "$*"; }
HOST=atchan.netROOT="/opt/atchan"CURRENT="$ROOT/app" # symlink systemd resolves (WorkingDirectory=/opt/atchan/app)RELEASES="$ROOT/releases" # each deploy lands in its own timestamped dir hereSERVICE_NAME="atchan.service"KEEP=5 # old releases kept around for instant rollbackREADY_URL="https://atchan.net/readyz/"READY_TIMEOUT=60 # seconds to wait for the new release to answer /readyzRELEASE="$(date +%Y%m%d%H%M%S)-$(git rev-parse --short HEAD 2>/dev/null || echo nogit)"NEW="$RELEASES/$RELEASE"
# every ssh/rsync below reuses a single multiplexed connection instead of# opening its own — six back-to-back SSH handshakes was tripping the server's# rate limiting. the master stays up for the duration of the deploy and is# torn down on exit.CONTROL_PATH="$(mktemp -u /tmp/atchan-deploy-ssh.XXXXXX)"SSH_OPTS="-o ControlMaster=auto -o ControlPath=$CONTROL_PATH -o ControlPersist=60"ssh() { command ssh $SSH_OPTS "$@"; }rsync() { command rsync -e "command ssh $SSH_OPTS" "$@"; }cleanup() { command ssh $SSH_OPTS -O exit "$HOST" 2>/dev/null || true rm -rf "${STAGE:-}"}trap cleanup EXIT
log "building"npm install
# a lexicon edited without `npm run lexicons` leaves a stale CID in the# manifest, and one edited without publishing leaves the authorization server# expanding include: scopes from the old copy. both stop the deploy here.log "checking lexicons"(cd .. && npx ts-lex install --ci --no-save > /dev/null 2>&1) || { echo "lexicons.json is stale; run npm run lexicons" >&2 exit 1}npm run --silent lexicons:resolve
npm run build
# the server's node_modules is resolved here rather than on the box. native# modules ship as platform-specific binaries, and the two we depend on disagree# about how: better-sqlite3 (since v13) carries every prebuild in its own tarball# and picks one at runtime, but sharp resolves its binary through per-platform# optional deps, so the tree only comes out right when npm resolves it *for* the# target — debian x64, hence linux/x64/glibc. built in a staging dir because# `npm ci` in the repo would swap the dev node_modules for linux binaries.log "staging production deps for linux-x64"STAGE="$(mktemp -d)"cp ../package.json ../package-lock.json "$STAGE/"(cd "$STAGE" && npm ci --omit=dev --os=linux --cpu=x64 --libc=glibc)
# the glibc-gated packages are dropped silently if --libc is missing or the# platform flags drift, leaving @img/sharp-wasm32 as the fallback — a tree that# runs, slowly, without ever erroring. refuse to ship one.for binary in \ "$STAGE/node_modules/better-sqlite3/prebuilds/linux-x64.node" \ "$STAGE"/node_modules/@img/sharp-linux-x64/lib/*.nodedo [ -f "$binary" ] || { echo "missing linux-x64 native binary: $binary" >&2; exit 1; }done
log "staging release $RELEASE"
# seed the new release from the currently-live one with hardlinks, so the rsyncs# below only transfer the files that actually changed. the previous release is# resolved server-side; nothing here needs to come back to this machine.ssh "$HOST" " set -eu arch=\"\$(uname -m)\" [ \"\$arch\" = x86_64 ] || { echo \"server is \$arch, but deps were resolved for x64\" >&2; exit 1; } mkdir -p '$RELEASES'
# a pre-symlink install leaves a real directory at \$CURRENT: retire it into # releases/ so the symlink swap below has something to point at. if [ -e '$CURRENT' ] && [ ! -L '$CURRENT' ]; then mv '$CURRENT' '$RELEASES/legacy-$RELEASE' ln -sfn '$RELEASES/legacy-$RELEASE' '$CURRENT' fi
prev=\"\$(readlink -f '$CURRENT' 2>/dev/null || true)\" if [ -d \"\$prev\" ]; then cp -al \"\$prev\" '$NEW' else mkdir -p '$NEW/scripts' fi"
# each --delete mirrors one source tree onto its own subset of the release, so# every path a later rsync owns (deps, manifests, scripts, migrations) has to be# excluded here or the build sync would wipe it.rsync -azh --delete \ --exclude node_modules \ --exclude package.json --exclude package-lock.json \ --exclude scripts --exclude migrations \ ../build/ "$HOST:$NEW/"rsync -azh ../package.json ../package-lock.json "$HOST:$NEW/"rsync -azh --delete "$STAGE/node_modules/" "$HOST:$NEW/node_modules/"rsync -azh --delete ../migrations "$HOST:$NEW/"rsync -azh ../scripts/migrate.ts "$HOST:$NEW/scripts/"
log "swapping live release"ssh "$HOST" " set -eu chown -R atchan:atchan '$NEW'
systemctl stop '$SERVICE_NAME' ln -sfn '$NEW' '$CURRENT' systemctl start '$SERVICE_NAME' || true ready=0 for _ in \$(seq 1 $READY_TIMEOUT); do if curl -fs -m 2 -o /dev/null '$READY_URL'; then ready=1; break; fi sleep 1 done
if [ \"\$ready\" -ne 1 ]; then echo 'release $RELEASE never became ready after ${READY_TIMEOUT}s' >&2 exit 1 fi
echo 'deploy complete: $RELEASE'
# keep the last $KEEP releases for instant rollback, drop the rest cd '$RELEASES' ls -1dt */ 2>/dev/null | tail -n +$((KEEP + 1)) | xargs -r rm -rf"