Travis CI for Middleman Deploys to Github Pages

ByJordan Andree

One of the goals for the build out my personal website was to explore implementing Travis CI for my project. I had heard of Travis CI before because lets face it, its nearly impossible to visit an open-source project these days without seeing one these badges (yes, I’m allured by them too).

I wanted to see how it would work using Travis as an automated build and deploy tool with my static site generator of choice, Middleman.

.travis.yml

The nice thing about Travis is all you need is one YAML file in your repo to get things started. I like the simplicity of the single file approach since I can see what all is going on it one place.

Here is my .travis.yml:

language: ruby
rvm:
  - 2.2.3
before_install:
  - rvm install 2.2.3
install:
  - bundle install --path vendor/bundle
script:
  - git config --global user.name '${GIT_NAME}'
  - git config --global user.email '${GIT_NAME}'
  - git remote set-url origin https://jordanandree:${GH_TOKEN}@github.com/jordanandree/ld.git
  - bundle exec middleman build
  - bundle exec middleman deploy
branches:
  only:
  - master
env:
  global:
    - secure: [..]

The first few lines for language, rvm, and before_install, and install are all pretty self-explanatory: this is a ruby project, so use rvm to install ruby version 2.2.3. Travis will also install the project dependencies via bundler in the install section.

The script section is a little more complex. First, since we are using Github Pages to host the site, we need to setup git so that it knows who we are. The first two lines of script cover that.

Next, we need to make sure git knows where we are deploying to. The GH_TOKEN part of the remote is setup by grabbing a Personal Access Token from your Github settings page. That is then encrypted by means of the Travis CLI gem so that it can stored in your .travis.yml under the env->global key, but only accessible by Travis itself.

For sake of secrecy from search robots, I encryped my GIT_NAME as well for the git config lines.

After that, the Travis will run the build and then the deploy by use of the Middleman Deploy extension.

Next time your do git push, Travis will pickup your commits, run the build steps, and deploy your site.

If you need to see a working example, you can checkout the source for my site here: