Using Middleman and GitHub Pages with Codeship

Written by: Manuel Weiss
3 min read
Stay connected

This is a guest blog post by Ryan McGeary. Ryan runs Busyconf, where he aims to make conference planning easy. When we first saw his article we thought it would be a great tutorial about Continuous Integration and Continuous Deployment for static pages to publish on our blog. We got in contact with him and Ryan kindly provided his original article here.


Set up Continuous Deployment for Middleman

I use GitHub Pages for hosting some of my websites, and I use the Middleman static site generator as my content management system for some of these sites.

I like to run continuous integration for my projects whenever possible, and this goes for static site repositories as much as regular code repositories.

Recently, I started playing with Codeship for Continuous Integration and Continuous Deployment. It’s a well-priced continuous service, and I wanted to be able to automatically deploy my middleman site upon successful builds to the controller branch. It took a bit of trial and error, but I finally got something that works well.

Continuous Integration with Codeship – Test Settings

After initializing a new GitHub repository in Codeship, you’ll need to define how tests are run. Under the Test project settings in Codeship, select Ruby as the language and define your Setup Commands and Test Commands.

Setup Commands:

#!ruby
bundle install

Test Commands:

#!ruby
bundle exec middleman build

Perfect. Now your site will run a test build upon every code push.

Continuous Deployment with Codeship – Deployment Settings

This is where I had some trouble, but I finally found a set of commands that correctly deloyed to our gh-pages branch in the repository. First, it’s worth mentioning that I use the middleman-gh-pages gem for deploying to the gh-pages branch. This gem gives you a rake publish task that handles most of the dirty work.

Under the Deployment project settings in Codeship, configure a deployment from the controller branch using a Custom Script.

Custom Script:

#!ruby
git config --global user.email "robot@example.com"
git config --global user.name "Codeship Robot"
rm -rf build
git remote set-branches --add origin gh-pages
git fetch
bundle exec rake publish
sleep 30
wget --retry-connrefused --no-check-certificate -T 60 http://yoursite.com/

This configures a git user, removes the build directory that was left there from the test steps, adds a gh-pages remote branch (because Codeship only clones the relevant branch during setup), and runs the rake publish task to deploy the site to the gh-pages branch.

If all went well, your site will automatically deploy to GitHub Pages upon a push and successful build in the controller branch.

Extra Credit: Force Codeship to skip builds on the gh-pages branch.

Unfortunately, Codeship currently tries to run builds on all branches, including the gh-pages branch. This is undesirable for this setup, so to avoid this, we also need to add ”–skip-ci” or “[skip ci]” to the commit message that is pushed to the gh-pages branch.

Fortunately, after this pull-request by yours truly, middleman-gh-pages can support that.

If using version >= 0.0.3 (or the controller branch), you can add this to the bottom of your project’s Rakefile:

#!ruby
# Ensure builds are skipped when pushing to the gh-pages branch
ENV["COMMIT_MESSAGE_SUFFIX"] = "[skip ci]"

We want to thank Ryan for making his original blog post available. If you have any questions feel free to write them in the comments or ping Ryan on twitter.

Stay up to date

We'll never share your email address and you can opt out at any time, we promise.