ACA Blog

ACA Blog

January 2019
« Dec    


Jenkins build failure notification with a Raspberry Pi

Robin Van PraetRobin Van Praet

As a software development team we use Jenkins as a continuous integration server. This server notifies every team member by e-mail when one of the builds of the project fails. An e-mail notification is easily overlooked and/or disregarded and therefore we were looking for a more visual build failure notification.

The requirements for this were:

After a short brainstorm and some further investigation we decided to go with a Raspberry Pi with additional PiFace controller board and a light beacon that should light up when the build fails. This provided us with a network attached, programmable device, combined with the ease of controlling a beacon by just switching the relay on the PiFace board.

Which resulted in the following setup:

DSC09860 copy

I you are interested in building a build failure notification device like us, these are the steps to take.

Setting up the Raspberry Pi

When unpacking your Raspberry Pi you need to set it up first. There is plenty of information out there on how to do this. We installed Raspbian Wheezy on the SD-card and configured our Raspberry Pi with SSH enabled and a fixed IP address.

Out of the box, the Raspberry Pi is not able to communicate with the PiFace because the SPI driver is not enabled by default. To enable the SPI driver you need to edit the /etc/modprobe.d/raspi-blacklist.conf file with your favourite editor. After this, you need to reboot the Pi.

Installing Node.js

To control the PiFace a number of libraries are available for different languages like Perl, Java, … We choose to use Node.js. To install Node.js (and npm) on the Raspberry Pi, follow these steps as described here.

To control the PiFace we need to install the PiFace node c libraries as well.

Write the Node.js scripts

When you completed the above steps we are ready to start some Node.js programming. Create a working directory and install the PiFace node module to use the PiFace node c libraries in Node.js.

The first thing we did was getting the Raspberry Pi to control the relay and as such make the beacon light up. For this we created the policelightmanager.js file.

On the first line, we set the output port number. The beacon is connected to relay K0 which in turn is connected to output port number 0.

Next we create an instance of the piface-node module. You need to call init() on this module to set everything up for communication between Raspberry Pi and PiFace. When you have no need for the PiFace anymore, it is best to clean up by calling deinit(). We will be running this code continuously and therefore will not call the deinit-method.

Starting on line 4 we create a module that exports two simple functions that will turn on/off the beacon by writing a 1/0 to the output port.

Now let’s use this new library.

When you run this code by calling node buildmachine.js the beacon should turn on for 5 seconds and then turn off again.

The next thing we need is to be able to control the beacon by simple REST calls to the Raspberry Pi. There is a great node module available to create REST endpoints, called express. Let’s use it to create two simple REST endpoints. First we need to install the express module.

Now we can edit the buildmachine.js file to create two REST endpoints.

We instantiate the express module and add the two REST endpoints to turn the beacon on and off. Start the node script again and you should be able to turn the light on and off with a browser by surfing to the appropriate urls. For example http://your-raspberry-pi:3000/lamp/on and http://your-raspberry-pi:3000/lamp/off.

What about Jenkins?

Now for the final part: consuming notifications from Jenkins. To accomplish this we installed the notification plugin in Jenkins. This will allow us to let Jenkins call a specific REST endpoint with information about the build that was started, stopped, failed, … On every project configure the plugin to sent JSON data over HTTP to, for example, http://your-raspberry-pi:3000/buildnotification. And we need to update our script to make this REST endpoint available.

On line 4, we configured express to parse the request body which will contain JSON formatted information. We added an endpoint at line 6 which will log the request body and will turn the light on for 5 seconds when the build status equals UNSTABLE or FAILURE.

Run the script and watch the magic happen. (The & is added to make the script run on the background)

We challenge you to modify the script to your needs. Does the light have to be on until all builds are stable again? Does it have to be on for an amount of time for every failing build? The possibilities are endless…

Of course we hope that your build failure notification device has nothing to notify you about!

Leave a Reply

Notify of