GitLab 5.2后接收WebHook [英] GitLab 5.2 Post-Receive WebHook

查看:171
本文介绍了GitLab 5.2后接收WebHook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我试图设置一个标准的GitLab Post-Receive Hook,但我发现了一些关于如何设置脚本来处理发布的JSON数据的令人惊讶的信息。我并没有试图做任何自定义的事情,只是开箱即用,我想在我的网站位置(记得在同一台服务器上)接收收到后的数据,然后在收到原始主机时从其拉取(只要接收到的数据发起推送到主分支)。这样,在/ 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 /

提交:[
{
id:b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327,
message:将Catalan翻译为e38cb41。 ,
timestamp:2011-12-12T14:27:31 + 02:00,
url:http:// localhost / d iaspora / 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,
};


解决方案

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



PHP

  error_reporting(E_ALL); 
ignore_user_abort(true);

函数系统调用($ cmd,$ cwd){
$ descriptorspec = array(
1 => array('pipe','w')// stdout是一个孩子将写入
)的管道;
$ 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 /目录保持为最新;但是,其他各个分支呢?我需要一些方法来通过我的Web服务器查看其他分支,这样开发团队可以查看他们的工作...



这是我的考虑:

与我的代码只是在后字符串的ref部分查找master,我将后字符串分割为/分隔符并弹出结束元素:

$ $ p $ $ code $ $ branch = array_pop(split(/,$ HTTP_RAW_POST_DATA ['ref'])); //这将返回发布数据从
发送的分支

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

  if(is_dir(/ var / www / website / directory / $ branch)){//检查分支目录是否存在
$ result = syscall (git pull origin $ branch,/ var / www / website / directory / $ branch);
} else {
//如果分支目录不存在,用一个克隆
$ result = syscall创建它(git clone ssh://git@git.mydomain.com/ sadmicrowave / someproject.git $ branch,/ var / www / website / directory);
//将目录更改为克隆目录,并检出分支
$ result = syscall(git checkout $ branch,/ var / www / website / directory / $ branch);
}

这个逻辑看起来比较稳固,只是在这里发布以查看人们的意见。使用这种方法,开发人员可以创建一个新的远程分支,并推送到该分支,然后该分支将被拉入/ var / www Web目录中进行查看。



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



谢谢


I'm running GitLab v5.2 on the same server as my production webserver (Document Root in /var/www).

I'm trying to setup a standard GitLab Post-Receive Hook but am finding surprisingly little information about how to set up a script to process the posted JSON data. I'm not trying to do anything custom, just right out of the box, I want to receive the post-receive data at my website location (remember on the same server), and then pull from origin-master when it gets received (provided the post-receive data originating push was to the master branch). This way, the website found in /var/www is always same a master.

Could someone either, give me an example of a pull script from the post data or point me in the right direction for me to create one?

GitLab Hook Request Example - For those without a GitLab instance, here is what the GitLab Post-Receive JSON data looks like (straight from the GitLab help)

{
 "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,
};

解决方案

Alright, after extensive digging I've found enough docs to create my own script, and here it is:

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

So, this works perfectly for its purpose. But now I need to rethink the logic and potentially even the philosophy. This methodology will automatically keep the /var/www/website/directory up-to-date with master; however, what about various other branches? I need some methodology in place to be able to view other branches through my web server, so the dev teams can view their work...

Here is what I'm thinking:

Rather than my code just looking for "master" within the ref section of the post string, I split the post string on the "/" delimiter and pop off the end element:

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

Then I check to see if this branch has a working directory within the /var/www/website/directory/, like /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");
} 

This logic seems relatively solid, just posting it on here to see people's opinions. With this method, a developer can create a new remote branch, and push to it, then the branch will get pulled into the /var/www web directory for viewing.

Can anyone think of another way to allow developers to view their dev branches or any recommendations as to how to make this script better?

Thanks

这篇关于GitLab 5.2后接收WebHook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆