GitLab 5.2 Post-Receive WebHook

2019-03-13 12:55发布

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

2条回答
Ridiculous、
2楼-- · 2019-03-13 13:16
太酷不给撩
3楼-- · 2019-03-13 13:23

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

查看更多
登录 后发表回答