Set up Jenkins for Node.js

Jenkis is a great ci server with many options and plugins.
With a bit of time and configuration we can use Jenkins for our Node.js applications.

First of all the Node.js plugin on Jenkins enables us to run node.js scripts as a build step.

Screen Shot 2015-06-20 at 3.10.17 PM

In order to proceeed executing various node.js tasks we can use shell build step.

The next step is to install packages using npm

Screen Shot 2015-06-20 at 3.16.59 PM

The next problem is how to keep our application up and running.

To achieve this we can use forever.

Just add the latest version as a dependency inside your package.json

"dependencies": {
"forever": "0.14.1"

Then in the same build step we should stop any already running applications.

./node_modules/forever/bin/forever stopall

And by calling forever with BUILD_ID=dontKillMe in front of it we will ensure that Jenkins would not kill our node.js process running, which is most probably an application listening to a port.

BUILD_ID=dontKillMe ./node_modules/forever/bin/forever start bin/www


Screen Shot 2015-06-20 at 3.23.46 PM

For unit testing I prefer the mocha framework.
xunit-file is a great plugin that will transform the results of mocha into a JUnit result file. Therefore we will add the mocha and xunit-file dependencies.

"dependencies": {
"mocha": "~2.2.4",

As a build step we should setup

./node_modules/.bin/mocha --recursive --reporter xunit-file


Screen Shot 2015-06-20 at 3.31.53 PM 1

The xunit file produced on the workspace would be used from Jenkins in order to produce a JUnit report.

Therefore we should add a post build action to Jenkins that will publish the JUnit results.

Screen Shot 2015-06-20 at 3.33.31 PM

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s