1.0.0 • Published 9 years ago

learn-travis v1.0.0

Weekly downloads
6
License
-
Repository
github
Last release
9 years ago

Learn Travis Build Status Dependencies

Our quick guide to Travis CI (Continuous Integration) for complete beginners

Toilet Roll Blocks Seat 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!

Getting Started

Following the Travis Getting Started guide:

Visit: https://travis-ci.org/ and click "Sign in with GitHub" no "registration" required.

Travis Login with GitHub

You will be re-directed to GitHub where you need to click "Authorize application"

Authorize Travis at GitHub

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

Enable Repo in Travis

Create The Project Files

Now back in your text editor create a few new files:

vi .travis.yml
language: node_js
node_js:
  - 0.12

.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)

Define The Test

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/');

Watch it Fail

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!

Travis Build Failing

Travis Build Failing Error Message

On line 343 we are missing a semi-colon.

Correct Code To Pass Build

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/');

Build Status

Travis Build Passing


Todo: create a more realistic test that does something useful. pull requests welcome!


Notes:

General CI Background Reading

Travis Specific

Further Reading

Future