-
Notifications
You must be signed in to change notification settings - Fork 235
/
deployer.php
230 lines (186 loc) · 7.95 KB
/
deployer.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
$content = file_get_contents("php://input");
$json = json_decode($content, true);
$file = fopen(LOGFILE, "a");
$time = time();
$token = false;
$sha = false;
$DIR = preg_match("/\/$/", DIR) ? DIR : DIR . "/";
// retrieve the token
if (!$token && isset($_SERVER["HTTP_X_HUB_SIGNATURE"])) {
list($algo, $token) = explode("=", $_SERVER["HTTP_X_HUB_SIGNATURE"], 2) + array("", "");
} elseif (isset($_SERVER["HTTP_X_GITLAB_TOKEN"])) {
$token = $_SERVER["HTTP_X_GITLAB_TOKEN"];
} elseif (isset($_GET["token"])) {
$token = $_GET["token"];
}
// retrieve the checkout_sha
if (isset($json["checkout_sha"])) {
$sha = $json["checkout_sha"];
} elseif (isset($_SERVER["checkout_sha"])) {
$sha = $_SERVER["checkout_sha"];
} elseif (isset($_GET["sha"])) {
$sha = $_GET["sha"];
}
// write the time to the log
date_default_timezone_set("UTC");
fputs($file, date("d-m-Y (H:i:s)", $time) . "\n");
// specify that the response does not contain HTML
header("Content-Type: text/plain");
// use user-defined max_execution_time
if (!empty(MAX_EXECUTION_TIME)) {
ini_set("max_execution_time", MAX_EXECUTION_TIME);
}
// function to forbid access
function forbid($file, $reason) {
// format the error
$error = "=== ERROR: " . $reason . " ===\n*** ACCESS DENIED ***\n";
// forbid
http_response_code(403);
// write the error to the log and the body
fputs($file, $error . "\n\n");
echo $error;
// close the log
fclose($file);
// stop executing
exit;
}
// Check for a GitHub signature
if (!empty(TOKEN) && isset($_SERVER["HTTP_X_HUB_SIGNATURE"]) && $token !== hash_hmac($algo, $content, TOKEN)) {
forbid($file, "X-Hub-Signature does not match TOKEN");
// Check for a GitLab token
} elseif (!empty(TOKEN) && isset($_SERVER["HTTP_X_GITLAB_TOKEN"]) && $token !== TOKEN) {
forbid($file, "X-GitLab-Token does not match TOKEN");
// Check for a $_GET token
} elseif (!empty(TOKEN) && isset($_GET["token"]) && $token !== TOKEN) {
forbid($file, "\$_GET[\"token\"] does not match TOKEN");
// if none of the above match, but a token exists, exit
} elseif (!empty(TOKEN) && !isset($_SERVER["HTTP_X_HUB_SIGNATURE"]) && !isset($_SERVER["HTTP_X_GITLAB_TOKEN"]) && !isset($_GET["token"])) {
forbid($file, "No token detected");
} else {
// check if pushed branch matches branch specified in config
if ($json["ref"] === BRANCH) {
fputs($file, $content . PHP_EOL);
// ensure directory is a repository
if (file_exists($DIR . ".git") && is_dir($DIR)) {
// change directory to the repository
chdir($DIR);
// write to the log
fputs($file, "*** AUTO PULL INITIATED ***" . "\n");
/**
* Attempt to reset specific hash if specified
*/
if (!empty($_GET["reset"]) && $_GET["reset"] === "true") {
// write to the log
fputs($file, "*** RESET TO HEAD INITIATED ***" . "\n");
exec(GIT . " reset --hard HEAD 2>&1", $output, $exit);
// reformat the output as a string
$output = (!empty($output) ? implode("\n", $output) : "[no output]") . "\n";
// if an error occurred, return 500 and log the error
if ($exit !== 0) {
http_response_code(500);
$output = "=== ERROR: Reset to head failed using GIT `" . GIT . "` ===\n" . $output;
}
// write the output to the log and the body
fputs($file, $output);
echo $output;
}
/**
* Attempt to execute BEFORE_PULL if specified
*/
if (!empty(BEFORE_PULL)) {
// write to the log
fputs($file, "*** BEFORE_PULL INITIATED ***" . "\n");
// execute the command, returning the output and exit code
exec(BEFORE_PULL . " 2>&1", $output, $exit);
// reformat the output as a string
$output = (!empty($output) ? implode("\n", $output) : "[no output]") . "\n";
// if an error occurred, return 500 and log the error
if ($exit !== 0) {
http_response_code(500);
$output = "=== ERROR: BEFORE_PULL `" . BEFORE_PULL . "` failed ===\n" . $output;
}
// write the output to the log and the body
fputs($file, $output);
echo $output;
}
/**
* Attempt to pull, returing the output and exit code
*/
exec(GIT . " pull 2>&1", $output, $exit);
// reformat the output as a string
$output = (!empty($output) ? implode("\n", $output) : "[no output]") . "\n";
// if an error occurred, return 500 and log the error
if ($exit !== 0) {
http_response_code(500);
$output = "=== ERROR: Pull failed using GIT `" . GIT . "` and DIR `" . DIR . "` ===\n" . $output;
}
// write the output to the log and the body
fputs($file, $output);
echo $output;
/**
* Attempt to checkout specific hash if specified
*/
if (!empty($sha)) {
// write to the log
fputs($file, "*** RESET TO HASH INITIATED ***" . "\n");
exec(GIT . " reset --hard {$sha} 2>&1", $output, $exit);
// reformat the output as a string
$output = (!empty($output) ? implode("\n", $output) : "[no output]") . "\n";
// if an error occurred, return 500 and log the error
if ($exit !== 0) {
http_response_code(500);
$output = "=== ERROR: Reset failed using GIT `" . GIT . "` and \$sha `" . $sha . "` ===\n" . $output;
}
// write the output to the log and the body
fputs($file, $output);
echo $output;
}
/**
* Attempt to execute AFTER_PULL if specified
*/
if (!empty(AFTER_PULL)) {
// write to the log
fputs($file, "*** AFTER_PULL INITIATED ***" . "\n");
// execute the command, returning the output and exit code
exec(AFTER_PULL . " 2>&1", $output, $exit);
// reformat the output as a string
$output = (!empty($output) ? implode("\n", $output) : "[no output]") . "\n";
// if an error occurred, return 500 and log the error
if ($exit !== 0) {
http_response_code(500);
$output = "=== ERROR: AFTER_PULL `" . AFTER_PULL . "` failed ===\n" . $output;
}
// write the output to the log and the body
fputs($file, $output);
echo $output;
}
// write to the log
fputs($file, "*** AUTO PULL COMPLETE ***" . "\n");
} else {
// prepare the generic error
$error = "=== ERROR: DIR `" . DIR . "` is not a repository ===\n";
// try to detemrine the real error
if (!file_exists(DIR)) {
$error = "=== ERROR: DIR `" . DIR . "` does not exist ===\n";
} elseif (!is_dir(DIR)) {
$error = "=== ERROR: DIR `" . DIR . "` is not a directory ===\n";
}
// bad request
http_response_code(400);
// write the error to the log and the body
fputs($file, $error);
echo $error;
}
} else{
$error = "=== ERROR: Pushed branch `" . $json["ref"] . "` does not match BRANCH `" . BRANCH . "` ===\n";
// bad request
http_response_code(400);
// write the error to the log and the body
fputs($file, $error);
echo $error;
}
}
// close the log
fputs($file, "\n\n" . PHP_EOL);
fclose($file);