DEV Community

Cover image for ⌨ Update to my Hugo script
Ryan Collins
Ryan Collins

Posted on • Originally published at on

⌨ Update to my Hugo script

I ran in to a little issue with my script that I use to automatically publish my site with Hugo. I wipe out the public folder and then rebuild the site with Hugo. After that is done, the public folder is synced to the web host, using the --delete option. The problem arises if Hugo encounters an error. The site isn’t rebuilt but the now empty public folder is synced, wiping out the website.

The script now checks Hugo’s exit code, and will only sync if the code is 0. I hope that helps someone!


if ! command -v /snap/bin/hugo &> /dev/null
    echo "Hugo not found. Install with:"
    echo "sudo snap install hugo"

cd /home/goz/Web/
echo "Building at `date`"

echo "Clear out public.."
[[-d "public"]] || mkdir "public"
rm -r public/*

echo "Building site..."
HUGO_ENV=production /snap/bin/hugo 

# If something goes wrong building the site, exit the script!
if [! $? -eq 0]; then 
    echo "Something is wrong..."

echo "Set up some RSS stuff..."
mkdir public/feed
mkdir public/feed/podcast
cp public/posts/index.xml public/feed/
cp public/tags/podcast/index.xml public/feed/podcast/

echo "Adding build date..."
date > public/built.txt
hostname >> public/built.txt

echo "Uploading site..."
rsync -e ssh -avp --progress --delete public/

Enter fullscreen mode Exit fullscreen mode

Top comments (0)