forked from shenzhe/git-hook-auto-update
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
70 lines (63 loc) · 1.85 KB
/
server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
function mylog($msg, $filename='hook_error'){
error_log(date("Y-m-d H:i:s")."\t".$msg.PHP_EOL, 3, __DIR__.DIRECTORY_SEPARATOR.$filename.'.mylog');
}
define('PASSWORD', '');
$config = null;
$http = new swoole_http_server("0.0.0.0", 9501);
$http->set([
'worker_num'=>4,
'daemonize'=>1,
]);
$http->on('request', function ($request, $response) {
$response->end('ok');
if(false !== strpos($request->server['path_info'], 'reload')) { //重启server,(可重载配置)
global $http;
$http->reload();
return;
}
if(empty($request->post['payload'])) {
mylog('hook data empty');
return;
}
$datas = json_decode($request->post['payload'], true);
if(empty($datas['hook'])) {
//错误的数据
mylog('hook no json data');
return;
}
$data = $datas['hook'];
if(PASSWORD && $data['config']['secret'] != PASSWORD) {
//密码不匹配
mylog('password error');
return ;
}
if(empty($data['push_data']['commits'])) {
//没有提交者
mylog('no commit');
return ;
}
//项目名称
$project = $data['push_data']['repository']['name'];
//分支名称
$ref = $data['push_data']['ref'];
mylog("{$project}: {$ref} push");
global $config;
if(empty($config[$project][$ref])) {
mylog("no {$project} {$ref}");
return ;
}
$client = new \swoole_client(SWOOLE_SOCK_UDP);
foreach($config[$project][$ref] as $item) {
$client->connect($item['host'], $item['port']);
$client->send(json_encode([$project, $ref, $item['path']]));
}
});
$http->on('workerStart', function($serv, $workerId) {
global $config;
if(function_exists('opcache_reset')) {
opcache_reset();
}
$config = include(__DIR__.DIRECTORY_SEPARATOR.'config.php');
});
$http->start();