Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NodeJS to self-update project [closed]

I have a project that runs mainly on NodeJS. During startup, it creates a socket.io+http server and a few workers (mainly event listeners that do their job on specific events).

Now I would like to implement a worker process that automatically checks if a new commit is available on a git repo that is hosted via GitLab on the same server.

So I would like to know:

  • Is there a npm module that can look up if a local and remote git repository match in commits?
  • Or would it be more advised to use Redis' Pub/Sub and a git hook to notify the application about an update this way?
  • Or is there one entirely different approach you would recommend?

Currently this project is running as a development server and I might turn off the auto-update function once it is in productive state. But since I develop on my laptop but test on my remote server, an auto-updater would be handy.

like image 732
Ingwie Phoenix Avatar asked Sep 03 '14 22:09

Ingwie Phoenix


People also ask

Does node update automatically?

NodeJS doesn't have any kind of update support, at all, and certainly no auto-update support. You'd have to manually install a new version. You may be using NVM (node version manager), which lets you install multiple versions of node.

How do I restart node server automatically?

Nodemon is a package for handling this restart process automatically when changes occur in the project file. It will show the version of nodemon as shown in the below screenshot. Now, when we make changes to our nodejs application, the server automatically restarts by nodemon as shown in the below screenshot.


1 Answers

After some researching, I did find a solution, that actually seems to work too. Barely documented and a bit buggy - but it should work for the base purpose of a self-updating NodeJS app: http://registry.npmjs.org/gitlabhook

Here is how I coded it (taken directly from my code):

var fs=require("fs");
module.exports = function() {
    // Dynamically write this config.
    var obj = {
        tasks: {
            "*": [
                "cd '"+config.base+"'",
                "git pull",
                "git submodule update",
                "npm install",
                "node lib/updater.js '%m'"
            ],
        }
    }, str = JSON.stringify(obj), glConf = config.base+"/config/gitlabhook.json";

    log.info("BIRD3 Autp updater: Generating config to "+glConf);
    fs.writeFileSync(glConf, str);

    // Set it up
    var gitlabhook = require("gitlabhook"),
        gitlab = gitlabhook({
            host: config.host,
            configFile: "gitlabhook.json",
            configPathes: [ config.base+"/config" ],
            logger: log,
        });

    log.info("BIRD3 Auto updater: Starting");
    gitlab.listen();
    BIRD3.on("update", function(){
        setTimeout(function(){
            log.info("BIRD3 Auto updater: Exiting to allow update.");
            process.exit(2);
        }, 200);
    });
    log.info("BIRD3 Auto Updater -> Online!");
}

To explain:

  • config is a global object, storing app-specific stuff. config.base is equal to the path of the main script.
  • BIRD3 is the app's name.
  • The BIRD3 object is an EventEmitter shared thru the entire application.
  • updater.js sends a message to a redis server - which, by another half of the app, is turned into a proper event.
  • I am generating the .json file, simply because I am about to move servers. In order to keep things dynamic, I decided to work with this aproach.
  • Gitlabhooks needs the config filename and directory supplied separately. Supplying the tasks object during the call will result in the config file not being looked for. The callback receives an object with the following layout: https://gist.github.com/IngwiePhoenix/d08629af01ce93e39e4b

I see much potential in this module, and hope that it can develop further.

However, if you have another solution that work too, share it. I am sure that other people that may read this will find it useful, as it is a neat feature for continuous deployment.

like image 84
Ingwie Phoenix Avatar answered Sep 23 '22 00:09

Ingwie Phoenix