#!/bin/bash
-# theme repository
+# ... (Keep your variable definitions the same) ...
THEME_REPO=$HOME/repositories/danix2-hugo-theme.git
-# repository logical name
REPO="danix.xyz-2"
-# Directory where to work on our site
TARGET_DIR=/tmp/danix.xyz-compile
GIT_DIR=$HOME/repositories/${REPO}.git
-# Public dir where to push the site once compiled
PUBLIC_WWW="/var/www/html"
BACKUP_WWW=$HOME/danix.xyz-backup
SITE_DOMAIN=https://danix.xyz
-# Branch that is going to be deployed to server
BRANCH="master"
-# date to be appended to latest tag
NOW=$(date +"%d%m%Y-%H%M")
-# Log file
LOG_DIR=/tmp/hugo-danix-site
LOG_FILE=$LOG_DIR/post-receive.log
-
set -e
-
-# Ensure log directory exists
mkdir -p "$LOG_DIR"
-
-# Tee all output (stdout + stderr) to the log file, appending
exec > >(tee -a "$LOG_FILE") 2>&1
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
-log "========================================"
-log "POST-RECEIVE HOOK STARTED"
-log "Repo : $REPO"
-log "Branch : $BRANCH"
-log "Tag : release_$NOW"
-log "========================================"
+# The hook receives oldrev, newrev, and ref via stdin
+while read oldrev newrev ref
+do
+ # 1. IMMEDIATE BRANCH CHECK
+ # We exit the loop early if the ref is not the branch we want to deploy.
+ if [[ "$ref" != "refs/heads/$BRANCH" ]]; then
+ log "Ref $ref received. Doing nothing: only the $BRANCH branch triggers a build."
+ continue
+ fi
-# delete the working directory first
-log "Removing old working directory: $TARGET_DIR"
-rm -rf $TARGET_DIR
+ # 2. REPOSITORY CHECK
+ # Verify Gitolite is handling the correct repository.
+ if [[ "$GL_REPO" != "$REPO" ]]; then
+ log "GL_REPO='$GL_REPO' does not match '$REPO'. Skipping."
+ continue
+ fi
-# create new temporary site
-log "Creating fresh Hugo site at: $TARGET_DIR"
-/usr/local/bin/hugo new site $TARGET_DIR
+ # 3. START ACTUAL WORK
+ # Only if the checks above pass do we start modifying the filesystem.
+ log "========================================"
+ log "POST-RECEIVE HOOK STARTED (Target: $BRANCH)"
+ log "========================================"
-# backup public www directory first then setup trap
-log "Backing up $PUBLIC_WWW → $BACKUP_WWW"
-rsync -avz --no-t $PUBLIC_WWW/ $BACKUP_WWW
-trap "log 'A problem occurred. Reverting to backup.'; rsync -avz --no-t --del $BACKUP_WWW/ $PUBLIC_WWW; rm -rf $TARGET_DIR; log 'Revert complete.'" EXIT
+ log "Removing old working directory: $TARGET_DIR"
+ rm -rf "$TARGET_DIR"
-while read oldrev newrev ref
-do
- log "Received ref: $ref (${oldrev:0:8} → ${newrev:0:8})"
-
- # if TARGET_DIR is empty we don't want deploy for this project
- if [[ ! $TARGET_DIR == "" ]]; then
- if [[ "$GL_REPO" == "$REPO" ]]; then
- # let's check that we are deploying to the correct branch
- if [[ $ref = refs/heads/${BRANCH} ]]; then
- log "Deploying '$BRANCH' branch to production..."
-
- log "Checking out content into $TARGET_DIR"
- git --git-dir=$GIT_DIR --work-tree=. -C "$TARGET_DIR" checkout -f
-
- log "Cloning theme from $THEME_REPO"
- git clone $THEME_REPO ${TARGET_DIR}/themes/danix-xyz-hacker
- log "Theme cloned at commit: $(git -C ${TARGET_DIR}/themes/danix-xyz-hacker rev-parse --short HEAD)"
-
- log "Clearing $PUBLIC_WWW"
- rm -rf $PUBLIC_WWW/*
-
- log "Tagging: release_$NOW"
- git tag release_$NOW $BRANCH
-
- log "Running Hugo build..."
- /usr/local/bin/hugo -s $TARGET_DIR -d $PUBLIC_WWW -b "${SITE_DOMAIN}" -t "danix-xyz-hacker" --noTimes --minify
- log "Hugo build complete."
-
- log "========================================"
- log "DEPLOYMENT COMPLETED - ${REPO}"
- log "Target branch : ${BRANCH}"
- log "Target folder : ${PUBLIC_WWW}"
- log "Tag name : release_${NOW}"
- log "========================================"
- else
- log "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
- fi
- else
- log "GL_REPO='$GL_REPO' does not match '$REPO'. Skipping."
- fi
- else
- log "Target directory not declared. Skipping deploy to server."
- fi
+ log "Creating fresh Hugo site at: $TARGET_DIR"
+ /usr/local/bin/hugo new site "$TARGET_DIR"
+
+ log "Backing up $PUBLIC_WWW → $BACKUP_WWW"
+ rsync -avz --no-t "$PUBLIC_WWW/" "$BACKUP_WWW"
+
+ # Set trap only AFTER we know we are deploying
+ trap "log 'A problem occurred. Reverting to backup.'; rsync -avz --no-t --del $BACKUP_WWW/ $PUBLIC_WWW; rm -rf $TARGET_DIR;" EXIT
+
+ log "Checking out content into $TARGET_DIR"
+ git --git-dir="$GIT_DIR" --work-tree="$TARGET_DIR" checkout -f "$BRANCH"
+
+ log "Cloning theme from $THEME_REPO"
+ git clone "$THEME_REPO" "${TARGET_DIR}/themes/danix-xyz-hacker"
+
+ log "Clearing $PUBLIC_WWW"
+ rm -rf $PUBLIC_WWW/*
+
+ log "Tagging: release_$NOW"
+ git --git-dir="$GIT_DIR" tag "release_$NOW" "$BRANCH"
+
+ log "Running Hugo build..."
+ /usr/local/bin/hugo -s "$TARGET_DIR" -d "$PUBLIC_WWW" -b "${SITE_DOMAIN}" -t "danix-xyz-hacker" --noTimes --minify
+
+ # Clear the trap so it doesn't fire on a successful exit
+ trap - EXIT
+ log "DEPLOYMENT COMPLETED - ${REPO}"
+ log "========================================"
done
-trap - EXIT
-log "Hook finished successfully."
+log "Hook finished successfully."
\ No newline at end of file