提交代码到码云(gitee)或者github自动同步到阿里云服务器
匿名 · 更新于 2018/11/26
最近在学习swoole,因为没有window的拓展,所以学习测试很不方便,然后就想着弄一个阿里云服务器,本地写完代码,直接git到码云(gitee)的仓库上,并且自动同步到阿里云服务器,我这里主要以码云(gitee)作讲解,
github,除了1,2处不一样,其他都一样的怎么做呢?往下看吧
二.确保阿里云服务器上安装了git,这里就不讲解git的安装
![]()
三.为了避免git pull时输入账号和密码,我们需要创建.git-credentials
1.先cd到当前用户目录
cd ~
2.然后创建.git-credentials文件
vi .git-credentials
3.写入如下数据,注意,用户名和密码替换上自己码云(gitee)或者github的用户名和密码,当然,如果你同时存在可以一行一个,存在同一个文件
https://用户名:密码@gitee.com //码云(gitee)的配置 https://用户名:密码@github.com //github的配置
4.接着运行如下命令,这里注意,如果你是第一次在服务器上使用git,那么你最好先运行git config配置一下你的用户,邮箱
git config --global user.name "用户名" git config --global user.email 邮箱
git config --global credential.helper store
5.查看~/.gitconfig,会发现多一项

6.注意这里只是配置的当前用户,这里我们查看php-fpm运行的用户是www(大家或许会跟我不同),所以我们需要为www也配置.git-credentials
![]()
我们把.gitconfig和.git-credentials复制到/home/www下,并设置所属用户和组为www
cp ~/.gitconfig /home/www/ cp ~/.git-credentials /home/www/ cd /home/www chown www.www .gitconfig chown www.www .git-credentials
四.我们到阿里云服务器的项目目录git clone项目代码
cd /data/wwwroot/ git clone https://gitee.com/guixianfeng/test.git
五.由于我们是通过php脚本执行git pull所以,需要给www用户读写test目录的权限
chown -R www:www /data/wwwroot/test chmod -R g+w /data/wwwroot/test
六.接着我们在服务器/data/wwwroot/test项目入口出新建一个webhooks.php, 拉取代码脚本,注意该脚本一定能外网访问, 这里我的为test项目解析了一个http://tp.kinggui.com的域名
码云(gitee)
//本地路径
$local = '/data/wwwroot/test';
//仓库地址
$remote = 'https://gitee.com/guixianfeng/test.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()));
此处有点要注意,git pull代码处,我在服务器上/data/wwwroot/test可以手动git pull拉下代码,但是远程访问webhooks.php脚本时,提示git: command not found,那么此处倒数第二行代码可以改为如下:
echo shell_exec("cd {$local} && /usr/local/git/bin/git pull {$remote} 2>&1");
这里/usr/local/git为我git的安装目录,大家的安装目录写成各自对应的目录即可
github所用webhooks
//本地路径
$local = '/data/wwwroot/gxf';
//仓库地址
$remote = 'https://github.com/kingwow/gxf.git';
//密钥,github是密钥,验证方式跟gitee不一样
$secret = '123456';
//获取请求参数
$request = file_get_contents('php://input');
if (empty($request)) {
die('request is empty');
}
//获取http 头
$headers = getHeaders();
//github发送过来的签名
$hubSignature = $headers['X-Hub-Signature'];
list($algo, $hash) = explode('=', $hubSignature, 2);
// 计算签名
$payloadHash = hash_hmac($algo, $request, $secret);
// 判断签名是否匹配
if ($hash != $payloadHash) {
die('secret is error');
}
echo shell_exec("cd {$local} && /usr/local/git/bin/git pull {$remote} 2>&1");
die('done ' . date('Y-m-d H:i:s', time()));
/**
* @todo 获取头信息
*/
function getHeaders()
{
$headers = array();
//Apache服务器才支持getallheaders函数
if (!function_exists('getallheaders')) {
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
} else {
$headers = getallheaders();
}
return $headers;
}
八、最后我们只要git push代码,gitee就会同步触发webhooks设置的脚本,进行同步拉取代码。


