#!/usr/bin/env bash # Build the leveva docs site and publish it to the root of the `www` branch. # The `www` branch is an orphan that contains ONLY the built site files. set -euo pipefail REMOTE="${1:-origin}" BRANCH="www" REPO_ROOT="$(git rev-parse --show-toplevel)" SITE_DIR="$REPO_ROOT/docs/site" PUBLIC_DIR="$SITE_DIR/public" # 1. Build the site fresh. rm -rf "$PUBLIC_DIR" (cd "$SITE_DIR" && zola build) # Disable Jekyll processing on static hosts that run it (keeps _-prefixed paths). touch "$PUBLIC_DIR/.nojekyll" # 2. Check out `www` into a throwaway worktree (created as an orphan if absent). # Drop any worktree admin entries left dangling by a previously-failed run. git -C "$REPO_ROOT" worktree prune WORKTREE="$(mktemp -d)" cleanup() { git -C "$REPO_ROOT" worktree remove --force "$WORKTREE" 2>/dev/null || rm -rf "$WORKTREE" git -C "$REPO_ROOT" worktree prune } trap cleanup EXIT # Resolve the base for `www`, preferring the REMOTE tip so repeated deploys from a # dev machine never build on (or try to fast-forward over) a stale local `www`: # - remote branch exists → fetch it and reset local `www` to the remote tip; # - only a local branch → build on it (a first push will create the remote); # - neither exists → start a fresh orphan (first-ever deploy). if git -C "$REPO_ROOT" ls-remote --exit-code --heads "$REMOTE" "$BRANCH" >/dev/null 2>&1; then git -C "$REPO_ROOT" fetch "$REMOTE" "$BRANCH" git -C "$REPO_ROOT" worktree add --force -B "$BRANCH" "$WORKTREE" "$REMOTE/$BRANCH" elif git -C "$REPO_ROOT" show-ref --verify --quiet "refs/heads/$BRANCH"; then git -C "$REPO_ROOT" worktree add --force "$WORKTREE" "$BRANCH" else git -C "$REPO_ROOT" worktree add --detach "$WORKTREE" git -C "$WORKTREE" checkout --orphan "$BRANCH" fi # 3. Replace the worktree's entire contents with the freshly built site. git -C "$WORKTREE" rm -rf --quiet . >/dev/null 2>&1 || true find "$WORKTREE" -mindepth 1 -maxdepth 1 -not -name '.git' -exec rm -rf {} + cp -a "$PUBLIC_DIR/." "$WORKTREE/" # 4. Commit and push only if something changed. git -C "$WORKTREE" add -A if git -C "$WORKTREE" diff --cached --quiet; then echo "www: already up to date, nothing to publish." else git -C "$WORKTREE" commit -m "Deploy docs site: $(date -u +%Y-%m-%dT%H:%M:%SZ)" git -C "$WORKTREE" push "$REMOTE" "$BRANCH" echo "www: published to $REMOTE/$BRANCH." fi