(PHP) git webhook 自动部署脚本

在git管理中配置POST请求该方法对应的路由进行自动调用


 public function giteepush()
    {
        //git webhook 自动部署脚本
        //项目存放物理路径,第一次clone时,必须保证该目录为空
        $isClone = true;//设置是否已经Clone到本地,true:已经clone,直接pull,false:先clone.
        $savePath = '/data/www/project';
        //如果已经clone过,则直接拉去代码
        if ($isClone) {
            $requestBody = file_get_contents("php://input");
            if (empty($requestBody)) {
                die('send fail');
            }

            //解析Git服务器通知过来的JSON信息
            $content = json_decode($requestBody, true);
            // var_dump($content)
            //若是dev分支且提交数大于0
            if ($content['ref']=='refs/heads/dev' && $content['total_commits_count']>0) {

                $res = PHP_EOL."pull start --------".PHP_EOL;
                // $output = shell_exec("cd /data/www/project && git pull 2>&1");
                $res .= shell_exec("cd {$savePath} && git pull");//拉取代码
                $res_log = '-------------------------'.PHP_EOL;
                $res_log .= $content['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '项目的' . $content['ref'] . '分支push了' . $content['total_commits_count'] . '个commit:';
                $res_log .= $res.PHP_EOL;
                $res_log .= "pull end --------".PHP_EOL;
                echo $content['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '项目的' . $content['ref'] . '分支push了' . $content['total_commits_count'] . '个commit:';
                Log::channel('daily')->info($res_log);

            }else{
                die('非dev分支提交操作不更新');
            }
        }else {
            // $res = "clone start --------".PHP_EOL;
            //注:在这里需要设置用户邮箱和用户名,不然后面无法拉去代码
            // $res .= shell_exec("git config --global user.email {$email}}").PHP_EOL;
            // $res .= shell_exec("git config --global user.name {$name}}").PHP_EOL;
            // $res .= shell_exec("git clone {$gitPath} {$savePath}").PHP_EOL;
            // $res .= "clone end --------".PHP_EOL;
            // Log::channel('daily')->info($res);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值