diff options
Diffstat (limited to 'deploy/deploy.sh')
| -rwxr-xr-x | deploy/deploy.sh | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/deploy/deploy.sh b/deploy/deploy.sh new file mode 100755 index 0000000..03ece4a --- /dev/null +++ b/deploy/deploy.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# Push the app to a server, install its dependencies there, swap it in, and +# restart. First-time setup is described in README.md; this is for updates. +# +# deploy/deploy.sh [ssh-target] +# +# Settings come from the environment or from deploy/deploy.env (not tracked; +# copy deploy/deploy.env.example): +# DEPLOY_TARGET ssh target, e.g. root@example.org (or the first argument) +# APP_DIR where the app lives (default /opt/yesod) +# SERVICE its systemd unit (default yesod) +# PORT its port, for the health check (default 8095) +# NPM the npm command on the server (default npm) +set -euo pipefail + +cd "$(dirname "$0")/.." +if [ -f deploy/deploy.env ]; then + # shellcheck source=/dev/null + . deploy/deploy.env +fi + +TARGET="${1:-${DEPLOY_TARGET:-}}" +APP_DIR="${APP_DIR:-/opt/yesod}" +SERVICE="${SERVICE:-yesod}" +PORT="${PORT:-8095}" +NPM="${NPM:-npm}" + +if [ -z "$TARGET" ]; then + echo "Set DEPLOY_TARGET in deploy/deploy.env, or pass an ssh target: deploy/deploy.sh root@example.org" >&2 + exit 1 +fi + +tar --exclude=node_modules --exclude=.git --exclude=deno.lock \ + --exclude='.passphrase*' --exclude='*.local.md' --exclude=deploy --exclude=tools -czf - . \ + | ssh "$TARGET" "set -e + rm -rf $APP_DIR.new + mkdir -p $APP_DIR.new + tar -C $APP_DIR.new --no-same-owner -xzf - + chmod -R u=rwX,go=rX $APP_DIR.new + cd $APP_DIR.new + $NPM ci --omit=dev --ignore-scripts --no-audit --no-fund --loglevel=error + rm -rf $APP_DIR.old + if [ -d $APP_DIR ]; then mv $APP_DIR $APP_DIR.old; fi + mv $APP_DIR.new $APP_DIR + systemctl restart $SERVICE + sleep 1 + systemctl is-active $SERVICE + curl -fsS http://127.0.0.1:$PORT/healthz && echo" |