2011-10-27 14 views
1

Githubリポジトリが更新されたときにNodejsで受信後のフックを作成してサーバーを更新しようとしています。NodejsのGithub受信後フック

私はこれをPHPで前に行っています。私は今Nodejsを使用していて、どのように達成されるべきかわかりません。

ec2インスタンスにnodejsを設定する方法については、thisブログ記事を参照してください。それは言う:

私は正確にどのように上記のコードがやっているか、実装する必要があるのか​​分からない。

これを行う最善の方法についてのご意見はありますか?

私は基本的な32ビットのAmazon Linux EC2インスタンスを実行しています。

答えて

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 

マイnodeExpress)アプリは下に走る http://toroid.org/ams/git-website-howto

0

リモート裸のgitリポジトリを設定するためのまともなrunthroughですforever、私は基本的にを行う次のハンドラを使用しますそれ自身を殺す(したがって再創造する)。今

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

であなたのプッシュフック
関連する問題