Git Hooks With Hugo

I recently joined and wanted to host to start using the http hosting to document some technical learnings…I don’t have the greatest memory and writing things down helps with retention, as well as giving me a reference place of my own for things I’ve already learned (and since forgotten). To kick this off, I thought I should document the workflow for publishing new content…I’ve done this once or twice before but always forget the steps.


For both your local and remote machines, you will need:

Remote Machine Setup

Login to your remote machine using ssh

ssh user@remote.machine

Create a --bare git repository

git init --bare my-website.git

Local Machine Setup

First, create a hugo site on your local machine

hugo new site my-hugo-site
cd my-hugo-site

Initialize the directory as a git repository

git init

Add your remote machine as a remote

git remote add prod ssh://user@remote.machine:/home/user/my-website.git

That should be it for your local machine.

Git hooks

In your remote git repo, create a new git-hook, From your git directory:

cd hooks
nvim post-receive

I modified the code from a Digital Ocean tutorial…only slightly though. The script from that site has a flag to set the baseUrl on the command line, but I set this in the config.toml of my hugo site.



set -e

trap "echo 'A problem occurred.  Reverting to backup.'; rsync -aqz --del $BACKUP_WWW/ $PUBLIC_WWW; rm -rf $WORKING_DIRECTORY" EXIT

rm -rf $PUBLIC_WWW/*
/usr/bin/hugo -s $WORKING_DIRECTORY -d $PUBLIC_WWW --cacheDir /home/user/.cache/hugo
trap - EXIT


Now, after you create any new content on your local machine you can git push prod, and when your remote machine receives update it will automatically rebuild your Hugo site.