Deploying to Heroku from TravisCI

· Read in about 2 min · (237 Words)

Travis CI and Heroku are two of my favorite tools. Recently I tried to figure out if there was an easy way to have Travis automatically deploy to Heroku once the tests have passed. Turns out it’s incredibly easy!

All you need to do is add the following as your after_script in your .travis.yml file.

  # Install the Heroku gem (or the Heroku toolbelt)
  - gem install heroku
  # Add your Heroku git repo:
  - git remote add heroku
  # Add your Heroku API key:
  # Turn off warnings about SSH keys:
  - echo "Host" >> ~/.ssh/config
  - echo "   StrictHostKeyChecking no" >> ~/.ssh/config
  - echo "   CheckHostIP no" >> ~/.ssh/config
  - echo "   UserKnownHostsFile=/dev/null" >> ~/.ssh/config
  # Clear your current Heroku SSH keys:
  - heroku keys:clear
  # Add a new SSH key to Heroku
  - yes | heroku keys:add
  # Push to Heroku!
  - yes | git push heroku master

Replace with your own Heroku API key and your Heroku git endpoint and Bob’s your uncle Travis will do the heavy lifting for you and deploy to Heroku for you if, and only if, your tests pass.

One quick final note, I would recommend creating a new Heroku account to use as a «deployer» account because you do have to include your API key in the .travis.yml file.

That’s it! Enjoy your continuous integration and continuous deployment all mixed up.