forked from magenx/gitlab-webhook-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab.php
62 lines (54 loc) · 1.7 KB
/
gitlab.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
<?php
/* gitlab deploy webhook */
/* file.php?token=oGN3YTBuPizLa5Pwgx8ICvoNn3OqFVFKBOxtwchjs2a8z8vOdEqcUiLWsvjfz5j */
/* security */
$access_token = 'oGN3YTBuPizLa5Pwgx8ICvoNn3OqFVFKBOxtwchjs2a8z8vOdEqcUiLWsvjfz5j';
$access_ip = array('122.34.65.90');
/* get user token and ip address */
$client_token = $_GET['token'];
$client_ip = $_SERVER['REMOTE_ADDR'];
/* create open log */
$fs = fopen('./webhook.log', 'a');
fwrite($fs, 'Request on ['.date("Y-m-d H:i:s").'] from ['.$client_ip.']'.PHP_EOL);
/* test token */
if ($client_token !== $access_token)
{
echo "error 403";
fwrite($fs, "Invalid token [{$client_token}]".PHP_EOL);
exit(0);
}
/* test ip */
if ( ! in_array($client_ip, $access_ip))
{
echo "error 503";
fwrite($fs, "Invalid ip [{$client_ip}]".PHP_EOL);
exit(0);
}
/* get json data */
$json = file_get_contents('php://input');
$data = json_decode($json, true);
/* get branch */
$branch = $data["ref"];
fwrite($fs, '======================================================================='.PHP_EOL);
/* if you need get full json input */
//fwrite($fs, 'DATA: '.print_r($data, true).PHP_EOL);
/* branch filter */
if ($branch === 'refs/heads/master')
{
/* if master branch*/
fwrite($fs, 'BRANCH: '.print_r($branch, true).PHP_EOL);
fwrite($fs, '======================================================================='.PHP_EOL);
$fs and fclose($fs);
/* then pull master */
exec("/home/deploy/master_deploy.sh");
}
else
{
/* if devel branch */
fwrite($fs, 'BRANCH: '.print_r($branch, true).PHP_EOL);
fwrite($fs, '======================================================================='.PHP_EOL);
$fs and fclose($fs);
/* pull devel branch */
exec("/home/deploy/devel_deploy.sh");
}
?>