服务器的WEB目录:/mnt/webroot
在项目下面新建一个hooks.php:
<?php
//网站目录
$root = dirname(__FILE__) . '/';
//打开网站目录下的hooks.log文件 需要在服务器上创建 并给写权限
$fs = fopen($root . 'hooks.log', 'a');
fwrite($fs, '================ 更新开始 ===============' . PHP_EOL . PHP_EOL);
//自定义字串掩码 用于验证
$access_token = 'RVBEhz3DtM44bs6H';
//接受的ip数组,也就是允许哪些IP访问这个文件 这里是gitlab服务器IP
$access_ip = array('1.2.3.4');
//获取请求端的ip和token
$client_token = !empty($_GET['token']) ? $_GET['token'] : NULL;
if (empty($client_token)) exit;
$client_ip = $_SERVER['REMOTE_ADDR'];
//把请求的IP和时间写进log
fwrite($fs, 'Request on [' . date("Y-m-d H:i:s") . '] from [' . $client_ip . ']' . PHP_EOL);
//验证token 有错就写进日志并退出
if ($client_token !== $access_token) {
echo "error 403";
fwrite($fs, "Invalid token [{$client_token}]" . PHP_EOL);
exit(0);
}
//验证ip
if (!in_array($client_ip, $access_ip)) {
echo "error 503";
fwrite($fs, "Invalid ip [{$client_ip}]" . PHP_EOL);
exit(0);
}
//获取请求端发送来的信息,具体格式参见gitlab的文档
$json = file_get_contents('php://input');
$data = json_decode($json, true);
//把传送过来的信息写进log
//fwrite($fs, 'Data: '.print_r($data, true).PHP_EOL);
//执行shell命令并把返回信息写进日志
$output = shell_exec("cd $root && git checkout master && git pull origin master 2>&1");
fwrite($fs, 'Info:' . $output . PHP_EOL);
fwrite($fs, PHP_EOL . '================ Update End ===============' . PHP_EOL . PHP_EOL);
$fs and fclose($fs);
在WEB服务器上的设置:
查看php-fpm的运行用户,找到该用户的主目录。
我的运行用户为www,主目录为/mnt/webroot
把gitlab中项目的ssh key对应的秘匙id_rsa放入/mnt/webroot/.ssh
.ssh需要700的权限
chmod -R 700 .ssh
进入网站目录,初始化git 并设置权限
git init
chmod -R g+s .git
chown -R www:www .git
git remote add origin
手动进行第一次pull操作
git pull origin master
进入gitlab中项目settings设置,在web hooks中的URL填写:
http://www.xxx.com/hooks.php?token=RVBEhz3DtM44bs6H
这里的token要和hooks.php的token一致
因为.ssh文件在web的目录,在nginx中需要设置防止.ssh文件下载
location ~ \.ssh$ {
deny all;
}
注:代码中使用到了shell_exec这个函数,php配置中需要打开shell_exec