Automating GitHub tasks with Node-RED
Tags: javascript, nodered, devops, productivity
Published: 2018-12-23 22:01:38

---
title: Automating GitHub tasks with Node-RED
published: true
description: How to build easy-to-use workflows on Node-RED to automate GitHub tasks
tags: javascript, node-red, devops, productivity
---

## Some background

While majority of the [Node-RED](https://nodered.org/) community seems to be interested in home automation (me too, but just a little bit and I honestly think it's light-years ahead of other home automation solutions), I focus mostly on automating my ops tasks as a developer/engineer such as updating cluster resources, for that I wrote [Keel](https://github.com/keel-hq/keel), updating CLI clients through continuous delivery, manifests. 

[Homebrew](https://brew.sh/) is a package manager for MacOS where anyone can create their application manifests for an easy distribution and installation. However, there are multiple ways brew formulas can work, such as compiling during installation or downloading a binary. My method of installation is downloading a pre-compiled binary but I had to always remember to update the formula with the latest sha256 https://github.com/webhookrelay/homebrew-tap/blob/master/relay.rb#L5. If a newer version would be released without an update to this manifest, installation via homebrew would fail :) Well, and there were times when I forgot that :D 

## Disclaimer

This flow is not directly reusable for you as it's working around my infrastructure. The purpose is to showcase some template generation, webhookrelay node and I also managed to find a problem in crypto node (more about it later). 

## Stack

Node-RED on RPI with [ node-red-contrib-webhookrelay](https://flows.nodered.org/node/node-red-contrib-webhookrelay) so I don't need to expose it to the internet, [GitHub account + node](https://flows.nodered.org/node/node-red-contrib-github) and Slack (that's really optional, but helpful as I push lots of updates to my Slack channel).

## The flow

The actual flow looks like this:

![node-red-brew-update](https://thepracticaldev.s3.amazonaws.com/i/e7bcuace8q410xbviw08.png) 

I have uploaded flow here: https://gist.github.com/rusenask/93ddb94479fbd6f1e3dcf3308da4ec1b.

1. Initially, workflow is triggered by a build job that's being executed with [Google Cloud Builder](https://cloud.google.com/cloud-build/docs/) and a webhook is dispatched through [this little application](https://github.com/webhookrelay/webhook-dispatcher)
2. Flow is triggered by a webhook to webhookrelay.com endpoint which is routed through a tunnel to a [node here](https://flows.nodered.org/node/node-red-contrib-webhookrelay) which are then translated into Node-RED events. 
3. Flow then downloads the binary and saves to disk.
4. `sha256` custom function node is actually required due to a problem that I found with generating it via `node-red-contrib-crypto-js`. The problem I suspect was due to the way data is being fed into the crypto node (not in one go but with streams). Even setting it to 'one bytes buffer' it would still present a wrong sha. So, I had to:

In settings.js enable `require` as we will need additional crypto libs:

```json
    functionGlobalContext: {
        require: require
    },
``` 

And then read the file and calculate digest:

```js
var require = global.get('require');
var crypto = require('crypto');
var fs = require('fs');

var algo = 'sha256';
var shasum = crypto.createHash(algo);

var file = '/tmp/relay-darwin-amd64-nr';
var s = fs.ReadStream(file);

s.on('data', function(d) { shasum.update(d); });
s.on('end', function() {
    var sum = shasum.digest('hex');
    node.send({payload: sum});
    
});

return;
```

5. Once we have the sha256 sum, we create two templates: one for GitHub brew formula template and one for Slack.
6. For Github we just do a simple overwrite since the file is quite small and only the sha sum ever changes.
7. As for Slack, I chose just to do a simple HTTP request with their incoming webhook integration as the available Slack node seemed a bit too heavyweight for such a tiny requirement. Have a look at the payload that needs to be sent:

```json
{
    "response_type": "in_channel",
    "text": "Brew formula SHA updated to: {{payload}}"
}
```

You could expand it to add pictures, buttons, attachments and so on, but in this case I just needed to see a notification. 

## To sum up

So far I am really happy with Node-RED. Even though it would pretty pretty straightforward to just write everything in Go/JS/Python, for some reason it's more fun in Node-RED :) I will be automating more and more side project tasks with it. Also, `function` node is a life saver!