Skip to content

Commit 07155c8

Browse files
Pass error messages to endScript()
1 parent efc488c commit 07155c8

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

‎deploy.php

+39-31
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ function errorPage($msg) {
2424
. $msg . "\n"
2525
. "</body>\n</html>\n"
2626
. "<!--\n~~~~~~~~~~~~~ Prevent browser friendly error page ~~~~~~~~~~~~~~\n"
27-
. str_repeat(str_repeat("~", 64) . "\n", 8)
27+
. str_repeat(str_repeat("~", 64) . "\n", 8)
2828
. "-->\n";
2929
}
3030

3131
// Command to execute at the end of the script
32-
function endScript($errorMessage = "") {
32+
function endScript($msg = "") {
3333
// Remove lock file
3434
unlink(__DIR__ . '/deploy.lock');
3535
// Flush buffer and prepare output for log and email
@@ -51,7 +51,7 @@ function endScript($errorMessage = "") {
5151
if(defined('LOG_FILE') && LOG_FILE !== '') error_log($output, 3, LOG_FILE);
5252
// Send email notification
5353
if(defined('EMAIL_NOTIFICATIONS') && EMAIL_NOTIFICATIONS !== '') error_log($output, 1, EMAIL_NOTIFICATIONS);
54-
die($errorMessage);
54+
die($msg);
5555
}
5656

5757
/* Begin Script Execution */
@@ -69,8 +69,9 @@ function endScript($errorMessage = "") {
6969
if (file_exists(__DIR__ . '/deploy-config.php')) {
7070
require_once __DIR__ . '/deploy-config.php';
7171
} else {
72-
errorPage('<h2>File deploy-config.php does not exist</h2>');
73-
endScript();
72+
$msg = 'File deploy-config.php does not exist';
73+
errorPage('<h2>'. $msg . '</h2>');
74+
endScript($msg);
7475
}
7576

7677
// Check configuration errors
@@ -87,13 +88,14 @@ function endScript($errorMessage = "") {
8788
// If there is a configuration error
8889
if (count($err)) {
8990
errorPage("<h2>Configuration Error</h2>\n<pre>\n" . implode("\n", $err) . "\n</pre>");
90-
endScript();
91+
endScript("Configuration Error:\n\n" . implode("\n", $err));
9192
}
9293

9394
// Check if lock file exists
9495
if (file_exists(__DIR__ . '/deploy.lock')) {
95-
errorPage('<h2>File deploy.lock detected, another process already running</h2>');
96-
endScript();
96+
$msg = 'File deploy.lock detected, another process already running';
97+
errorPage('<h2>' . $msg . '</h2>');
98+
endScript($msg);
9799
}
98100

99101
// Create lock file
@@ -129,15 +131,17 @@ function endScript($errorMessage = "") {
129131
}
130132
}
131133
if(!$allow) {
132-
errorPage('<h2>Access Denied</h2>');
133-
endScript();
134+
$msg = 'Access Denied';
135+
errorPage('<h2>' . $msg . '</h2>');
136+
endScript($msg);
134137
}
135138
}
136139

137140
// If there's authorization error
138141
if (!isset($_GET['t']) || $_GET['t'] !== ACCESS_TOKEN || DISABLED === true) {
139-
errorPage('<h2>Access Denied</h2>');
140-
endScript();
142+
$msg = 'Access Denied';
143+
errorPage('<h2>' . $msg . '</h2>');
144+
endScript($msg);
141145
}
142146
?>
143147
<!DOCTYPE html>
@@ -187,8 +191,9 @@ function getallheaders() {
187191
// Check branch
188192
$branch = $payload->pullrequest->destination->branch->name;
189193
} else {
190-
echo "\nOnly push and merged pull request events are processed\n\nDone.\n</pre></body></html>";
191-
endScript();
194+
$msg = 'Only push and merged pull request events are processed';
195+
echo "\n" . $msg . "\n\nDone.\n</pre></body></html>";
196+
endScript($msg);
192197
}
193198
} else if(isset($headers['X-GitHub-Event'])) {
194199
// Github webhook
@@ -204,17 +209,19 @@ function getallheaders() {
204209
// Check branch
205210
$branch = $payload->pull_request->head->ref;
206211
} else {
207-
echo "\nOnly push and merged pull request events are processed\n\nDone.\n</pre></body></html>";
208-
endScript();
212+
$msg = 'Only push and merged pull request events are processed';
213+
echo "\n" . $msg . "\n\nDone.\n</pre></body></html>";
214+
endScript($msg);
209215
}
210216
}
211217

212218
// Branch from webhook?
213219
if($branch) {
214220
// Only main branch is allowed for webhook deployments
215221
if($branch != unserialize(BRANCH)[0]) {
216-
echo "\nBranch $branch not allowed, stopping execution.\n</pre></body></html>";
217-
endScript();
222+
$msg = 'Branch ' . $branch . ' not allowed, stopping execution.';
223+
echo "\n" . $msg . "\n</pre></body></html>";
224+
endScript($msg);
218225
}
219226

220227
} else {
@@ -223,9 +230,10 @@ function getallheaders() {
223230
$branch = $_GET['b'];
224231
// Check if branch is allowed
225232
if(!in_array($branch, unserialize(BRANCH))) {
226-
echo "\nBranch $branch not allowed, stopping execution.\n</pre></body></html>";
227-
endScript();
228-
}
233+
$msg = 'Branch ' . $branch . ' not allowed, stopping execution.';
234+
echo "\n" . $msg . "\n</pre></body></html>";
235+
endScript($msg);
236+
}
229237
} else {
230238
$branch = unserialize(BRANCH)[0];
231239
echo "No branch specified, assuming default branch $branch\n";
@@ -241,7 +249,9 @@ function getallheaders() {
241249
$path = trim(shell_exec('which '.$command));
242250
if ($path == '') {
243251
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
244-
endScript(sprintf('<div class="error"><b>%s</b> not available. It needs to be installed on the server for this script to work.</div>', $command));
252+
$msg = $command . ' not available. It needs to be installed on the server for this script to work.';
253+
echo $msg;
254+
endScript($msg);
245255
} else {
246256
$version = explode("\n", shell_exec($command.' --version'));
247257
printf('<b>%s</b> : %s'."\n"
@@ -279,11 +289,9 @@ function cmd($command, $print = true, $dir = GIT_DIR) {
279289
// Error handling and cleanup
280290
if($print && $return_code !== 0) {
281291
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
282-
printf('<span class="error">Error encountered! Stopping the script to prevent possible data loss.
283-
CHECK THE DATA IN YOUR TARGET DIR!</span>
284-
'
285-
);
286-
endScript();
292+
$msg = "Error encountered! Stopping the script to prevent possible data loss.\nCHECK THE DATA IN YOUR TARGET DIR!";
293+
echo '<span class="error">' . $msg . '</span>';
294+
endScript($msg);
287295
}
288296

289297
return $tmp;
@@ -358,15 +366,15 @@ function cmd($command, $print = true, $dir = GIT_DIR) {
358366
, GIT_DIR
359367
, GIT_DIR
360368
, $checkout
361-
));
369+
));
362370

363371
// Update the submodules
364372
echo "\nUpdating git submodules in git directory\n";
365373
cmd(sprintf(
366374
'git --git-dir="%s.git" --work-tree="%s" submodule update --init --recursive'
367375
, GIT_DIR
368376
, GIT_DIR
369-
));
377+
));
370378

371379
// Get current version or assume oldest commit
372380
if(file_exists(TARGET_DIR . 'VERSION')) {
@@ -473,5 +481,5 @@ function cmd($command, $print = true, $dir = GIT_DIR) {
473481
</pre>
474482
</body>
475483
</html>
476-
<?php
477-
endScript();
484+
<?php
485+
endScript();

0 commit comments

Comments
 (0)