-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.php
More file actions
69 lines (55 loc) · 1.56 KB
/
service.php
File metadata and controls
69 lines (55 loc) · 1.56 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
<?php
/**
* github-deploy
*
* Automatically deploy from github with webhooks
*
* @author Roberto Ramírez <robertoiran@gmail.com>
*/
include 'lib/config.php';
$data = json_decode(file_get_contents('php://input'), true);
$commands = ['whoami'];
try {
Config::init(__DIR__ . '/config.json');
preg_match_all('/\w+/', $data['ref'], $pushRef);
$Repository = Config::getRepository($data['repository']['id'])
->validateToken(@$_GET['_token'])
;
$Branch = $Repository->getBranch($pushRef[0][2]);
if (true == $data['deleted']) {
$commands = array_merge($commands, $Branch->triggerOnDeleted());
} elseif (! $Branch->isLocalDirectory()) {
$commands = array_merge($commands, $Branch->gitClone());
} else {
chdir($Branch->getLocalDirectory());
$commands = array_merge($commands, $Branch->gitPull());
}
$output = [];
foreach ($commands as $command) {
$result = [];
$command = escapeshellcmd($command);
exec($command . ' 2>&1', $result, $returnCode);
$output[] = [
'$ ' . $command,
$result,
$returnCode
];
}
header('HTTP/1.0 200');
header('Content-type: application/json');
echo json_encode($output);
} catch (InvalidArgumentException $e) {
header(
sprintf('HTTP/1.0 %s %s',
$e->getCode(),
$e->getMessage()
)
);
} catch (Exception $e) {
header(
sprintf('HTTP/1.0 %s %s',
$e->getCode(),
$e->getMessage()
)
);
}