Automate git deployment of hugo websites

How can I autmatically deploy my hugo website, if I push a new commit to my git repo?

Starting situation

If you put your bare Git Repos to /srv/git/ and the websites to /srv/www/.

cd /srv
mkdir -p git www git/website
cd git/website
git init --bare
cd /srv/www/
git clone /srv/git/website website

Create post-receive file

To update another git repo from a post-receive hook, both variables GIT_WORK_TREE and GIT_DIR has to be set correctly.

#!/bin/bash
GIT_WORK_TREE=/path/to/a/git/repo/
GIT_DIR=${GIT_WORK_TREE}/.git

cd ${GIT_WORK_TREE}/ &&
  git pull && \
  git checkout -f master && \
  git clean -df && \
  hugo --enableGitInfo

Note

Currently, hugo (0.47.1) does not support bare repos if the --enableGitInfo option is used. See issue #5489.

Links