How To Setup CI Build Pipeline With Travis CI, Heroku and sbt

This post covers all steps that are required to setup a Continuous Integration (CI) build pipeline using Travis CI as a main driver for deploying our Play application written in Scala to Heroku cloud.

Expected end result:

After each commit to the master branch of my Github project, I’d like to run full test suite. Following that, each successful build, should trigger a deployment to Heroku. If the tests fail, application should not be deployed.

I’ll use my own project as an example, but this tutorial is suitable for any other project that is uses sbt as a built as well. I’ll be covering following aspects of the work that needs to be performed:

  1. Deploy Play application to Heroku
  2. Integrate Travis CI with Play app
  3. Setup Travis to deploy to Heroku

Deploy Play application to Heroku

Deploying a Play application (build with Activator or sbt) can be done very simply with the help of sbt-heroku plugin.

Those are the steps I had to perform:

  1. Install heroku toolbelt and login with your heroku account
  2. Run heroku create which will generate new application name for you
  3. Add the sbt-heroku plugin – setup application name from the previous step
  4. Run sbt stage deployHeroku
  5. The application will be deployed in around 1 or 2 minutes
  6. If you go to your app activity log (in my case here: https://dashboard.heroku.com/apps/warm-hamlet-57324/activity) you should see all actions that were performed, including your latest deployment

Integrate Travis CI with Play app

This step is even simpler, it’s enough to login to Travis CI with your Github account, then from the list of discovered applications select the one you are interested in.

Travis CI will do everything else automatically and to get started that’s enough. Travis will get notified by Github on each commit you make, and will run your tests for you.

To allow for more customization and control over the build and test process it’s recommend to add .travis.yml configuration file. This is the simple one to get started.


language: scala

jdk:
  - oraclejdk8

scala:
  - 2.11.8

cache:
  directories:
    - $HOME/.m2/repository
    - $HOME/.sbt
    - $HOME/.ivy2

Setup Travis to deploy to Heroku

Next step is the combination of the work we did so far.

To get started you need your Heroku API key which can be found on your account page.

This API key needs to be configured on Travis CI project configuration page, in my case it is: https://travis-ci.org/wlk/game-arena/settings, you need to setup a new environment variable called HEROKU_API_KEY and set the API key as a value.

Additionally we need to update .travis.yml file to describe deployment steps:

language: scala

jdk:
  - oraclejdk8

scala:
  - 2.11.8

cache:
  directories:
    - $HOME/.m2/repository
    - $HOME/.sbt
    - $HOME/.ivy2

deploy:
  provider: script
  script: sbt stage deployHeroku

As you can see I have decided to configure Travis to run my own deployment script which is sbt stage deployHeroku. It’s exactly the same one I have used when deploying from localhost (this time the Heroku API key is not read from heroku toolbelt, but from the environment variable we configured one step above).

Note: Travis comes with build in Heroku deployment capabilities, but I decided no to use them, because I wanted to be able to reuse the same deployment code for both automated and manual deployments.

Summary

As you can see, setting up a simple CI build pipeline is quite a straightforward thing to do, after that the whole process of testing and deploying will happen automatically, and new version of your app can be live within few minutes after your last commit.

I have been using Travis CI for all my Github projects with good results, but this is the first time I have been deploying application automatically to Heroku, so there is still much more to learn how to do this effectively.

BTW. Did you know that I’m available for hire?