-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab_tag_push.php
More file actions
86 lines (68 loc) · 2.46 KB
/
Copy pathgitlab_tag_push.php
File metadata and controls
86 lines (68 loc) · 2.46 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* aachim 09 2017
*
* Redirects webhook from gitlab to command wish starts build.
* A new security token has to be changed in gitlab AND here.
*/
$logPath = '/var/log/build.log';
$lockPath = '/tmp/build.lock';
$gitRequestAsArray = json_decode(file_get_contents('php://input'));
mylog('new Request received', $logPath);
if(!isLocked($lockPath)) {
createLock($lockPath, $logPath);
if(evaluateRequest($gitRequestAsArray, $logPath)) {
header("HTTP/1.1 200 OK");
} else {
header('HTTP/1.1 401 See mylog for details', true, 401);
}
removeLock($lockPath, $logPath);
} else {
mylog('Aborted because another process is already running', $logPath);
}
mylog('script end', $logPath);
function createLock($lockPath, $logPath) {
mylog('create lock to prevent multiple calls @ "/tmp/build.lock"', $logPath);
$lockFileHandle = fopen($lockPath, "w");
fclose($lockFileHandle);
}
function removeLock($lockPath, $logPath) {
unlink($lockPath);
mylog('remove lock', $logPath);
}
function isLocked($lockPath) {
return file_exists($lockPath);
}
function mylog($message, $path) {
system('echo "`date "+%Y-%m-%d %H:%M:%S"` ' . $message . '" >> ' . $path);
}
function evaluateRequest($gitRequestAsObject, $logPath) {
if(!is_object($gitRequestAsObject)) {
mylog('No information found in request body', $logPath);
return false;
}
$isTagPushEvent = $gitRequestAsObject->object_kind == 'tag_push';
if(!$isTagPushEvent) {
mylog('Aborting because of wrong push event (' . $gitRequestAsObject->object_kind . ')', $logPath);
return false;
} else {
mylog('Detected "tag_push" event', $logPath);
}
if($gitRequestAsObject->after == '0000000000000000000000000000000000000000') {
mylog('Aborting, because this is a delete push', $logPath);
return false;
} else {
$tag = array_pop(explode('/', $gitRequestAsObject->ref));
mylog('Extracted tag "' . $tag . '" from "' . $gitRequestAsObject->ref . '"', $logPath);
}
if(empty($_GET['app'])) {
mylog('Aborting because app was not defined in request. Please add app=(akira|kiwishop) to the push url', $logPath);
return false;
}else {
$app = $_GET['app'];
}
$command= "ssh build01@build01.gkm.cloud '/sbin/build_package " . $app . " " . $tag . " 1'";
mylog('Executing command: "' . $command . '"', $logPath);
system($command . ' >> ' . $logPath);
return true;
}