Controlled jekyll republishing with git commit messages

November 2, 2015


Relevant XKCD

It’s only fitting to start this little scrapbook with a short snippet for Jekyll. I’m a typical engineer in that I am willing to spend five hours setting something up that will save me 20 seconds about every other month, so I was tempted by the git post-receive hook deployment method from the Jekyll documentation.

Such a complete automatism doesn’t fit my usual workflow, however. I often push unfinished/untested code, mostly for the benefit of a remote backup.And while Jekyll has drafts, that mechanism does not help with changes to the rest of the codebase.

What I wanted was a method to control which pushes lead to redeployments. I’ve settled on the following post-receive hook, which only redeploys if the last commit message contains the string
'p! ' (without quotes). This method is adaptable to perform any conditional post-push action based on commit messages or other information in the git log.

#!/home/<user>/.rvm/bin/rvm-shell
#
# Republish Jekyll when last commit message
# contains p!

LOG=`git log -1 --pretty=%B HEAD`

if [[ $LOG != *"p! "* ]]
then
echo "We're keeping this version private."
  exit;
fi

echo "Publishing... "

GIT_REPO=$HOME/myrepo.git
TMP_GIT_CLONE=$HOME/tmp/myrepo
PUBLIC_WWW=/var/www/myrepo

git clone $GIT_REPO $TMP_GIT_CLONE
jekyll build -s $TMP_GIT_CLONE -d $PUBLIC_WWW
rm -Rf $TMP_GIT_CLONE
echo "Done. Time to die. How fitting for a damn shell script."
exit

Caveats

I have a deep hatred for bash scripting and therefore not invested much time. Consider:

Redeploys only happen when the last commit message contains 'p! '. Any intermediate commits that where not pushed are ignored.

The 'p! ' trigger can occure anywhere in the commit message, not just at the beginning.

A better method would be to use branches for intermediate code. But we’re publishing a blog here and avoiding work clearly outranks orthodoxy.

Controlled Jekyll Republishing with git Commit Messages - November 2, 2015 - mw