我node
(Express
)的應用程序下運行forever
,所以我使用下面的處理程序,它基本上做了一個git pull
並殺死自己(因此重生)。
function handleGitHub(req, res, next) {
setTimeout(function() {
var shouldRestart = !app.config.webhook || req.body.head_commit.message.indexOf(app.config.webhook) > -1;
console.log('[GITHUB] WebHook Received for "%s" (WebHook: %s, Restart? %s)', req.body && req.body.head_commit.message, app.config.webhook && app.config.webhook, shouldRestart);
if (!shouldRestart) {
console.log('[GITHUB] Not restarting');
return;
}
if (app.server_https && app.server_https.close) {
console.log('[GITHUB] Closing HTTPS');
app.server_https.close();
}
if (app.server && app.server.close) {
console.log('[GITHUB] Closing HTTP');
app.server.close();
}
setTimeout(function() {
var spawn = require('child_process').spawn;
var shell = process.env.SHELL;
var args = ['-c', 'git pull'];
var path = require('path');
var projectPath = '/path/to/your/project/folder'; //or path.join(__dirname, '....
var opts = { cwd: projectPath };
console.log('[GITHUB] Spawning...', opts);
var spawnProcess = spawn(shell, args, opts);
spawnProcess.stdout.on('data', function(data) {
// could log these
});
spawnProcess.stderr.on('data', function(data) {
// could log these
});
spawnProcess.on('close', function(exitCode) {
console.log('[GITHUB] Spawn finished', exitCode);
console.log('[GITHUB] Exiting...');
console.log('-------------------------------------------------------');
process.exit(0);
});
}, 1000);
}, 500);
res.send(200, 'OK');
}
現在,只需要使用一個指向此功能的路線:
app.post('/some_path_you_setup_in_github_web_hooks', handleGitHub);
您可以設置在 https://github.com/your_username/your_project/settings/hooks
你推鉤