9

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.

Ingwie Phoenix
  • 2,703
  • 2
  • 24
  • 33
  • I think gitlab's web hooks would be a great fit for this. There are for example the gitlabhook module that could listen for the hooks, but I've had some problems implementing this myself. I made a really hacky thing myself, but would be really interested in a more proper solution. – Henrik Karlsson Sep 03 '14 at 22:51
  • This seems like an interesting thing to do, but does it really look like a nail? I mean, do you have use the app to self-update? There are many proven and well tested continuous deployment tools out there, that there's no need to increase complexity of your Node app. – Zlatko Sep 05 '14 at 13:10
  • @Zlatko: I have seen some of the tools, but never understood how they worked in general. If I saw one, it talked about automatically releasing new versions etc...it confused me. So I sought for a solutiont hat sounds more like what I need - code that can update itself and restart itself too. Though, if you have a good software to recomend, Ill look into it for sure. – Ingwie Phoenix Sep 05 '14 at 18:03

1 Answers1

2

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.

Ingwie Phoenix
  • 2,703
  • 2
  • 24
  • 33