一:需求:本地将代码推送到远程的git的远程仓同库,
然后远程仓库的代码自动把代码同步到appcha的服务器上去
二:解决方案
1:服务器安装git
yum install git
2:添加用户文件
为了避免git pull时输入账号和密码,需要创建.git-credentials
2.1 切换到用户目录
cd ~
2.2:然后创建.git-credentials文件
vim .git-credentials
2.3:输入数据信息
https://用户名:密码@gitee.com
注意:用户名和密码是pull git的用户和密码
2.4:创建配置文件 .gitconfig
运行如下命令
git config --global credential.helper store
查看~/.gitconfig
2.5:为运行php-fpm的用户创建文件
查看运行 php-fpm的用户
ps -ef |grep php
例如是apache用户运行,则
> cp ~/.gitconfig /home/www/
> cp ~/.git-credentials /home/www/
> cd /home/www
> chown www.www .gitconfig
> chown www.www .git-credentials
3:克隆代码
例如:
> cd /data/wwwr/
> git clone https://gitee.com/lackone/xxxx.git
4:创建执行pull 的脚本文件
添加读写目录的权限
例如:给www读写 xxx的读写权限
chown -R :www /data/www/xxx
chmod -R g+w /data/www/xxx
<?php
//本地路径
$local = '/data/www';
//仓库地址
$remote = 'https://gitee.com/lackone/xxx.git';
//密码
$password = '123456';
//获取请求参数
$request = file_get_contents('php://input');
if (empty($request)) {
die('request is empty');
}
//验证密码是否正确
$data = json_decode($request, true);
if ($data['password'] != $password) {
die('password is error');
}
echo shell_exec("cd {$local} && git pull {$remote} 2>&1");
die('done ' . date('Y-m-d H:i:s', time()));
5:配置gitee的webhooks
6:验证,测试 ,结束