Welcome!

Web 2.0 Authors: Esmeralda Swartz

Blog Feed Post

Continuous Integration and Continuous Deployment with Middleman, Codeship, and GitHub Pages

Continuous Integration and Continuous Deployment with Middleman, Codeship, and GitHub Pages

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.


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 master branch. It took a bit of trial and error, but I finally got something that works well.

Codeship – A hosted Continuous Deployment platform for web applications

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:

bundle install

Test Commands:

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 master branch using a Custom Script.

Custom Script:

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 master 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.

middleman-gh-pages doesn’t support that, but I opened a pull-request to add such support. If you’re interested in using it, change your Gemfile to point at this branch bundle.

gem "middleman-gh-pages", github: "rmm5t/middleman-gh-pages", branch: "commit-message-suffix"

Afterwards, you can add this to the bottom of your project’s Rakefile:

# 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.

Read the original blog entry...

More Stories By Manuel Weiss

I am the cofounder of Codeship – a hosted Continuous Integration and Deployment platform for web applications. On the Codeship blog we love to write about Software Testing, Continuos Integration and Deployment. Also check out our weekly screencast series 'Testing Tuesday'!