Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Nodejs中的Github Post接收钩子_Javascript_Node.js_Github_Amazon Ec2 - Fatal编程技术网

Javascript Nodejs中的Github Post接收钩子

Javascript Nodejs中的Github Post接收钩子,javascript,node.js,github,amazon-ec2,Javascript,Node.js,Github,Amazon Ec2,我试图在Nodejs中创建一个post-receive钩子,以便在我的Github repo更新后更新我的服务器 我以前在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-rece

我试图在Nodejs中创建一个post-receive钩子,以便在我的Github repo更新后更新我的服务器

我以前在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裸存储库不包括工作树。因此,要使用这些文件,您必须将其签出

将上述脚本放入(伪路径)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
比:

下面是一个设置远程裸git回购的完整演示 我的
节点
Express
)应用程序在
永久
下运行,因此我使用下面的处理程序,它基本上执行
git拉动
并自杀(因此重生)

现在,只需使用指向此功能的路由:

app.post('/some_path_you_setup_in_github_web_hooks', handleGitHub);
您可以在中设置推钩

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);