Continuous Integration with Travis CI

Perform Integration Testing and Continuous Deployments to Heroku

Continuous Integration with Travis CI

by John Vincent


Posted on April 05, 2017



Configure Github, Travis and Heroku so that a change to a github repository triggers a Travis CI build which executes Mocha/Chai Unit Tests and if successful, deploys to Heroku

Lets get started.

Install Heroku

If Heroku is not installed, see Heroku Notes for details.

Install Travis CLI

If not already installed, install Travis CI command line interface.

Usually, the following will work

sudo gem install travis

but on OSX, fails due to OSX security feature.

May need to install to /usr/local/bin

Update system

sudo gem update -n /usr/local/bin

then install Travis

sudo gem install travis -n /usr/local/bin

To update Travis

sudo gem update travis

or to remove Travis

sudo gem uninstall travis

Join Travis

From Travis

  • Sign-up
    • Authorize, give lots of privileges
    • Travis syncs projects

Travis CI

From Github

* Select {github-project} project
* Settings, Integrations & services (left menu)
* Add a Service (mid-right)
    * Select Travis CI from dropdown
    * Do not add User, Token, Domain
    * Add service (green button at bottom)

From Travis

* click on `username`(top right)
    * Lists github projects
    * Sync Account
    * Activate {github-project}

From Github

* Select {github-project} project
    *  Settings, Integrations & services
        * Travis CI, Edit
            * Notice Travis CI entry in Webhooks

For a Node/Express application, or for a React/Node/Express application, edit .travis.yml

language: node_js
node_js: node
cache:
  directories:
    - node_modules

or, if already done

touch .travis.yml

to force a rebuild.

Git push .travis.yml to master

From Travis CI:

  • Select User-name, Accounts (top-right)
  • Sync Account (top-right)
  • Select {project}
  • Travis builds the project, see Job log
  • When complete, Restart build appears (mid-right)

Set up continuous deployment

Configure Travis to work with Heroku.

  • Push changes to master on GitHub, or merge a pull request into master, our tests will automatically run.
  • If our tests pass, TravisCI will deploy to Heroku. If our tests do not pass, it will not deploy.

Setup Travis to Deploy to Heroku

Travis login requires my Github login.

cd {project}
travis login

and provide Github username & password

or, already have setup SSH for Github

cd {project}
travis login --auto-password

Deploy to Heroku

travis setup heroku

“return” to all the questions.

To see the differences

git diff

Create Heroku Application

heroku create {heroku-project}

Notice:

* App name: {heroku-project}
* https://{heroku-project}.herokuapp.com/
* https://git.heroku.com/{heroku-project}.git

Edit .travis.yml

deploy:
    app: {heroku-project}

Git commit changes to master

Test Heroku App

To run the app on Heroku:

https://{heroku-project}.herokuapp.com/

Test CI is working

To test the continuous integration

  • Change or touch any file
  • git commit to master
  • verify Travis CI rebuilds the project.

Other

git remote -v

shows Github and Heroku repositories.

Heroku Remote branch

If Heroku Remote Repository is wrong, for example:

git remote -v

heroku	https://git.heroku.com/{bad-project}.git

Let’s fix that:

git remote rm heroku
git remote add heroku https://git.heroku.com/{heroku-project}.git

MongoDB

If require MongoDB at Heroku, must add the following to .travis.yml

services:
- mongodb

Travis Build Error

invalid option "--api_key="

is caused by bad API key.

The solution is to build a new key.

  • Heroku
  • Navigate to your Heroku account
  • API Key section
  • Copy API key
  • Go to project root directory
  • Run the following
travis encrypt <api-key> -r <github-user>/<repo-name> --add deploy.api_key

Notice that .travis.yml is updated

 api_key:
    secure:

secure: has a new value.

Git submit to master and Travis will rebuild.