1. 最好单独优先安装一个nginx, 因为gitlab自导nginx, 不然你再单独装nginx的时候会有意想不到的问题
2. 参考:
https://blog.csdn.net/duyusean/article/details/80011540
https://blog.csdn.net/liudihedy/article/details/80595268
3.基本安装成功
4. 安装webhook
webhook 有点像githook, 作用就是将git服务器上的仓库及时同步你测试服务器上(git服务器可以和测试服务器在同一台机器上)
本地改完代码只要推到git服务器, git服务器再通过webhook(就是一个网址)触发测试服务上的某段代码自动同步git服务器代码
如下所示:
1. 本地git( ip: 101. 56.45.88)
2.git服务器(gitlab) (ip: 60:66:57:80)
3.测试服务器(ip: 60:66:57:80)
A): 本地git推代码待git服务器
B): git服务器配置webhook
C): git服务器的项目收到push事件通知, 会hook这个url (www:ceshi.com/webhook)
D:): 测试服务器代码配置(php)
function index()
{
//网站目录
$www_file = '/var/www/html/ceshi/';
//打开网站目录下的hooks.log文件,需要在服务器上创建,并给写权限
$fs = fopen('/var/www/html/ceshi/'.date('Ymd').'-hooks.log', 'a');
fwrite($fs, '================ Update Start ===============' . PHP_EOL . PHP_EOL);
//自定义字串掩码 用于验证
$access_token = 'qwertyuiolmjhnfgsjdfgg';
//接受的ip数组,也就是允许哪些IP访问这个文件 这里是gitlab服务器IP
$access_ip = array('127.0.0.1', '192.168.0.4');
//如果使用www.xxx.com/xxx.php?token=xxxxxxx 的方式来传送验证字符串,则用这个方法获取
# $client_token = $_GET['token'];
// 获取请求端的secret token
//$client_token = $_SERVER["HTTP_X_GITLAB_TOKEN"];
$client_token = $this->request()->getHeaders()['x-gitlab-token'][0];
//获取请求端的IP
//$client_ip = $_SERVER['REMOTE_ADDR'];
$client_ip = $this->request()->getHeaders()['x-real-ip'][0];
//把请求的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);
return $this->writeJson(2000, 'param not null');
}
//验证ip
if (!in_array($client_ip, $access_ip)) {
echo "error 503";
fwrite($fs, "Invalid ip [{$client_ip}]" . PHP_EOL);
return $this->writeJson(2000, 'param not null');
}
//获取请求端发送来的信息,具体格式参见gitlab的文档
//$json = file_get_contents('php://input');
$data = json_decode($this->request()->getBody()->__toString(), true);
//如果有需要 可以打开下面,把传送过来的信息写进log
# fwrite($fs, 'Data: '.print_r($data, true).PHP_EOL);
//执行shell命令并把返回信息写进日志
$output = shell_exec("cd $www_file && git pull 2>&1");
fwrite($fs, 'Info:' . $output . PHP_EOL);
fwrite($fs, PHP_EOL . '================ Update End ===============' . PHP_EOL . PHP_EOL);
$fs and fclose($fs);
//return $this->writeJson(2000, 'param not null');
}