-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.php
59 lines (54 loc) · 1.5 KB
/
deploy.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
<?php
/**
* GIT DEPLOYMENT SCRIPT
*
* Used for automatically deploying websites via github securely, more deets here:
*
* https://gist.github.com/limzykenneth/baef1b190c68970d50e1
*/
// The header information which will be verified
$agent = $_SERVER['HTTP_USER_AGENT'];
$signature = $_SERVER['HTTP_X_HUB_SIGNATURE'];
$body = @file_get_contents('php://input');
// The commands
$commands = array(
'echo $PWD',
'whoami',
'git pull origin master',
'git status',
'git submodule sync',
'git submodule update',
'git submodule status',
'php -d detect_unicode=Off bin/composer install'
);
$output = "";
base64_encode($agent);
base64_encode($signature);
if (strpos($agent, 'GitHub-Hookshot') !== false) {
if (hash_equals($signature, verify_request())) {
// Run the commands
foreach ($commands as $command) {
// Run it
$tmp = shell_exec($command);
$output .= $command . "\n";
$output .= htmlentities(trim($tmp)) . "\n\n";
}
} else {
header('HTTP/1.1 403 Forbidden');
echo "Invalid request.";
}
} else {
header('HTTP/1.1 403 Forbidden');
echo "Invalid request.";
}
// Generate the hash verification with the request body and the key stored in your .htaccess file
function verify_request()
{
$message = $GLOBALS['body'];
$key = $_ENV['GIT_TOKEN'];
$hash = hash_hmac("sha1", $message, $key);
$hash = "sha1=" . $hash;
return $hash;
}
echo "Deploy successful.\n\n";
echo $output;