Githubレポが更新されたときに、Nodejsでポスト受信フックを作成してサーバーを更新しようとしています。
私はこれを前にPHPで行っています。私は今Nodejsを使用していて、どのように達成されるべきかわかりません。
私はこののブログを見てきましたec2インスタンスにnodejsを設定する方法についての記事を投稿してください。それは言う:
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インスタンスを実行しています。
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
here is a decent runthrough of setting up a remote bare git repo http://toroid.org/ams/git-website-howto
私の 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);
You can setup your push hook in https://github.com/your_username/your_project/settings/hooks