A quick Travis CI (Continuous Integration) Tutorial for Node.js developers
![Toilet Roll Blocks Seat FAIL](https://raw.github.com/nelsonic/learn-travis/master/images/Roll-Blocks-Toilet-Seat.jpg "Toilet Roll Blocks Seat from Closing. Fail!"")
test early, test often to spot "integration issues" before its too late ...
Continuous integration is a software development process in which all development work is integrated at a predefined time or event and the resulting work is automatically tested and built. The idea is that development errors are identified very early in the process.
If you are completely new to Continuous Integration (CI)
I recommend reading the
CI Wikipedia Article
and Martin Fowler's Article on CI.
Note: Both of these are quite text-heavy but contain all the info you need. Read them! If you have any questions, ask!
The key advantages of Travis:
- Nothing to Install (Cloud Based ... Not Java!)
- Free Both to Use and Open Source (MIT License) see: http://about.travis-ci.org/
- Integrates nicely with GitHub (without any developer effort!)
I've used Jenkins/Hudson CI in the past @groupon, but found the learning curve a bit steep for new developers. Travis by contrast has a much shallower learning curve!
Following the Travis Getting Started guide:
Visit: https://travis-ci.org/ and click "Sign in with GitHub" no "registration" required.
You will be re-directed to GitHub where you need to click "Authorize application"
Note: If you ever want to stop Travis accessing to your GitHub account, simply visit: https://github.com/settings/applications and click on Revoke.
Once you have allowed access you will be taken back to Travis where you will need to enable a specific Git Repository. You can also do this in your Travis Profile: https://travis-ci.org/profile
Now back in your text editor create a few new files:
vi .travis.yml
node_js:
- 0.10
.travis.yml is a basic Travis configuration file that tells travis-ci our application runs on node.js and we want them to test it using a specific version of node. (the file needs to be in the root of your git repository)
In your package.json file, define the test you want Travis-CI to run:
vi package.json
{
"name": "learn-travis",
"description": "Simple nodejs, travis and grunt demo",
"author": "your name here :-)",
"version": "0.0.1",
"devDependencies": {
"jshint": "^2.6.0"
},
"scripts": {
"test": "./node_modules/jshint/bin/jshint hello.js"
}
}
The package.json file is a standard node.js package file with one extra element on the end, the "scripts" property identifies a "test" command:
npm install jshint --save-dev
you can run this command locally by typing npm test
in your terminal
or in our case, we ask Travis to run it on the travis-ci.org servers.
vi hello.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Travis!\n') // this will FAIL travis ci lint
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Commit all the files you just created and push them to github. Travis will automatically scan your repository and pickup the .travis.yml file which informs travis this is a node.js project/app next travis will look for a package.json file and scan for a scripts entry (specifically the test one) Travis will download all the modules listed in your devDependencies and attempt to run your test script npm test
In our case we are only asking travis to lint the hello.js file. and since the file was missing a semi-colon on the 4th line above, it will fail the lint and thus the build process fails!
On line 343 we are missing a semi-colon.
Simply add the simi-colon to the 4th line of hello.js and commit your changes:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Travis!\n'); // build should pass now!
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Todo: create a more realistic test that does something useful. [pull requests welcome!]
- Continuous Integration Wikipedia Article: http://en.wikipedia.org/wiki/Continuous_integration
- Martin Fowler's Article on CI: http://www.martinfowler.com/articles/continuousIntegration.html
- CI Beginners Guide Video: https://vimeo.com/19596466
- Travis Getting Started: http://about.travis-ci.org/docs/user/getting-started/
- Build Podcast Ep.32 (Travis) Video: http://build-podcast.com/travisci/
- @sayanee_'s Build Podcast GitHub: https://github.com/sayanee/build-podcast/tree/master/032-travisci
- Comparison of CI Software: http://en.wikipedia.org/wiki/Comparison_of_continuous_integration_software
- Great Book on CI: http://www.amazon.com/Continuous-Integration-Improving-Software-Reducing/dp/0321336380/
- Jenkins/Hudson CI: http://jenkins-ci.org/
- Lars Vogel Jenkins Tutorial: http://www.vogella.com/articles/Jenkins/article.html
- This is why I avoid Java: http://www.cvedetails.com/vulnerability-list/vendor_id-5/product_id-1526/ by comparison, Node.js: http://www.cvedetails.com/vulnerability-list/vendor_id-12113/product_id-22804/opginf-1/Nodejs-Nodejs.html
- ALL The Diagrams on Google Image Search for Continuous Integration are terrible! https://www.google.com/search?q=continuous+integration&source=lnms&tbm=isch I either need to make time to draw one or ask someone to do one.