git自动更新功能

确认权限

因为一般Linux系统网页用的wwwwww-data用户和用户组,所以要实现自动来去,首先要在www用户权限下生成ssh密钥,不然没有权限,其次就是,要把用root用户拉去的代码,批量改成www用户

1. 给www权限

复制代码
vi  /etc/sudoers
www     ALL=(ALL)     NOPASSWD:/bin/chown,/bin/chmod,/usr/bin/git

2.切换www账户

一切操作在www用户下完成

给www账户添加git 邮箱和和用户名

bash 复制代码
sudo git config --global user.email "[email protected]"
sudo git config --global user.name "finejade"

添加origin

如果remote- roigin 远程地址是http的,改成ssh的,可以使用ssh秘钥免登录提交

bash 复制代码
git remote -v
git remote remove origin
git remtoe add origin [email protected]:finejade/thinkphp6.git

生成密钥

生成密钥,一直下一步

复制代码
 ssh-keygen -t rsa -C "[email protected]"

把公钥填写到git个人账号的SSH密钥

编写自动拉去代码

php 复制代码
  public function autopull()
    {


        $json = file_get_contents("php://input");
        file_put_contents('./a.txt',$json);
        //获取请求参数
        if (empty($json)) {
            die('request is empty');
        }
        $data = json_decode($json, true);

        $savePath =  Env::get('root_path');
        //密码
        if (isset($data['password'])) {
            $password = 'AJ8ENFEOWLS';
        //验证密码是否正确
            if ($data['password'] != $password) {
                header("HTTP/1.1 403 Forbidden");
                die('非法提交');
            }
        }

        //一定要在php.ini 配置文件中解除所有禁用 disable_funtions  exec 方法

        // git放弃修改,强制覆盖本地代码
        echo shell_exec("cd {$savePath} && git checkout ."); // git checkout
        // echo shell_exec("cd {$savePath} && git pull {$gitPath}  2>&1");

        $res = PHP_EOL . "pull start " . PHP_EOL;
        $res .= shell_exec("cd " . $savePath . " && git pull origin develop 2<&1 "); //代码仓库 一定要使用ssh方式不然每次都得输入密码
        $res_log = PHP_EOL;
        $res_log .= $data['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $data['repository']['name'];
        $res_log .= '项目的' . $data['ref'] . '分支push了' . $data['total_commits_count'] . '个 ';
        $message = '';
        if (isset($data['commits']['message'][0])){
            $message = $data['commits']['message'][0];
        }elseif (isset($data['commits']['message'])){
            $message = $data['commits']['message'];
        }
        $res_log .= 'commit:' . $message;
        $res_log .= $res;
        $res_log .= "pull end -----------------------------------------------------" . PHP_EOL;

        //保存日志文件路径 注意开启日志写入权限
        Log::debug('git自动拉去:'.$res_log);
        if (isset($data['ref']) && $data['total_commits_count'] > 0) {
            $typeText = "\r\n更新正常\r\n";
            $res_log .= $typeText;
        } else {
            $typeText = "\r\n无提交也更新\r\n";
            $res_log .= $typeText;
        }
        echo $res_log;
    }

在git远程仓库配置回调地址

查看触发结果

相关推荐
自来也_35 分钟前
Git配置代理
git
Tianyanxiao1 小时前
华为×小鹏战略合作:破局智能驾驶深水区的商业逻辑深度解析
大数据·人工智能·经验分享·华为·金融·数据分析
线条14 小时前
大数据 ETL 工具 Sqoop 深度解析与实战指南
大数据·sqoop·etl
mazhafener12311 小时前
智慧照明:集中控制器、单双灯控制器与智慧灯杆网关的高效协同
大数据
打码人的日常分享11 小时前
物联网智慧医院建设方案(PPT)
大数据·物联网·架构·流程图·智慧城市·制造
Lansonli12 小时前
大数据Spark(六十一):Spark基于Standalone提交任务流程
大数据·分布式·spark
Rverdoser14 小时前
电脑硬盘分几个区好
大数据
傻啦嘿哟14 小时前
Python 数据分析与可视化实战:从数据清洗到图表呈现
大数据·数据库·人工智能
Theodore_102214 小时前
大数据(2) 大数据处理架构Hadoop
大数据·服务器·hadoop·分布式·ubuntu·架构
簌簌曌14 小时前
CentOS7 + JDK8 虚拟机安装与 Hadoop + Spark 集群搭建实践
大数据·hadoop·spark