⌨ Update to my Hugo publish.sh script

mrrcollins

Ryan Collins

Posted on May 11, 2021

⌨ Update to my Hugo publish.sh script

I ran in to a little issue with my publish.sh 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!

#!/bin/bash

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

cd /home/goz/Web/gozgeek.com/
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..."
    exit
fi

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/ gozgeek:gozgeek.com/

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
mrrcollins
Ryan Collins

Posted on May 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related