Saturday 11 January 2014

Using jshint with violations plugin on Jenkins

In case you’ve landed here trying to answer the question of how to lint your JS as part of a Jenkins build, then I do hope this post will be of help to you. Before I go any further I will assume that you have the the violations plugin installed and working.

Where I went wrong

I naively assumed that getting jshint to work with Jenkins was a case of installing the jslint or jshint ( I prefer jshint in case you were wondering; I’m a tabs and single quotes kind of guy JS ) and then piping the output from the binary into a textfile. I assumed this because that is more or less how the pep8 integration worked & mdash; I was wrong.

Googling around the subject lead me to this fine post on how to configure the jslint ant task for Jenkins. This was great info, but I didn’t have any real first hands experience of using Ant directly ( I suspected dark Java voodoo ) and I was starting to think I’d end up down a Java rabbit hole — again, I was wrong.

Actually very easy

Install Node, then JShint

The first step is to get the jshint javascript package installed via node package manager ( NPM ). I didn’t have NPM installed so I had to install it. However, there was a problem; the version that ships with Ubuntu 13.10 is old enough to cause problems when trying to install things. Fortunately help was at hand via this thread on how to install latest node on Ubuntu Stack Overflow. I’ve reproduced it here and I direct all credit to Cris-O

# incase you don't have add-apt-repository
sudo apt-get install python-software-properties

# install a external apt source
sudo add-apt-repository ppa:chris-lea/node.js  
sudo apt-get update  

# install npm ( it is part of nodejs) 
sudo apt-get install nodejs

Groovy, now you should have access to npm so you can install JShint, but before you do, switch to your Jenkins user.

# you're certain that you're logged in as Jenkins?
# note: I'm omitting the -g flag (global) because I want JShint installed for Jenkins only
npm install jshint

That should install JShint into ~/node_modules/jshint/ and you should have access to the binary at ~/node_modules/jshint/bin/jshint. You’re on the home straight now.

Call it as part of the build process

Now that JShint is installed it is a matter of calling it as part of the build process and telling it to output into the xml format that the violations plugin expects. Build process

I’m piping everything to true because at thing point in time, I don’t want to fail a build if there is any feedback from the violations plugin. After a few builds you’ll have a enough data for the graph to start plotting –

Happy hinting.