【原】Git@OSC webHooks源码分享
in 代码片段 with 0 comment

【原】Git@OSC webHooks源码分享

in 代码片段 with 0 comment

Amanage的项目在Git@OSC上落地啦,但是由于是团队开发,出现了测试困难的情况,好在开源中国提供了WebHook的功能,所以我就在我的阿里云上建了好些个虚拟机,在每次有人提交时候,服务器自动从git上拉取代码,这样,测试啥的就变得简单多了,下面,就和大家分享下,我在服务端是如何实现的吧。代码很简单~

<?php
    
    $str = $_POST['hook'];
    $date = date('Y-m-d');
    $path =  dirname(__FILE__);
    mkdir($path.'/tmp/'.$date.'/',0755, true);

    $arr = json_decode($str, true);
    $fieName = $arr['push_data']['after'];
    
    if( $arr['hook_name'] == 'push_hooks' ){
        file_put_contents('./tmp/'.$date.'/'.$fieName.'.log', $str);
        switch ( $arr['push_data']['ref'] ) {
            case 'refs/heads/zx':
                chdir($path.'/../zx/');
                shell_exec('git clean -df');
                shell_exec('git reset --hard');
                shell_exec('git pull origin zx:zx');
                shell_exec('git checkout zx');
                break;
            case 'refs/heads/mxy':
                chdir($path.'/../mxy/');
                shell_exec('git clean -df');
                shell_exec('git reset --hard');
                shell_exec('git pull origin mxy:mxy');
                shell_exec('git checkout mxy');
                break;
            case 'refs/heads/cxl':
                chdir($path.'/../cxl/');
                shell_exec('git clean -df');
                shell_exec('git reset --hard');
                shell_exec('git pull origin cxl:cxl');
                shell_exec('git checkout cxl');
                break;
            case 'refs/heads/master':
                chdir($path.'/../master/');
                shell_exec('git clean -df');
                shell_exec('git reset --hard');
                shell_exec('git pull origin master:master');
                shell_exec('git checkout master');
                break;
        }

    }
Comments are closed.