Our quick guide to Travis CI (Continuous Integration) for complete beginners
Testing your work (to be sure its working as expected) is the most important part of a project.
![Toilet Roll Blocks Seat FAIL](https://raw.github.com/dwyl/learn-travis/master/images/Roll-Blocks-Toilet-Seat.jpg "Toilet Roll Blocks Seat from Closing. Fail!"")
CI helps you Test Early, Test Often to spot "integration issues" before its too late ...
Travis CI takes the hassle out of running your own CI so you can focus on your project/product!
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) we 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!
- Nothing to Install (Travis Web-Based ... Not a heavy Java Application you have to host yourself1)
- Free Both to Use and Open Source (MIT License) see: http://about.travis-ci.org/
- Integrates nicely with GitHub (without any developer effort!)
1We've used Jenkins CI in the past for client projects,
but Jenkins has a
steep learning curve
for new developers.
Travis by contrast has a much shallower learning curve!
This tutorial will take you 20 minutes and will save you hours of frustration! #NoBrainer
- Computer with node.js installed
- any text editor
If you don't have these, see: https://github.com/dwyl/start-here
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
Paste the following code:
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 GitHub repository)
Create a package.json file and define the test you want Travis-CI to run:
vi package.json
{
"name": "learn-travis-YOURNAME",
"description": "Simple Travis-CI check for JSHint (Code Linting)",
"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.
To run the test command we will need to install the jshint
node module from NPM:
npm install jshint --save-dev
Now you can run the test
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.
But first lets create a file for jshint
to check:
vi hello.js
Paste (or type) out the following code:
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/');
And just like that our "build" is passing!
@dwyl we use Travis-CI for a lot more than code linting! We use Travis-CI to
automatically run our unit/integration tests.
If you are new to automated testing, we have a
Complete Beginners Test Driven Development Tutorial:
https://github.com/dwyl/learn-tdd
Which will show you how to use Travis-CI to check your code is working as expected!
If you are new to environment variables check out our introductory tutorial (for complete beginners): https://github.com/dwyl/learn-environment-variables/
Often your application will use environment variables to store keys, passwords or other sensitive data you don't want to hard-code in your code; Travis-CI makes this easy:
There are two ways of telling Travis-CI about your environment variables:
The easiest and most explicit way of listing your environment variables
is to add them to your .travis.yml
file:
language: node_js
node_js:
- 0.12
env:
- MY_VAR=EverythignIsAwesome
- NODE_ENV=TEST
The interesting part is the env:
key where you can then list
your environment variables and their corresponding values.
The other way of telling Travis-CI your environment variable(s) is to add them in the web-base user-interface (Web UI) in your project's settings page:
Notice how in if you add your environment variables in the the Travis Web UI
they are hidden (from the build log) by default.
This does not prevent you from accidentally console.log
them and exposing a key/passord.
So take care when console.logging ...!
If you are storing sensitive information (like API Keys or Database Passwords) for use in your node app, the best way is to use the travis ruby gem to encrypt your keys:
You will need to have ruby installed on your computer, if you don't already have this, we recommend installing it with RVM:
\curl -L https://get.rvm.io | bash -s stable --ruby
rvm install current && rvm use current
Once you have installed ruby you can install the travis ruby gem:
gem install travis
With the gem installed, you encrypt your variable by running the command in your terminal (ensure you are in the working directory of your project)
travis encrypt MY_SECRET=super_secret
Type yes
to confirm you are your project, you should now see your encrypted variable:
Paste this in your .travis.yml
file and commit it to GitHub!
๐ก Top tip: if you need to check your .travis.yml
file
is error-free, run the command travis lint
(in the folder where is your travis file)
Can be useful if one needs for instance to encrypt keys in setup with external deployment tools like s3, to simply check the syntax of your .travis.yml by doing a simple "travis lint" or any other advanced operations
The install process on the official page https://github.com/travis-ci/travis.rb#installation lacks a bit of help and details. Even with installing the dev package of ruby you may encounter troubles. But the process seems to work flawlessly with RVM (Ruby version manager), like described above: https://rvm.io/rvm/install
Just run the following commands:
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable --ruby
now add this line at the end of your ~/.bashrc file: source "$HOME/.rvm/scripts/rvm"
source ~/.bashrc
gem install travis
travis --version
- 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
- Travis-CI Environment Variables guide: http://docs.travis-ci.com/user/environment-variables/
- 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 we 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 we either need to make time to draw one or ask/commission someone to do one for us!