Along with technology to help open data publication, the ODI Labs team work on tools to help make open source code publication easier. ODI web developer Stuart Harrison introduces 'Henry', a tool to help you deploy Ruby gems faster and more frequently

Henry is a tool developed by ODI Labs to lower the barriers to publishing open source software. CC BY 2.0, uploaded by [davidwithacamera](https://www.flickr.com/photos/davidwithacamera/8735762528/in/photolist-eiX4Rj-acUjmF-4bovG5-iY8b5-8rFzHk-8zvhCt-7x3Arr-7x3At8-pwa9MS-gXVmsT-77P3vT-6wmd3u-4yFEiP-de88Zq-9n77JU-9ibgqA-77SWZW-iuDhWd-7hXjpC-Q4L3W-siaKXc-iMUCq-iMUCo-iMpV9-nfdQHV-iMpV7-bv7gRn-iMUCn-4XKeGw-bv7gPa-iMpVa-nuENLQ-ppP12m-iMpV6-76ZeS9-6XkzcH-sfVoQN-71dYXN-8RqpdM-8Rqpgp-fAts7o-b6fFHc-aFjc81-6FECmy-6Xpzku-bd84hV-d3RA1-6BQfCG-7pUwj9-amBpQP “davidwithacamera").

In the ODI Labs team, our weapon of choice for almost all software development work is Ruby. It's fast, pleasing to write and enormously flexible. Plus, it has a huge community of people writing open source code that we can build upon, meaning we don't need to reinvent the wheel every time we want to carry out a common task, like parsing XML, interfacing with an API, or connecting to a database.

As with most modern programming languages, these pieces of code are often wrapped up in packages, which in Ruby-land are called 'gems'. When taking on a new project, Ruby developers will pretty much always use at least one gem, and often many, many more.

Every now and then, we come across a problem that hasn't been solved, and if it's a problem we think other people may have, we write a gem of our own. Amongst other things, we've written gems to validate CSV files, extract metadata out of open data datasets and keep code dependencies up to date. When we're ready to share our code with the world, we publish our gem to Rubygems, which is the de-facto standard package repository for Ruby gems.

Continuous deployment for fun and for profit

To rewind for a minute, when we're building tools for the Web, such as CSVlint or Open Data Certificates, we employ a technique known as Continuous Deployment.

Put very simply, when we push our code up to GitHub, our tests run (currently in Travis), and if the code is on a master branch, a git tag marked CURRENT is applied to that version of the code. We have a job that runs every 10 minutes on our servers, and if it picks up that the CURRENT tag has changed, it deploys that version of the code to the server. This way we don't have to think about deploying our code when it changes, it just works.

I've often wished we could apply that approach to our gems. Usually, when you publish a new version of a gem, you need to write a Changelog (saying what you've added or changed), package your code into a new gem and push this up to Rubygems. It also helps to push a new tag in Github with the current version number, so people can see a snapshot of what the code looked like at a certain point.

This can be automated to a certain extent in Travis, using Travis's fantastic Rubygems integration, but I wanted more.

Sensibly, by default, Travis suggests that you only publish to Rubygems when you push a new tag to Github. This is fine and dandy, but still involves another manual step after you've written new code. You can often forget to do this, too, meaning users won't immediately get the benefit from your shiny new code.

It's gems, all the way down

After this had been bugging me for a while, I decided to do something about it. Initially I reengineered a bash script that we already used with our web-based projects for continuous deployment, which applies the CURRENT tag.

This runs after a successful test run on the master branch, gets the current version of the code, and pushes a tag with that version. Travis then runs the tests for a second time against the tagged code and deploys to Rubygems.

export GEM_VERSION=`ruby -e "puts Gem::Specification.load(Dir['*.gemspec'].first).version.to_s"`
'[ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && curl \
-v -X POST -d ''{"ref":"refs/tags/''$GEM_VERSION''","sha":"''$TRAVIS_COMMIT''"}'' --header \
"Content-Type:application/json" -u $GITHUB_USER:$GITHUB_PASSWORD "https://api.github.com/repos/$TRAVIS_REPO_SLUG/git/refs"'

That worked surprisingly well, and we have been happily using it with CSVLint for a few months now. But, again, I wanted more.

I'd been using a tool called Github Changelog Generator for a few months, which uses your Github history to create a changelog, showing what code has changed, as well as what issues and pull requests have been closed since the last version of the code. You can see some example output here. I wondered if I could combine what I'd written with Github Changelog Generator to automatically create a tag, publish the gem and create a changelog, making pushing a new version of a gem as easy as deploying new code to a webserver. So I gave it a shot, and here's the outcome.

'[ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && \
gem install github_changelog_generator && github_changelog_generator && git config user.name "Travis CI" && \
git config user.email "[email protected]" && git checkout master && git add CHANGELOG.md && \
git commit -m "Updated Changelog [ci skip]" && git push "https://${CHANGELOG_GITHUB_TOKEN}@github.com/${TRAVIS_REPO_SLUG}" master'

This horror show of a bash script checks if we're on master and the code isn't from a pull request, installs and runs the changelog generator, then pushes the newly generated changelog to master. The commit message is prefixed with [ci skip], which tells Travis not to run the tests, otherwise we'd be in a whole heap of trouble with infinite looping test runs.

This worked reasonably well, but I still wasn't 100% happy. My bash-fu is pretty weak, and I wasn't entirely sure that copying and pasting bash scripts from one project to another was an entirely smooth development flow, so, like in many of these cases, I decided to write a gem.

Enter Henry

The result was Henry, initially named after Henry Ford, the inventor of the production line, but I'm actually happier with using a Henry vacuum cleaner as a mascot, because who doesn't love a smiling anthropomorphic household appliance?

Now, if you want to continuously deploy your own Ruby gems, rather than copying and pasting some crufty, error-prone bash script, you can run gem install henry inside your project, then run henry bootstrap, and Henry will do the rest!

It's still very early days for Henry, and I'm sure there are some rough edges that could be ironed out, but I'm pretty happy with the overall approach. It lowers the barriers to publishing open source software, and will (hopefully) make some people's lives easier. So if you're a Ruby developer, please give it a go, and if anything breaks – or if you have an idea for any improvements – please open an issue or a pull request.

Stuart Harrison is Web Developer at the ODI. Follow @pezholio on Twitter.

If you have ideas or experience in open data that you'd like to share, pitch us a blog or tweet us at @ODIHQ.