GitLab 5.2 Post-Receive WebHook

sad*_*ave 8 git json webhooks git-post-receive gitlab

我在与生产网络服务器相同的服务器上运行GitLab v5.2(/ var/www中的Document Root).

我正在尝试设置一个标准的GitLab Post-Receive Hook,但我发现很少有关于如何设置脚本来处理发布的JSON数据的信息.我不打算做任何自定义的事情,只是开箱即用,我想在我的网站位置接收后接收数据(请记住在同一台服务器上),然后在收到时从origin-master拉出(提供后接收数据源于推送到主分支).这样,在/ var/www中找到的网站总是与主人相同.

有人可以,从帖子数据中给我一个拉动脚本的例子,或者指向我正确的方向让我创建一个?

GitLab Hook请求示例 - 对于那些没有GitLab实例的人,这里是GitLab Post-Receive JSON数据的样子(直接来自GitLab帮助)

{
 "before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
 "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
 "ref": "refs/heads/master",
 "user_id": 4,
 "user_name": "John Smith",
 "repository": {
   "name": "Diaspora",
   "url": "git@localhost:diaspora.git",
   "description": "",
   "homepage": "http://localhost/diaspora",
 },
 "commits": [
   {
     "id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
     "message": "Update Catalan translation to e38cb41.",
     "timestamp": "2011-12-12T14:27:31+02:00",
     "url": "http://localhost/diaspora/commits/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
     "author": {
       "name": "Jordi Mallach",
       "email": "jordi@softcatala.org",
     }
   },
   // ...
   {
     "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
     "message": "fixed readme",
     "timestamp": "2012-01-03T23:36:29+02:00",
     "url": "http://localhost/diaspora/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
     "author": {
       "name": "GitLab dev user",
       "email": "gitlabdev@dv6700.(none)",
     },
   },
 ],
 "total_commits_count": 4,
};
Run Code Online (Sandbox Code Playgroud)

sad*_*ave 8

好吧,经过大量挖掘后,我找到了足够的文档来创建自己的脚本,这里是:

PHP

 error_reporting(E_ALL);
 ignore_user_abort(true);

 function syscall ($cmd, $cwd) {
    $descriptorspec = array(
            1 => array('pipe', 'w') // stdout is a pipe that the child will write to
    );
    $resource = proc_open($cmd, $descriptorspec, $pipes, $cwd);
    if (is_resource($resource)) {
            $output = stream_get_contents($pipes[1]);
            fclose($pipes[1]);
            proc_close($resource);
            return $output;
    }
 }

 if( $HTTP_RAW_POST_DATA && !empty( $HTTP_RAW_POST_DATA['ref'] ) ){
    // pull from master
    if( preg_match( '(master)', $HTTP_RAW_POST_DATA['ref'] ) )
            $result = syscall('git pull origin master', '/var/www/website/directory');
 }
Run Code Online (Sandbox Code Playgroud)

因此,这完全符合其目的.但现在我需要重新思考逻辑,甚至可能是哲学.此方法将自动使/ var/www/website /目录与master保持同步; 各种其他分支怎么样?我需要一些方法来通过我的Web服务器查看其他分支,因此开发团队可以查看他们的工作......

这是我在想的:

而不是我的代码只是在post字符串的ref部分中查找"master",而是将post字符串拆分为"/"分隔符并弹出end元素:

 $branch = array_pop( split("/", $HTTP_RAW_POST_DATA['ref']) ); //this will return which branch the post data was sent from
Run Code Online (Sandbox Code Playgroud)

然后我检查这个分支是否在/ var/www/website /目录/中有一个工作目录,如/var/www/website/directory/master:

if( is_dir( "/var/www/website/directory/$branch" ) ){ //check if branch dir exists
  $result = syscall("git pull origin $branch", "/var/www/website/directory/$branch");
} else {
  //if branch dir doesn't exist, create it with a clone
  $result = syscall("git clone ssh://git@git.mydomain.com/sadmicrowave/someproject.git $branch", "/var/www/website/directory");
  //change dir to the clone dir, and checkout the branch
  $result = syscall("git checkout $branch", "/var/www/website/directory/$branch");
} 
Run Code Online (Sandbox Code Playgroud)

这个逻辑似乎相对稳固,只是在这里张贴以查看人们的意见.使用此方法,开发人员可以创建新的远程分支,然后推送到该分支,然后分支将被拉入/ var/www web目录以供查看.

任何人都可以想到另一种方式,允许开发人员查看他们的开发分支或任何关于如何使这个脚本更好的建议?

谢谢