diff options
Diffstat (limited to 'deploy')
| -rwxr-xr-x | deploy/post-receive | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/deploy/post-receive b/deploy/post-receive new file mode 100755 index 0000000..78e4ba2 --- /dev/null +++ b/deploy/post-receive @@ -0,0 +1,61 @@ +#!/bin/sh +# Deploy hook for cs:~/coinosv3-server.git. Install it as hooks/post-receive. +# +# A push to `prod` checks the commit out to $CHECKOUT, installs dependencies, +# restarts the services whose files changed, then mirrors the commit to +# git.coinos.io and gittr. What runs is always a commit: never copy files +# into the checkout by hand. +set -u +CHECKOUT=/home/adam/coinosv3-server +LOG=/home/adam/coinosv3-server-deploy.log +MIRROR=/home/adam/git/coinosv3-server.git [email protected]:npub1w27mc4aa6m0ufe3xs5z3m6qyr52gc0rglept7vqlwx4xeaf72tasnqz70r/coinosv3-server.git +BARE=$(pwd) +SERVICES="names nwcpush blossom" + +while read -r oldrev newrev ref; do + [ "$ref" = refs/heads/prod ] || continue + { + echo "== $(date -u +%FT%TZ) deploy $newrev" + mkdir -p "$CHECKOUT" + git --git-dir="$BARE" --work-tree="$CHECKOUT" checkout -f "$newrev" || { echo "checkout failed"; exit 1; } + cd "$CHECKOUT" || exit 1 + # the pinned coinosv3 commit comes from the coinosv3 repo on this host + env -u GIT_DIR git -C "$CHECKOUT" --git-dir="$BARE" --work-tree="$CHECKOUT" \ + -c submodule.coinosv3.url=/home/adam/coinosv3.git -c protocol.file.allow=always \ + submodule update --init --force || { echo "submodule update failed"; exit 1; } + docker run --rm -v "$CHECKOUT":/app -w /app oven/bun:1.3.14 bun install --frozen-lockfile --production \ + || { echo "bun install failed"; exit 1; } + + # which services to restart: their own directory changed, or something + # they all share did (the pin, dependencies, compose); everything on a + # first deploy + if [ "$oldrev" = 0000000000000000000000000000000000000000 ]; then + restart=$SERVICES + else + changed=$(git --git-dir="$BARE" diff --name-only "$oldrev" "$newrev") + if echo "$changed" | grep -qE '^(coinosv3|package\.json|bun\.lock|compose\.yml)$'; then + restart=$SERVICES + else + restart="" + for s in $SERVICES; do echo "$changed" | grep -q "^$s/" && restart="$restart $s"; done + # names imports bridge/ln.js + echo "$changed" | grep -q '^bridge/ln\.js$' && case "$restart" in *names*) ;; *) restart="$restart names";; esac + fi + fi + if [ -n "$restart" ]; then + echo "restarting:$restart" + docker compose -f "$CHECKOUT/compose.yml" --env-file /home/adam/coinosv3-state/.env up -d --force-recreate $restart \ + || echo "compose up failed" + else + echo "no service files changed" + fi + + # mirrors; a failure here never fails the deploy + git --git-dir="$BARE" push --quiet "$MIRROR" "$newrev:refs/heads/master" "refs/tags/*:refs/tags/*" \ + || echo "mirror push to git.coinos.io failed" + GIT_SSH_COMMAND="ssh -i /home/adam/.ssh/gittr_ed25519 -o IdentitiesOnly=yes -o BatchMode=yes -o ConnectTimeout=15" \ + timeout 120 git --git-dir="$BARE" push --quiet "$GITTR" "$newrev:refs/heads/master" "refs/tags/*:refs/tags/*" \ + || echo "mirror push to gittr failed" + } >> "$LOG" 2>&1 +done |
