Skip to content

Instantly share code, notes, and snippets.

@nkt
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkt/6d7106f30c6614e42d9f to your computer and use it in GitHub Desktop.
Save nkt/6d7106f30c6614e42d9f to your computer and use it in GitHub Desktop.
Simple standalone deploy server written on Node.js
var childProcess = require('child_process');
var http = require('http');
var util = require('util');
var path = require('path');
var fs = require('fs');
var listen = process.env.LISTEN || 8000;
var home = process.env.HOME;
var key = process.env.DEPLOY_KEY;
if (!key) {
var crypto = require('crypto');
var random = crypto.randomBytes(128);
key = crypto.createHash('sha1').update(random).digest('hex');
console.info('Generated key: %s', key);
}
var server = http.createServer(function(req, res) {
if (req.url.indexOf('key=' + key) === -1) {
res.writeHead(403);
res.end();
return;
}
var _path = path.join(home, req.url.replace(/\?.+/, '').replace('/deploy', ''));
var _gitPath = path.join(_path, '.git');
fs.exists(_gitPath, function(exists) {
if (!exists) {
res.writeHead(404);
res.end(JSON.stringify({
error: 'Deploy target not exists!'
}));
return;
}
childProcess.exec('git pull', {
cwd: _path,
timeout: 60 * 1000,
encoding: 'utf-8'
}, function (err, stdout, stderr) {
res.writeHead(err ? 500 : 200);
res.end(JSON.stringify({
error: err,
stdout: stdout,
stderr: stderr
}))
});
});
});
server.listen(listen);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment