GitLab 5.2后置接收WebHook

8

我正在同一台服务器上运行GitLab v5.2和我的生产Web服务器(文档根目录在/var/www)。

我正在尝试设置标准的GitLab Post-Receive Hook,但是发现关于如何设置处理发布JSON数据的脚本的信息非常少。我不想做任何自定义,只是想要在我的网站位置(记住是在同一台服务器上)接收post-receive数据,然后在接收到数据时从origin-master进行pull(前提是post-receive数据起始推送是到主分支)。这样,在/var/www中找到的网站始终与主分支相同。

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

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,
};
2个回答

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

所以,这对于其目的完美地起作用。但现在我需要重新思考逻辑,甚至可能是哲学。这种方法将自动使/var/www/website/directory与主机保持最新状态;然而,其他各种分支怎么办?我需要一些方法来查看我的 Web 服务器中的其他分支,以便开发团队可以查看他们的工作...
我正在考虑以下内容:
与其让我的代码只在帖子字符串的 ref 部分查找 "master" ,我将 post 字符串按 "/" 分隔并弹出末尾元素:
 $branch = array_pop( split("/", $HTTP_RAW_POST_DATA['ref']) ); //this will return which branch the post data was sent from

然后我检查这个分支是否在 /var/www/website/directory/ 中有一个工作目录,例如 /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");
} 

这个逻辑似乎相对稳定,只是在这里发布以获得人们的意见。采用此方法,开发人员可以创建新的远程分支并将其推送到该分支中,然后该分支将被拉入/var/www网站目录中进行查看。
有没有其他方法允许开发人员查看他们的dev分支,或者有什么建议来改进这个脚本?
谢谢。

如果有人感兴趣的话,我已经添加了一篇关于我所采取的措施以及用于在我的服务器上设置此功能的完整代码块的博客文章: http://www.kernelops.com/gitlab-post-receive-webhook/ - sadmicrowave

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接