Task #167
closedTask #174: Automatically deploy blog on each fossil commit
Fossil: setup after-receive hooks to deploy nawan.my.id
Description
#!/bin/bash
# --- CONFIGURATION ---
# Path to the Fossil repository file on the server
FOSSIL_REPO="/path/to/mywebsite.fossil"
# The temporary directory to create the working copy for Hugo build
# This must be outside the web root.
WORK_DIR="/tmp/hugo_build_temp"
# The final destination for the static site files on the web server (via rsync)
# This is usually the document root (e.g., public_html)
# Replace user@host:path_to_webroot with your actual details
RSYNC_DEST="user@host:/var/www/myhugo-site-public"
# --- DEPLOYMENT LOGIC ---
# Ensure we operate in a clean environment
mkdir -p "$WORK_DIR"
cd "$WORK_DIR" || exit 1
# 1. Clean up any previous checkout (important for a fresh build)
rm -rf "$WORK_DIR"/*
# 2. Check out the latest committed version from the repository
# We use 'fossil open' to establish a checkout connection
# The '--once' flag tells it to only run the checkout and exit (not start a UI)
fossil open "$FOSSIL_REPO"
# 3. Build the Hugo site
# The 'hugo' command builds the site into the 'public/' subdirectory of the
# current working directory (which is $WORK_DIR)
# Use '--cleanDestinationDir' if you want a complete wipe before building
echo "Building Hugo site..."
hugo
# Check if the Hugo build was successful
if [ $? -ne 0 ]; then
echo "Hugo build failed! Aborting deployment."
exit 1
fi
# 4. Deploy the static files using rsync over SSH
# -a: archive mode (preserves permissions, ownership, etc.)
# -v: verbose
# -z: compress file data during transfer
# --delete: delete extra files from the destination that are not present in the source
echo "Deploying with rsync..."
rsync -avz --delete public/ "$RSYNC_DEST"
# 5. Clean up the temporary checkout directory after successful deployment
cd /
fossil close --force || true # Close the checkout connection
rm -rf "$WORK_DIR"
echo "Deployment complete."
Updated by Nawan 13 days ago
Nawan wrote in #note-23:
Memutuskan untuk mengimplementasikan
after-receivehook di repositori lokal alih-alih di server.
Ternyata lebih mudah jika di server karena after-receive hook tidak berjalan setelah mengkomit perubahan lokal dan dalam banyak kasus perlu dibantu dengan cron.
Updated by Nawan 13 days ago
ยท Edited
Because of the way I set up Fossil (#170), the after-receive hook script written in Bash will not work unless Bash, Hugo, and everything else necessary to run are mounted in the chroot directory. This is because, in my current configuration, Fossil automatically drops into the chroot jail. It may be worth considering using the before-commit hook instead, which I already use to test whether it can build.