Auto publish blog to Github

Github pages is an excellent way to publish static sites, but I hate one aspect of their implementation: Having the static sites’ assets in another branch: gh-pages. I think this is just wrong, especially when considering many projects forced to have their site in the same repo.

I feel the coupling is unwanted and totally avoidable. Its much easier and saner to maintain a separate repo.

Also, I just don’t understand the point of having the static site built at the server. This makes Jekyll to be a hard dependency for Github pages. Thus needing the plugins to be approved by Github, since there is a security vulnerability to use any plugin.

Having said that, I too use Jekyll for this blog. But to avoid the need for using gh-pages, I maintain two separate projects. One that holds the built files in root (which will also will be used as static files for the site, in case they don’t find gh-pages branch).

Since the setup is just a one time task, its not a big deal. But publishing the blog is. So, to avoid repetition involved, I wrote a simple Rakefile that helps me publish the blog using a single command.

desc "Publish blog to Github"
task :publish, :message do |t, args|
  message = "Automated commit: #{args[:message] || 'Empty commit'}"

  Dir.chdir('_site') do
    system('git checkout -- CNAME')
    system('git add .')
    system("git commit -m '#{message}'")
    system("git push")

The Rakefile does the following:

  1. Accepts a parameter as the commit message
  2. Changes directory to build folder (_site)
  3. Restores the CNAME file that gets deleted when the site regenerates.
  4. Stage all changed files.
  5. Commit all the staged files with the commit message
  6. Push the change to server, effectively publishing the site.

Incase, someone else is also as paranoid to do this, the script may come in handy. By the way this very post is to test the script ;)