2011-10-27 3 views
1

Github 레포가 업데이트되었을 때 Nodejs에서 포스트 수신 훅을 만들어 서버를 업데이트하려고합니다.노드 내 Github 포스트 수신 훅

나는 전에 PHP에서 이것을 해왔다. 지금 Nodejs를 사용하고 있는데 어떻게해야하는지 확신 할 수 없습니다.

나는 ec2 인스턴스에 nodejs를 설정하는 것에 관해 this 블로그 게시물을 보았습니다. 그것은 :

위의 코드가 무엇을하고 있으며 어떻게 구현되어야하는지 확신 할 수 없습니다.

이 작업을 수행하는 가장 좋은 방법에 대한 아이디어가 있습니까?

기본 32 비트 Amazon Linux EC2 인스턴스를 실행하고 있습니다.

답변

2

git bare repository에는 작업 트리가 포함되어 있지 않습니다. 따라서 파일을 사용하려면 체크 아웃해야합니다.

위 스크립트를 (의사 경로) 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 

http://toroid.org/ams/git-website-howto

0

node (Express) 응용 프로그램에서 실행되는 원격 베어 자식의 repo를 설정 괜찮은 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

에 푸시 훅
관련 문제