Home » Nodejs » Deploying Node.js and Node.js application to Raspberry Pi

Deploying Node.js and Node.js application to Raspberry Pi

Posted by: admin November 29, 2017 Leave a comment

Questions:

I have a Node.js application that I want to run on a Raspberry Pi.

And, I’d like to be able to deploy new version of my application as well as new versions of Node.js to that Raspberry Pi remotely.

Basically, something such as:

$ pi-update 192.168.0.37 [email protected]
$ pi-update 192.168.0.37 [email protected]

I don’t have any preferences on how to transfer my app to the Pi, may it be pushing or pulling. I don’t care (although I should add that the code for the application is available from a private GitHub repository).

Additionally, once Node.js and / or my app were deployed, I want the potentially running Node.js app to restart.

How could I do this? Which software should I look into? Is this something that can easily be done using tools from Raspbian, or should I look for 3rd party software (devops tools, such as Chef & co.), or …?

Any help is greatly appreciated 🙂

Answers:

a) For running the script continuously, you can use tools like forever or pm2, otherwise you can also make the app a debian daemon on raspian you can run with sudo <servicename> start (if you’re running Arch Linux, this is handled differently I guess).

b) If your Raspberry is reachable from the internet, you can use a GitHub hook (API Documentation) to run every time you push a change to your repository. This hook is basically a URL endpoint on your Pi that runs a little shell script locally.

This script should shutdown you app gracefully, do a git pull for your repository and start the app/service again. You could also trigger this shell script over SSH from your local machine, e.g. ssh [email protected] /path/to/your/script

A update script could look like this:

# change the 'service' command to your script runner of choice
service <yourapp> stop
cd /path/to/your/app
git pull
service <yourapp> start

c) The problem with remote updating Node itself is, that the official binary builds for Raspberry Pi appear only very irregularly, otherwise it would be easy to just download/update the binaries with wget or curl. So most of the time you either need to cross compile Node on your own machine or spend about two hours to recompile it on your Pi. If you want to go with the unofficial builds on GitHub, you can install them with curl -# -L https://gist.github.com/raw/3245130/v0.10.17/node-v0.10.17-linux-arm-armv6j-vfp-hard.tar.gz | tar xzvf - --strip-components=1 -C /usr/local but you need to check the file name for every release.

Questions:
Answers:

Look no further than resin.io All you need to is flush your rpi with their image and then git push your project. resin.io will compile its code and dependencies for your device’s architecture and send the result to your device(s) (in a docker file).

Questions:
Answers:

You can create a very simple continuous integration scheme using supervisor, which does two things:

  • keeps your process running even if it fails,
  • and restarts your process if any of the files changes.

It becomes a simple issue to keep your app updated: you just have to run the commands git pull; npm install: when code is downloaded (or even node modules change) supervisor will will restart the app automatically for you.

If the Raspberry Pi is visible from the internet you can use a GitHub webhook, pointing it to a very simple page that runs the commands git pull; npm install using child_process.exec(). (One important note: use a non-trivial URL (with a code or something) so that people don’t run into it by mistake.) Otherwise just run those commands from the crontab every hour or so, for instance.

As for updating node.js itself, I would use the official Debian package, either from testing or getting it from unstable. Otherwise you would have to create a private repo to host your own packages, which probably is not worth the hassle; but is doable.