Continuously deploy Node apps with GitHub, Travis and Heroku

GitHub, Travis and Heroku make it simple to build a continuous deployment pipeline for Node.js applications. Here is a walkthrough.

Heroku has WebSockets

Back in October Heroku announced that WebSockets are now in public beta. This had been a blocker for many Node.js developers using the platform so was great to see. By all accounts the service has been really good and comes with one of Heroku's best features - it is free to play and experiment. This is particularly useful for quick prototypes of hacks days that you want to bring up quickly then potentially trash.

This article will detail how to build a pipeline that will continuously deploy a Node.js application to Heroku via GitHub for source control and Travis CI for Continuous Integration.

For the purpose of this article we'll use a simple Express application created for a walkthrough on creating a basic Express application. It has one test!

GitHub

I'm assuming you know what GitHub is. You'll need an account and a Node.js application as a Git repository hosted on GitHub. You know what to do here.

Travis CI

Travis CI is a Continuous Integration Service that is free for Open Source projects. It supports a range of languages including Node.js and also have offers private Continuous Integration for a fee. It is a first class service and I highly recommend it. To create an account you just sign in with your GitHub account - easy! You'll need to authorise Travis to access your GitHub account but after that you are done.

In the top right hand corner you'll be able to view your GitHub accounts where you can flip the switch for use Travis on a repository.

Travis needs to know about your application so you need to add a new .travis.yml file to the root of your git repository. For Node.js it looks like this.

language: node_js
node_js:
  - "0.10"

Note you can run tests against a range of Node.js versions by specifying the versions you want. By default Travis will run the npm test command so make sure you set this up in your package.json file. If you are using Mocha it looks like this.

"scripts": {
  "test": "./node_modules/mocha/bin/mocha"
}

Travis uses a RubyGem for some functions. RubyGems is Ruby's central repository and Ruby's npm while Gems are modules. You can install the Travis gem with

gem install travis

If you need to install RubyGems there are instructions here.

Deploying to Heroku

To deploy to Heroku you need an account and you also need to install the heroku gem on your system. Once you have logged in create an application. You will also need the Heroku toolbelt. This installs everything you need to interact with the Heroku platform. There are full instructions for installing on all platforms on the site.

Once you've installed the toolbelt open a terminal and login.

heroku login

With heroku and the travis gem install you can now add an encrypted version of your heroku api key to your .travis.yml file

travis encrypt $(heroku auth:token) --add deploy.api_key

This will add some lines to your .travis.yml file. Open it up and you should see something like

deploy:
  api_key:
    secure: FF7yK....

By default Travis will try to deploy to a Heroku application with the same name as your git repository. If you Heroku application is called something different you can edit the .travis.yml file and change it. You also need to add the provider as 'heroku'.

deploy:
  app: express-example
  provider: heroku
  api_key:
    secure: FF7yK....

Commit the file to your git repository and push the file up to GitHub.

Finally for an application to work on Heroku it needs a Procfile. This is so Heroku knows how to start a Node.js application. For the example project you can see this file here

web: node app.js

The build and deployment pipeline

Now everything is set up here is the process flow of the build and deployment pipeline.

Getting fancy with branches

Travis is also aware of branches so if you want to deploy to different Heroku sites (say for staging and production) you can also configure this. To do this update your travis.yml file with the name of the branch and the heroku application to deploy to.

app:
  master: my-app-staging
  production: my-app-production

Conclusion

With very little effort you can create a Continuous Deployment pipeline with GitHub, Travis and Heroku. There are other PaaS providers that support deploying from Heroku including AppFog and Nodejitsu so if you would rather not install the additional dependencies required by Heroku you may wish to look at them. At the time of writing AppFog offer a free service for up to 8 applications and Nodejitsu offer free hosting for Open Source projects.

Continuous Deployment removes the requirement to do any DevOps from you as a developer. It ensures that your application can only be deployed if your tests pass and you can even use branching to create a process for reviewing changes before they are released into the production branch. Deploying your application just becomes a git push. It is so simple why wouldn't you do it?

Resources

Have an update or suggestion for this article? You can edit it here and send me a pull request.