#!/bin/sh # Builds site/out and serves it at https://socialweb.computer/ from the gateway's /var/www/socialweb.computer (Caddy's # site block for it is in /etc/caddy/Caddyfile; nothing here touches Caddy or any other site). It prints what it would # change, saves the tree it replaces to /var/backups/socialweb.computer-.tgz on the gateway, swaps the new # tree in and checks every built file against what https://socialweb.computer/ serves. # site/deploy.sh [--build-only] [--dry-run] set -eu cd "$(dirname "$0")/.." SITE=socialweb.computer SSH_TARGET=${SSH_TARGET:-root@socialweb.computer} SSH_PORT=${SSH_PORT:-1997} build_only= dry_run= for a in "$@"; do case $a in --build-only) build_only=1 ;; --dry-run) dry_run=1 ;; *) echo "usage: site/deploy.sh [--build-only] [--dry-run]" >&2; exit 2 ;; esac done ssh="ssh -p $SSH_PORT -o BatchMode=yes $SSH_TARGET" out=site/out site/build.sh bad=$(cd "$out" && find . -mindepth 1 \( -name '.*' -o \( ! -type d ! -type f \) -o \( -type f ! -name '*.html' ! -name '*.css' ! -name '*.svg' ! -name '*.png' ! -name '*.ico' ! -name '*.txt' ! -name COMMIT ! -path ./install.sh \) \) -print) if [ -n "$bad" ]; then echo "deploy: refusing: $out holds files that are not site files:" >&2 echo "$bad" >&2 exit 1 fi commit=$(cat "$out/COMMIT") [ -z "$build_only" ] || exit 0 mkdir -p "$HOME/.tmp" work=$(mktemp -d "${TMPDIR:-$HOME/.tmp}/site-deploy.XXXXXX") trap 'rm -rf "$work"' EXIT (cd "$out" && find . -type f -exec sha256sum {} + | sort -k2) > "$work/new" $ssh "d=/var/www/$SITE; [ ! -d \"\$d\" ] || (cd \"\$d\" && find . ! -type d ! -type f -printf 'NOT-A-FILE %p\n'; find . -type f -exec sha256sum {} + | sort -k2)" > "$work/old" echo "deploy: $SSH_TARGET:/var/www/$SITE now -> $commit" awk 'NR == FNR { old[$2] = $1; next } { seen[$2] = 1; if (!($2 in old)) print " add " $2; else if (old[$2] != $1) print " change " $2 } END { for (p in old) if (!(p in seen)) print " remove " p }' "$work/old" "$work/new" | sort -k2 [ -z "$dry_run" ] || { echo "deploy: --dry-run: nothing changed"; exit 0; } case $commit in *-dirty) echo "deploy: refusing: the tree has changes not in a commit ($commit); commit them first" >&2 exit 1 ;; esac if [ -z "$(git for-each-ref --contains "$commit" refs/remotes/swc)" ]; then echo "deploy: refusing: $commit is on no swc branch; push it first (the footer links it)" >&2 exit 1 fi tar -C "$out" -cf - . | $ssh "rm -rf /var/www/$SITE.new && mkdir -p /var/www/$SITE.new && tar -C /var/www/$SITE.new --no-same-owner -xf -" $ssh "SITE=$SITE sh -s" <<'REMOTE' set -eu d=/var/www/$SITE ts=$(date -u +%Y%m%dT%H%M%SZ) find "$d.new" -type d -exec chmod 0755 {} + find "$d.new" -type f -exec chmod 0644 {} + chown -R caddy:caddy "$d.new" if [ -d "$d" ]; then tar -C /var/www -czf "/var/backups/$SITE-$ts.tgz" "$SITE" chmod 0600 "/var/backups/$SITE-$ts.tgz" echo "backup /var/backups/$SITE-$ts.tgz" fi rm -rf "$d.old" [ ! -d "$d" ] || mv "$d" "$d.old" mv "$d.new" "$d" rm -rf "$d.old" REMOTE echo "deployed $commit" site/deno.sh run --no-config --no-lock --allow-read="$out" --allow-net="$SITE" site/verify.ts "https://$SITE/" "$out"