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.