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 "xxx@qq.com"
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 git@git.code.tencent.com:finejade/thinkphp6.git

生成密钥

生成密钥,一直下一步

复制代码
 ssh-keygen -t rsa -C "332410549@qq.com"

把公钥填写到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远程仓库配置回调地址

查看触发结果

相关推荐
大只鹅33 分钟前
Springboot3.3.4使用spring-data-elasticsearch整合Elasticsearch7.12.1
spring boot·elasticsearch
lifallen1 小时前
Paimon vs. HBase:全链路开销对比
java·大数据·数据结构·数据库·算法·flink·hbase
爱吃面的猫1 小时前
大数据Hadoop之——Hbase下载安装部署
大数据·hadoop·hbase
viperrrrrrrrrr71 小时前
大数据(1)-hdfs&hbase
大数据·hdfs·hbase
拓端研究室2 小时前
专题:2025即时零售与各类人群消费行为洞察报告|附400+份报告PDF、原数据表汇总下载
大数据·人工智能
武子康3 小时前
大数据-30 ZooKeeper Java-API 监听节点 创建、删除节点
大数据·后端·zookeeper
CC码码3 小时前
管理你的多个 Git 密钥(多平台多账号)
git·gitlab·github
CC码码3 小时前
管理你的多个 Git 密钥(单平台多账号)
git·gitlab·github
大卫小东(Sheldon)3 小时前
GIM 1.5发布了! 支持Windows系统了
git·ai·rust
小手WA凉3 小时前
Hadoop之MapReduce
大数据·mapreduce