Notifications of CI build results with Slack, Raspberry Pi and Nodejs.
We configure our CI to send a notification to one channel of our Slack with a Slack App Webhook, when the channel receives the build result (success or failure), a Slack Custom Bot checks that the message comes from the CI and the result, now with the result, the Raspberry turn on a Red Led if it was failure and the Green Led if it was successful, also reproduce two different songs with tones from buzzer accordingly to the result received.
For Slack configuration you'll need to create an app on slack's api site:
After create it, add an Incoming Webhook, the option is on the left sidebar on Features. Turn On Activate Incoming Webhooks and some new information will be displayed, they'll give you a sample curl request after you add a new webhook to your workspace making click on the button there and selecting the channel that the webhook will be talking to.
So now you will have to use this webhook on your CI Configuration.
We use Drone-CI, so for this we used the slack-plugin that they offer and it is pretty simple.
As you can see on the examples of how plugin works, it needs the Webhook URL to work and the configuration is very very simple. Of course, we used the webhook url as a secret like one of the examples.
Most of CI Systems offer one notification plugin or like drone, one slack notifications plugin ready to implement, so the only thing you need from your CI is the notificiation to a Slack Channel.
The next step is to create the bot that will read the notifications on the channel.
You can create a new bot user from this page and read some about them to know their features:
After creating it you'll have an API Token, that will be used by the library we'll use to program the bot.
For the installation process I'll suppose you already have Raspbian running on your Raspberry Pi.
You'll need node and npm on your Raspberry, run this commands
sudo apt-get install nodejs-legacy
sudo apt-get install npm
- Raspberry Pi
- Resistance 100 Ω
- Resistance 330 Ω
- Piezo (Buzzer)
- Male-Female Jumpers
- Male-Male Jumpers
- Red Led
- Green Led
First, make a new folder, enter to it and run npm init
this will create the package.json.
We are going to need this libraries:
Slack-Node-Client This library is used to manage the Slack Api with node and build our bot.
Johnny-Five This library is used for controlling the GPIOs, it makes pretty easy to control them, the documentation is great and comes with examples. To install it run
npm install johnny-five
Raspi-io This is an IO Plugin that provides support for the Raspberry Pi. To install it run
npm install raspi-io
j5-songs This library is optional, we add it to play a little with a buzzer we find while doing this and implement it for fun. To install it run
npm install j5-songs
After installing the dependencies we are ready to code the bot.
In our index.js file you can find the code with the explanation of what are we doing in the comments.
After running the index.js file you are ready to go, push something to your CI and wait for the build to finish and watch the magic!
This project can be bigger, it is extremely scalable because the possibility of adding a lot of new features thanks to the extensive different modules that are available on the market for this boards, the different functionalities of Slack Bots, etc... Sky's the limit for this project! :D
We made this project like this with little Leds just for testing, our real plan is to use bigger lights to mount them on the wall.
- Raspberry Pi 2 Model B v1.1
If you tested it and worked on another Raspberry Pi please make an issue to tell me to add it.