gitee使用webhook实现自动部署。
首先把服务器的公钥添加到你项目仓库中

1、首先生成公钥: sudo -u www ssh-keygen -t rsa -C "xxxxx@qq.com"
2、把公钥添加到gitee中
3、克隆远程仓库
sudo -u www git clone git@gitee.com:Abner3721/abner.git /www/wwwroot/hooks
2、在你的仓库中添加远程仓库地址:git remote add <remote_name> <repository_url> 在这个命令中,<remote_name> 是你为远程仓库设置的名称,<repository_url> 是远程仓库的 URL。
例如:git remote add origin https://github.com/your_username/your_repository.git
3、在你的根据目录添加一个可以访问的的脚本,我的是php脚本,auth.php
<?php
// 先做一下测试
// 测试ls是否能用
// $output = shell_exec('ls -lart');
// echo "<pre>$output</pre>";
// echo shell_exec("ls -a");
error_reporting(1);
//git webhook 自动部署脚本
//项目存放物理路径,第一次clone时,必须保证该目录为空
$savePath = '/www/wwwroot/demo.com';//更新
//$savePath = "/data/wwwroot/default/cms.fdgdgd.cn/application";//更新
//$savePath8 = "/data/wwwroot/default/www.fdgdgdfd.com/application";//更
// $gitPath = "https://gitee.com/fdgdg/cms.git";//代码仓库
$gitPath = 'git@gitee.com:Abner3721/tsgy.git';//代码仓库 一定要使用ssh方式不然每次都得输入密码
$branch = "refs/heads/master";//同步那个分支
//密码
$password = 'Abner@3721';
// //获取请求参数
$request = file_get_contents('php://input');
if (empty($request)) {
die('request is empty');
}
// //echo $request;
// //验证密码是否正确
$content = json_decode($request, true);
if (md5($content['password']) != md5($password)) {
die('非法提交');
}
//运行代码 拉取码云最代码到阿里服务器上
//说明:
//1.shell_exec()即PHP用于执行系统命令的函数,
//2.cd $dir:进入上面设置的项目仓库文件夹中,
//3.git checkout -f 撤销本地的修改,
//4.git pull origin master 从码云的项目仓库master分支拉取最新代码,注意origin为远程仓库的别名,要与 git remote add 远程仓库,
//5.别名 仓库地址 中的远程仓库别名保持一致
// echo shell_exec("cd {$savePath} && git checkout -f && git pull {$branch} 2>&1"); //更新锋牛代码
//echo shell_exec("cd {$savePath8} && git checkout -f && git pull {$branch} 2>&1"); //更众联牛代码
echo shell_exec("cd {$savePath} && git pull {$gitPath} {$branch} 2>&1");//只能拉取远程码云master分支到阿里云服务器上,其它分支不会拉取
die('done ' . date('Y-m-d H:i:s', time()));
?>
4、在gitee的webhooks中添加自动部署的勾子

出现这个提示 表示.git的没有对应的权限 error: cannot open .git/FETCH_HEAD: Permission denied \n\rdone 2023-05-26 19:08:06
4、修改你项目中的.git的权限,修改成和你网站的权限一样,比如:www
如果遇到问题:Host key verification failed.
参考:https://www.siediyer.cn/?p=1627
参考链接:https://blog.51cto.com/haibo0668/5885219

浙公网安备 33010602011771号