-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployer.php
177 lines (140 loc) · 5.69 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
<?php
$content = file_get_contents("php://input");
$json = json_decode($content, true);
$file = fopen(LOGFILE, "a");
$time = time();
$sha = false;
$DIR = preg_match("/\/$/", DIR) ? DIR : DIR . "/";
// 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);
}
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, returning 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 determine 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);