GitLab 5.2 Post-Receive WebHook

纵然是瞬间 提交于 2019-12-03 00:21:04

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

Sytse Sijbrandij

Up to date docs are in the application itself: https://docs.gitlab.com/ce/user/project/integrations/webhooks.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!