-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
60 lines (51 loc) · 1.63 KB
/
index.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
<?php
/*
* @Author: gogoend
* @Date: 2020-06-22 01:22:49
* @LastEditors: gogoend
* @LastEditTime: 2020-07-04 00:46:08
* @FilePath: \git-webhook\index.php
* @Description:
*/
if (file_exists('auth-config.php')) {
include('auth-config.php');
} else {
echo '发生错误 - 配置文件无效';
return http_response_code(404);
}
$secretInHeader = null;
$requestSource = null;
$requestHeader = getallheaders();
$requestBody = file_get_contents("php://input");
if(array_key_exists('X-Gitee-Token',$requestHeader)){
$requestSource = 'gitee';
$secretInHeader = $requestHeader['X-Gitee-Token'];
}
if(array_key_exists('X-Hub-Signature',$requestHeader)){
$requestSource = 'github';
$secretInHeader = $requestHeader['X-Hub-Signature'];
}
// var_dump($requestHeader);
if(!$secretInHeader || !$requestSource){
echo '发生错误 - 请求无效';
return http_response_code(404);
}
if ($requestSource==='github') {
// 来自GitHub的请求
list($algo, $hash) = explode('=', $secretInHeader, 2);
$payloadHash = hash_hmac($algo, $requestBody, SECRET_KEY);
if ($hash !== $payloadHash) {
echo '发生错误 - 签名无效';
return http_response_code(404);
}
} else if ($requestSource==='gitee') {
// 来自Gitee的请求
if ($secretInHeader !== SECRET_KEY) {
echo '发生错误 - 令牌无效';
return http_response_code(404);
}
}
echo "鉴权成功,可以部署\n";
$repoName = json_decode($requestBody)->repository->name;
echo "仓库名称:$repoName\n";
echo (exec("php build-cli.php $repoName 2>&1"));