【问题标题】:GitLab 5.2 Post-Receive WebHookGitLab 5.2 接收后 WebHook
【发布时间】:2013-06-16 02:59:59
【问题描述】:

我在与我的生产网络服务器相同的服务器上运行 GitLab v5.2(/var/www 中的文档根目录)。

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

谁能给我一个从帖子数据中提取脚本的示例,或者指出我创建一个正确的方向?

GitLab 挂钩请求示例 - 对于那些没有 GitLab 实例的用户,以下是 GitLab 接收后 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,
};

【问题讨论】:

    标签: json git gitlab webhooks git-post-receive


    【解决方案1】:

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

    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/ 目录与 master 保持同步;但是,其他各种分支呢?我需要一些方法来通过我的网络服务器查看其他分支,以便开发团队可以查看他们的工作......

    这是我的想法:

    我的代码不是在帖子字符串的 ref 部分中寻找“master”,而是在“/”分隔符上拆分帖子字符串并弹出结束元素:

     $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 网络目录中查看。

    谁能想到另一种方法让开发人员查看他们的开发分支或任何关于如何使这个脚本更好的建议?

    谢谢

    【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      • 2017-12-17
      • 2020-03-14
      • 1970-01-01
      • 2022-09-29
      • 2018-12-06
      • 2015-11-02
      相关资源
      最近更新 更多