Files
com.gregkilmer.www/deploy/deploy.sh
T
Claude dcfd97e823 Deploy from GitHub with a systemd timer instead of Gitea Actions
Cosmos keeps a deploy-only clone of the GitHub repo. gk-deploy.timer runs
pull-and-deploy.sh every 5 minutes: it fetches master and, when it has
moved, runs deploy.sh (build, check, restart, roll back if unhealthy). A
commit that failed is not retried until master moves (FORCE=1 retries), and
DEPLOY_HEALTHCHECK_URL turns red on a failed fetch or deploy. Removes the
Gitea workflow and updates the runbook, README and inventory note.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012aSbo2D1qP8cMFQCykf7kx
2026-10-12 00:57:18 +00:00

62 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Builds the three images for the checked-out commit, runs the checks, and
# restarts the stack on it. If the new containers don't come up healthy, the
# stack goes back to the last good tag. Run from the repo root on Cosmos, by the
# deploy timer (pull-and-deploy.sh) or by hand. See docs/deploy.md.
set -euo pipefail
STACK="${GK_STACK_DIR:-/home/greg/homelab/gregkilmer}"
ENV_FILE="$STACK/.env"
TAG="$(git rev-parse --short=12 HEAD)"
compose() { docker compose --project-directory "$STACK" -f "$STACK/compose.yaml" "$@"; }
setting() { grep -E "^$1=" "$ENV_FILE" | tail -n 1 | cut -d= -f2- || true; }
set_setting() {
if grep -qE "^$1=" "$ENV_FILE"; then sed -i "s|^$1=.*|$1=$2|" "$ENV_FILE"; else echo "$1=$2" >> "$ENV_FILE"; fi
}
for f in .env content.env admin.env site.env; do
[ -f "$STACK/$f" ] || { echo "Missing $STACK/$f (see docs/deploy.md)"; exit 1; }
done
CURRENT="$(setting GK_TAG)"
PREVIOUS="$(setting GK_PREVIOUS_TAG)"
if [ "$CURRENT" = "$TAG" ] && [ "${FORCE:-}" != "1" ]; then
echo "Already running $TAG; nothing to do (FORCE=1 to rebuild)."
exit 0
fi
echo "== Checks"
docker build --target check .
echo "== Images for $TAG"
for t in content admin site; do
docker build --target "$t" -t "gregkilmer/$t:$TAG" .
done
echo "== Deploy"
mkdir -p "$STACK/data/content"
cp deploy/compose.yaml "$STACK/compose.yaml"
set_setting GK_TAG "$TAG"
[ -n "$CURRENT" ] && set_setting GK_PREVIOUS_TAG "$CURRENT"
if ! compose up -d --remove-orphans --wait --wait-timeout 120; then
echo "!! $TAG did not come up healthy"
compose ps
compose logs --tail 50 || true
if [ -n "$CURRENT" ]; then
echo "== Rolling back to $CURRENT"
set_setting GK_TAG "$CURRENT"
set_setting GK_PREVIOUS_TAG "$PREVIOUS"
compose up -d --wait --wait-timeout 120
fi
exit 1
fi
echo "== Prune old images (keeping $TAG and ${CURRENT:-none})"
for t in content admin site; do
docker images "gregkilmer/$t" --format '{{.Tag}}' | while read -r old; do
[ "$old" = "$TAG" ] || [ "$old" = "$CURRENT" ] || docker rmi "gregkilmer/$t:$old" >/dev/null || true
done
done
echo "Deployed $TAG"