GitHub Pages Publication with Git Hooks

March 08 2014

As I wrote at length a couple weeks ago, this blog is hosted on GitHub Pages and generated by Pelican. Generally the integration between the two is quite blissful, except for managing two separate repositories - blog-source for my blog's source content, configuration files, and theme, and for my Pelican-generated site.

Lately I've been pushing to these two separate repositories manually, so my workflow looks something like:

$ cd blog/      # root directory for my blog
$ make devserver    # automatically re-generates site and hosts site locally
# change something in content or settings
$ git add --all
$ git commit -m "committing blog source"
$ git push origin master    # pushes to my blog-source repo on GitHub
$ cd output/    # pelican-generated output folder
$ git commit -am "committing pelican-generated site content"
$ git push origin master    # pushes to my repo

But alas! I have figured out how to automatically push any changes committed in my root blog directory to my blog-source repo and then automatically add, commit, and push changes to my GitHub Pages repo. Now my workflow looks like:

$ cd blog/
$ make devserver
# change something in content or settings
$ git add --all
$ git commit -m "committing blog source"
# everything else happens automatically! no more typing! yay!

The key is to use Git Hooks. For a particular .git repo, you can add an executable file in .git/hooks which will automatically execute before or after an event like commit. The available types of hooks can be found here.

For this specific automation task, I used a post-commit hook. To do this, first create the file .git/hooks/post-commit:

$ cd blog/
$ subl .git/hooks/post-commit

Add these lines to the file:

git push origin master
cd output/
git add --all       # sub-optimally will add all even if you didn't add all to blog-source
git commit -m "automatic commit"    # add whatever commit message you want
git push origin master

Then, to make the file executable, type:

$ chmod a=r+w+x .git/hooks/post-commit

And that's it! Joyful automation!


2014-03-14: Hacker Schooler Matthew Avant has a superior method using a pre-push hook (rather than a post-commit hook). I'm using his method now :)

tags: github pages pelican git blogging hacker school