From: Alex Pooley (@zuedev) Date: Mon, 23 Mar 2026 12:00:39 +0000 (+0000) Subject: refactor X-Git-Url: http://git.zue.dev/?a=commitdiff_plain;h=HEAD;p=git.zue.dev refactor --- diff --git a/cron/minutely.bash b/cron/minutely.bash index b1eee96..aac95af 100644 --- a/cron/minutely.bash +++ b/cron/minutely.bash @@ -1,71 +1,93 @@ #!/bin/bash +# +# minutely.bash — Cron job that syncs deployed files from the git repo +# and restarts services when configuration changes are detected. +# +# Runs every minute. Checks HEAD of the local bare repo against the +# deployed copies of: Caddyfile, HTML partials, and docker-compose.yaml. +# -GIT_ZUE_DEV="/home/git/git.zue.dev/" -DOCKER_COMPOSE_DIRECTORY="/root/" -DOCKER_COMPOSE_FILENAME="docker-compose.yaml" +set -euo pipefail -echo "This is a minutely cron job. Current time: $(date)" +# ============================================================================== +# Configuration +# ============================================================================== -# do we have the repo? -if [ -z "$GIT_ZUE_DEV" ]; then - echo "Repository not found. PANIC!" - exit 1 -fi - -# enter the repo -cd "$GIT_ZUE_DEV" || { echo "Failed to change directory to $GIT_ZUE_DEV. PANIC!"; exit 1; } - -# replace Caddyfile if it has changed -if ! cmp -s "$DOCKER_COMPOSE_DIRECTORY/Caddyfile" <(git show HEAD:"Caddyfile"); then - echo "Caddyfile has changed. Updating local copy..." - git show HEAD:"Caddyfile" > "$DOCKER_COMPOSE_DIRECTORY/Caddyfile" - CADDY_RESTART_NEEDED=true -else - echo "Caddyfile has not changed. No action needed." -fi +REPO_DIR="/home/git/git.zue.dev/" +DEPLOY_DIR="/root/" +COMPOSE_FILE="docker-compose.yaml" -# array of files to check for changes -HTML_FILES_TO_CHECK=( +HTML_FILES=( "site_header.html" "site_footer.html" "home_text.html" ) -# loop through each file and check for changes -for FILE in "${HTML_FILES_TO_CHECK[@]}"; do - if ! cmp -s "$DOCKER_COMPOSE_DIRECTORY/$FILE" <(git show HEAD:"html/$FILE"); then - echo "$FILE has changed. Updating local copy..." - git show HEAD:"html/$FILE" > "$DOCKER_COMPOSE_DIRECTORY/$FILE" +# ============================================================================== +# Helper functions +# ============================================================================== + +log() { echo "[$(date '+%H:%M:%S')] $*"; } +panic() { log "PANIC: $*"; exit 1; } + +# sync_file +# Copies a file from HEAD to the deploy directory if it has changed. +# Returns 0 if the file was updated, 1 if unchanged. +sync_file() { + local repo_path="$1" + local deploy_path="$2" + + if ! cmp -s "$deploy_path" <(git show HEAD:"$repo_path"); then + log "Changed: $repo_path -> $deploy_path" + git show HEAD:"$repo_path" > "$deploy_path" + return 0 else - echo "$FILE has not changed. No action needed." + log "Unchanged: $repo_path" + return 1 fi -done +} -# can we see the docker-compose.yaml file in the repo? -git show HEAD:"$DOCKER_COMPOSE_FILENAME" > /dev/null 2>&1 || { echo "Failed to find $DOCKER_COMPOSE_FILENAME in the repository. PANIC!"; exit 1; } +# ============================================================================== +# Main +# ============================================================================== -# compare our local copy of the docker-compose.yaml with the one in the repo -if ! cmp -s "$DOCKER_COMPOSE_DIRECTORY/$DOCKER_COMPOSE_FILENAME" <(git show HEAD:"$DOCKER_COMPOSE_FILENAME"); then - echo "$DOCKER_COMPOSE_FILENAME has changed. Updating local copy..." - git show HEAD:"$DOCKER_COMPOSE_FILENAME" > "$DOCKER_COMPOSE_DIRECTORY/$DOCKER_COMPOSE_FILENAME" +log "Starting minutely sync" - # then rebuild the containers - echo "Rebuilding Docker containers..." - cd "$DOCKER_COMPOSE_DIRECTORY" || { echo "Failed to change directory to $DOCKER_COMPOSE_DIRECTORY. PANIC!"; exit 1; } +# Validate and enter the repository +[ -d "$REPO_DIR" ] || panic "Repository directory $REPO_DIR does not exist" +cd "$REPO_DIR" +# --- Caddyfile ---------------------------------------------------------------- + +CADDY_RESTART_NEEDED=false +sync_file "Caddyfile" "${DEPLOY_DIR}/Caddyfile" && CADDY_RESTART_NEEDED=true + +# --- HTML partials ------------------------------------------------------------ + +for file in "${HTML_FILES[@]}"; do + sync_file "html/$file" "${DEPLOY_DIR}/$file" || true +done + +# --- docker-compose.yaml ----------------------------------------------------- + +git show HEAD:"$COMPOSE_FILE" > /dev/null 2>&1 \ + || panic "$COMPOSE_FILE not found in repository" + +if sync_file "$COMPOSE_FILE" "${DEPLOY_DIR}/$COMPOSE_FILE"; then + log "Rebuilding Docker containers..." + cd "$DEPLOY_DIR" docker compose down docker compose build docker compose up -d -else - echo "$DOCKER_COMPOSE_FILENAME has not changed. No action needed." + cd "$REPO_DIR" fi +# --- Caddy restart (if config changed) ---------------------------------------- + if [ "$CADDY_RESTART_NEEDED" = true ]; then - echo "Restarting Caddy container to apply new configuration..." - cd "$DOCKER_COMPOSE_DIRECTORY" || { echo "Failed to change directory to $DOCKER_COMPOSE_DIRECTORY. PANIC!"; exit 1; } + log "Restarting Caddy to apply new configuration..." + cd "$DEPLOY_DIR" docker compose restart caddy -else - echo "Caddy configuration has not changed. No restart needed." fi -echo "Minutely cron job completed." \ No newline at end of file +log "Sync complete" \ No newline at end of file