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远程仓库配置回调地址

查看触发结果

相关推荐
犹若故人归4 小时前
Github/Gitee和Git实践
git·gitee·github
商业讯网15 小时前
国家电投海外项目运营经验丰富
大数据·人工智能·区块链
面向Google编程5 小时前
Flink源码阅读:Mailbox线程模型
大数据·flink
007php0076 小时前
mySQL里有2000w数据,Redis中只存20w的数据,如何保证Redis中的数据都是热点数据
数据库·redis·git·mysql·面试·职场和发展·php
Elastic 中国社区官方博客7 小时前
使用 Elastic 中的 OpenTelemetry 为 Nginx 实现端到端分布式追踪的实用指南
大数据·运维·分布式·elasticsearch·搜索引擎·信息可视化·全文检索
fakerth7 小时前
Git 完全指南:从入门到精通
git
aliprice7 小时前
逆向拆解:用速卖通图片搜索破解竞品设计,找到你的差异化定价空间
大数据·跨境电商·电商
hg01187 小时前
埃及:在变局中重塑发展韧性
大数据·人工智能·物联网
XiaoHu02078 小时前
Linux网络编程套接字
linux·服务器·网络·git
向量引擎小橙8 小时前
“2026数据枯竭”警报拉响:合成数据如何成为驱动AI进化的“新石油”?
大数据·人工智能·深度学习·集成学习