gitlab配置自动同步

若是须要同步到生产环境,请作额外处理,如自动化测试,测试经过再同步。php

<?php

$project = trim($_GET['project']);

if (empty($project)) {
    die('project not exist.');
}

//网站目录
$www_file='/home/wwwroot/' . $project . '/';

//打开网站目录下的hooks.log文件 须要在服务器上建立 并给写权限

$fs = fopen('./hooks.log', 'a');

fwrite($fs, '================ Update Start ==============='.PHP_EOL.PHP_EOL);

//自定义字串掩码 用于验证

$access_token = 's7kjjhh8767laq29KLJK9089883hjjkgfdrrpipoinmw';

//接受的ip数组,也就是容许哪些IP访问这个文件 这里是gitlab服务器IP
$access_ip = array('8.8.8.8', '119.23.153.156');

//获取请求端的ip和token

$client_token = $_GET['token'];
$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 $www_file && git checkout dev-master && git pull origin dev-master 2>&1");
fwrite($fs, 'Info:'. $output.PHP_EOL);

fwrite($fs,PHP_EOL. '================ Update End ==============='.PHP_EOL.PHP_EOL);

$fs and fclose($fs);

  

实际上gitlab的钩子post的数据包含了更多内容,上面只是作一个简单的同步。css

nginx配置html

server {
                listen       80;
                server_name  你的ip;
                charset utf-8;
                #access_log  logs/host.access.log  main;
                root /home/wwwroot/hooks;
                index  index.html index.htm index.php ;

                error_log logs/hooks.err.log;
                location / {
                        if (!-e $request_filename) {
                                rewrite ^(.*)$ /index.php?s=$1 last;
                                break;
                        }
                }
                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                        root   html;
                }

                location ~ [^/]\.php(/|$) {
                   fastcgi_pass   unix:/tmp/php-cgi.sock;
                   fastcgi_index index.php;
                   include fastcgi_params;
                   set $real_script_name $fastcgi_script_name;
                   if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                           set $real_script_name $1;
                           set $path_info $2;
                   }
                   fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
                   fastcgi_param SCRIPT_NAME $real_script_name;
                   fastcgi_param PATH_INFO $path_info;
                }
                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
                        expires 30d;
                        access_log off;
                }
                location ~ .*\.(js|css)?$ {
                        expires 7d;
                        access_log off;
           }
}

  

项目配置nginx

为项目设置自动更新git

  • 在gitlab项目设置里面,点击"Web钩子"
  • 连接填写http://ip地址/hooks.php?token=s7kjjhh8767laq29KLJK9089883hjjkgfdrrpipoinmw&project=xiaomi
  • 上面连接的xiaomi是对应服务器的/home/wwwroot/下的xiaomi目录
  • 私密受权码填写: s7kjjhh8767laq29KLJK9089883hjjkgfdrrpipoinmwshell

  • 触发选项:json

    • 勾选 "推送事件"
    • 勾选 "标签推送事件"
    • 其他留空
    • 反选 "开启SSL证书验证"
  • 点击 "增长Web钩子" 按钮数组

  • 在最下面的Web钩子列表,点击测试,200则没有问题服务器

token和上面php代码里面的一致,project是服务器上的项目目录名,默认路径/home/wwwrootgitlab

相关文章
相关标签/搜索