Deploy a Hugo site with rsync
A Highly Recommended Alternative: Build Local, Then Sync
Network shares mounted via protocols like SMB or NFS can sometimes suffer from latency. Hugo writes files concurrently at blistering speeds, and network hiccups can cause the build to fail mid-process. The most robust Unix-native solution is to build locally into public/ and instantly push the files to your share using rsync.
The Optimized deploy.sh Script
Create a file named deploy.sh in the root of your Hugo project and paste the following content:
#!/usr/bin/env sh
# Exit immediately if a command exits with a non-zero status
set -eu
# --- CONFIGURATION ---
SSH_USER="your_username"
SSH_HOST="your.server.ip.or.domain"
SSH_PORT="22"
SRC="public/"
DEST="/var/www/html/your-site-folder/" # Absolute path on the server
# --- 1. BUILD THE SITE LOCALLY ---
echo "Building Hugo site..."
hugo --cleanDestinationDir --gc --minify
# --- 2. DEPLOY OVER SSH VIA RSYNC ---
echo "Syncing changes to the server..."
rsync \
-rtvzp \
--delete \
--rsh="ssh -p ${SSH_PORT}" \
"${SRC}" "${SSH_USER}@${SSH_HOST}:${DEST}"
echo "Deployment successful!"Why these specific flags?
• -rtvzp: Instead of using the catch-all -a (Archive) flag, this explicitly breaks down into Recursive, Timestamps, Verbose, Zip/compression, and Permissions. This avoids OS discrepancies regarding advanced file flags (like user groups or extended attributes) between macOS and Linux kernels.
• –delete: Deletes files on your target server that no longer exist in your local public/ directory. If you rename or delete a Markdown page locally, it won’t linger as a ghost page on your active site.
• –cleanDestinationDir: Instructs Hugo to clear out your local public/ folder entirely before generating a fresh build.
Setting Up Your Environment
- Make the script executable: Run this command once in your terminal to allow the script to execute:
chmod +x deploy.sh- Add to .gitignore:
Your compiled public/ directory does not need to be saved inside Git version control. Add it to your .gitignore:
public/- Deploy: Whenever you finish editing your documentation, simply run:
./deploy.shSet up SSH Key-Based Authentication.
To make your deploy.sh script run instantly without prompting you for a password, you need to set up SSH Key-Based Authentication. This works identically on both Linux and macOS. Instead of a password, your computer will use a secure cryptographic key pair to prove its identity to the target server.
Step 1: Generate an SSH Key Pair (On your local machine)
If you don’t already have an SSH key pair on your machine, open your terminal and run this command:
ssh-keygen -t ed25519 -C "hugo-deploy"• What to do: Press Enter to accept the default file location. When it asks for a passphrase, leave it blank (press Enter twice). If you add a passphrase, your script will prompt you for that passphrase instead of a password, defeating the automation.
Step 2: Copy the Public Key to your Production Server
You need to send the public half of your key to the web server so it recognizes your computer.
On Linux (and most macOS machines):
Run the built-in utility ssh-copy-id. Replace your_username and your.server.ip.or.domain with your server’s actual details:
ssh-copy-id -p 22 your_username@your.server.ip.or.domain(You will be asked for your server password one last time to authorize the transfer).
If ssh-copy-id is missing on macOS:
If your Mac doesn’t have that command installed, you can achieve the exact same result by running this single string instead:
cat ~/.ssh/id_ed25519.pub | ssh -p 22 your_username@your.server.ip.or.domain "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"Step 3: Test the Connection
Before running your script, verify that the password prompt is gone by attempting a manual connection:
ssh -p 22 your_username@your.server.ip.or.domainIf it drops you straight into the server’s terminal without asking for a password, it’s working perfectly! Type exit to return to your local machine.
Step 4: Run your script
Now you can fire off your script, and it will execute end-to-end completely hands-free:
./deploy.shInfo
Repeat Steps 1 & 2 on any author’s machine that needs to run the manual deploy.sh script. If you eventually move this logic into Forgejo Actions, you will copy the contents of ~/.ssh/id_ed25519 into the Forgejo Secrets panel instead so the automated runner can authenticate in the exact same way.