aien-edge
aien-dev/aien-edge
Brought by drakestapleton
aien-edge / deploy / publish-site.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Build a site on the Spark and publish it to the edge host, then hot-reload. |
| 3 | # |
| 4 | # deploy/publish-site.sh aienos.com ~/workspace/aienos.com |
| 5 | # deploy/publish-site.sh drakestapleton.com ~/workspace/drakestapleton.com |
| 6 | # |
| 7 | # EDGE_HOST (ssh target of the edge host) comes from the environment or from |
| 8 | # deploy/publish.env (local-only, git-ignored; see deploy/publish.env.example). |
| 9 | set -euo pipefail |
| 10 | |
| 11 | here="$(cd "$(dirname "$0")" && pwd)" |
| 12 | if [ -z "${EDGE_HOST:-}" ] && [ -f "$here/publish.env" ]; then |
| 13 | . "$here/publish.env" |
| 14 | fi |
| 15 | |
| 16 | name="${1:?site name, for example aienos.com}" |
| 17 | src="${2:?path to the site checkout}" |
| 18 | edge="${EDGE_HOST:?set EDGE_HOST or create deploy/publish.env}" |
| 19 | |
| 20 | cd "$src" |
| 21 | npm ci --no-audit --no-fund |
| 22 | npm run build |
| 23 | |
| 24 | # Upload into a fresh release directory, then repoint the /srv/sites/<name> |
| 25 | # symlink atomically so visitors never see a half-copied site. The server holds |
| 26 | # every file in memory, so it keeps serving the old copy until the reload. |
| 27 | stamp="$(date -u +%Y%m%dT%H%M%SZ)" |
| 28 | rsync -a --delete dist/ "$edge:/srv/sites/releases/$name-$stamp/" |
| 29 | ssh "$edge" "set -e |
| 30 | cd /srv/sites |
| 31 | ln -sfn releases/$name-$stamp .next-$name |
| 32 | mv -T .next-$name $name |
| 33 | sudo systemctl reload aien-edge |
| 34 | ls -1d releases/$name-* | head -n -3 | xargs -r rm -rf" |
| 35 | echo "published $name ($stamp)" |