Deploying your static site with Travis CI | NetEngine

Deploying your static site with Travis CI

Rowan Thursday, 14 May 2015

So I know I just wrote a post about deploying your sites with Netlify, but this very website uses Travis CI for continuous deployment. There was just one minor issue that was preventing the CI/CD process running smoothly… Any Pull Requests submitted to master were deployed just the same as a commit directly to master.

At first I thought it was an issue with the configuration. Travis allows you to specify branches to run on, e.g.

branches:
  only:
    - master

script:
  - 'bundle exec middleman build && bundle exec middleman deploy' # etc.

So, great, it should only run on master. If you push a bunch of commits to say my_new_feature branch, no builds from Travis. Pull requests are treated a little differently though, when you submit a PR to merge that branch into master, it runs the build no matter what branch it came from.

Now this actually makes sense: test that the new feature branch is A-OK, so that it can be merged into master. The problem is we were running the deploy script on a successful build, so for this website it would, for example, publish a perfectly ‘valid’ blog post, even if it hadn’t been proofread. It was arguably a little too ambitious with the Continuous part of Continuous Delivery.

So, we’ve made some changes to the Travis setup. We’ve now removed the deploying element from the script, and separated it:

script:
  - 'npm install -g bower'
  - 'bower install -f'
  - 'bundle exec middleman build'

This tests that everything is OK with the build process, and if it succeeds…

after_success:
  - './deploy.sh'

Which runs the following in deploy.sh

#!/bin/bash

if ([ $TRAVIS_BRANCH == "master" ] && [ $TRAVIS_PULL_REQUEST == "false" ])
then
  bundle exec middleman s3_sync
  echo 'Website published successfully.'
else
  echo "Build successful, but not publishing!"
fi

We’ve moved the deploy script code into a separate bash file to get around syntax errors in YAML (though you can write the same bash inside your travis.yml, you just have to escape it).

Travis build...

Huzzah!

The end process is as follows:

Happy Continuous Delivering :)

comments powered by Disqus