blob: 19f39849ab40fa4c0044f958251318da98389693 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#!/bin/bash
# ... (Keep your variable definitions the same) ...
THEME_REPO=$HOME/repositories/danix2-hugo-theme.git
REPO="danix.xyz-2"
TARGET_DIR=/tmp/danix.xyz-compile
GIT_DIR=$HOME/repositories/${REPO}.git
PUBLIC_WWW="/var/www/html"
BACKUP_WWW=$HOME/danix.xyz-backup
SITE_DOMAIN=https://danix.xyz
BRANCH="master"
NOW=$(date +"%d%m%Y-%H%M")
LOG_DIR=/tmp/hugo-danix-site
LOG_FILE=$LOG_DIR/post-receive.log
set -e
mkdir -p "$LOG_DIR"
exec > >(tee -a "$LOG_FILE") 2>&1
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
# 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
# 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
# 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 "========================================"
log "Removing old working directory: $TARGET_DIR"
rm -rf "$TARGET_DIR"
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
log "Hook finished successfully."
|