我正在尝试在Nodejs中创建一个post-receive挂钩,以便在我的Github repo更新后更新我的服务器。
我之前在php中做过这个。我现在正在使用Nodejs,我不确定应该如何实现它。
我查看了有关在ec2实例上设置nodejs的this博文。它说:
Create a post-recieve hook that will copy over new code after it’s been pushed to the repository
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/home/ubuntu/www
export GIT_WORK_TREE
git checkout -f
$ chmod +x hooks/post-receive
我不确定上面的代码究竟是做什么以及应该如何实现的。
有关最佳方法的任何想法吗?
我正在运行一个基本的32位Amazon Linux EC2实例。
答案 0 :(得分:2)
git bare存储库不包含工作树。所以要使用你必须检查的文件。
将上述脚本放入(伪路径)ec2:mybaregitrepo / hooks / post-recieve将导致每次推送到ec2时都会运行。
这意味着:
#!/bin/sh
//set git working tree (the files you can use) to the path /home/ubuntu/www
GIT_WORK_TREE=/home/ubuntu/www
export GIT_WORK_TREE
//force git checkout so that your files will be put into the working tree.
git checkout -f
比:
//make the post-recieve hook executable so that it can run when you push commits to ec2
chmod +x hooks/post-receive
这是设置远程裸git仓库的一个不错的尝试 http://toroid.org/ams/git-website-howto
答案 1 :(得分:0)
我的node
(Express
)应用在forever
下运行,因此我使用以下处理程序,它基本上执行git pull
并自行杀死(因此重生)。< / p>
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