Continuous Deployment with git, travis CI, heroku, node.js

4 minute read

A friend recently asked me about details for my Continuous Deployment setup for small projects. I promised to write him some notes about it - here they are.

Define the goal

I have tried to set up a process which allows me to prototype quickly, bring up a new project online with small effort, but still have a sound framework of integration tests, separation of development from staging (or production).

This is meant for small projects which are typically iterated fast by a small team (or just one developer), but coming from a big team development I would not feel well without some supporting structure like source control and tests.

The minimum I want to do is:

  • develop locally, run and test my software locally on my laptop
  • have a source control system which allows to track changes and branch out for trial versions.
  • have some testing framework to avoid completely broken versions to go live
  • have a deploy to a public url for staging and/or production, with a single line command to deploy

Example: Limitless Garden

My example project is Limitless garden - here you can see most of this in action.

Start with github

The starting point is creating a github repository and clone it to your working directory.

$ git clone git@github.com:<your-github-name>/<your-repo-name>.git

In my case the platform is node.js with Express, so I initialize the project with

$ cd <my-repo-name>
$ express -e .
$ npm install

The -eswitch in the express is for the ejs template engine, my personal favourite.

Now you can develop locally and run your local instance with

$ npm start

This will expose your website on http://localhost:3000

Any changes of course are commited as usual with

git add .
git commit 
git push

or (if you use Visual Studio Code like me) with the corresponding UI functions for git interaction.

Deploy to heroku

to deploy this from git to heroku you can just set up a webhook from your github repo to your heroku instance. To do this you should have a heroku account and then open your heroku dashboard and create a new app. It will ask you to choose a name or let heroku generate one for you.

heroku dashboard

Heroku has quite sophisticated setup for deployment pipelines, and there is a free version, but it is limited to run a few hours a day. Good enough for staging, but if you want something always on they will ask you to pay. Lets go for free first.

After you selected your instance name, heroku will ask you to specify some options. To deploy from your github repo you check teh options below:

heroku deploy from github

Now you should have heroku site which is always updated from your github repo.

You make a change in your code, commit and push the change to the github server, and a few moments later you see this new application in your heroku dashboard and can check the details:

heroku deploy succeeded

You can click on the “open app” link and see your application running.

Testing and Continous Integration

You can add tests to your solution ( a bit out of scope here, as there are so many frameworks) and run tests manually on your local system - necessary, but a bit tedious, just type

$ npm test 

before you decide to comit any changes.

Automation is possible in two ways:

Option 1: git hook

put your test script in a git commit hook, for our node.js application this can be done with the node package “pre-commit”

$ npm install pre-commit --save-dev

and then defining the “test” script as a pre-commit hook in your package.json:

{
  "name": "limitless-garden",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "jshint": "jshint **.js",
    "test": "./node_modules/.bin/mocha -u tdd -R spec",
  },
  "dependencies": {
[...]
  },
  "pre-commit": [
    "jshint",
    "test"
  ],
  "devDependencies": {
    "expect.js": "^0.3.1",
    "jshint": "^2.8.0",
    "mocha": "^2.3.2",
    "pre-commit": "^1.1.2",
[...]
  }
}

Option 2: travis.CI

The guys at travis-ci.org provide a fabulous Continous Integration service, and it is free if you can live with their limits (only public repositories, only one thread per test run,..) - for me this works fine.

You should sign in with your github account and Travis will utomatically pull the list of your public repositories and there are just two steps:

  • enable the repo with the button (image below)

travis dashboard

  • add a “.travis.yml” file to your repo to tell Travis what to do. Mine looks like this:
sudo: required
dist: precise
language: node_js
node_js:
  - "4"
services:
  - mongodb
env:
  - CXX=g++-4.8
addons:
  apt:
    sources:
      - ubuntu-toolchain-r-test
    packages:
      - g++-4.8

Now that we have a CI process we can tell heroku to only deploy on successful CI test:

travis dashboard

Conclusion

Voilá, that’s all. Almost. You will probably want to add some services like databases to this setup, and have different database instances for development, test, production?

Wait for the next post on this which should be coming soon.

Meanwhile, you could let me know what you think in the comments below, or enjoy some of my videos

Leave a Comment