forked from Lyquix/php-git-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.php
353 lines (323 loc) · 9.86 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/*
* php-git-deploy
* PHP script for automatic code deployment directly from Github or Bitbucket to your server using webhooks
* Documentation: https://github.com/Lyquix/php-git-deploy
*/
// Check if lock file exists
if (file_exists(__DIR__ . '/deploy.lock')) {
die('File deploy.lock detected, another process already running');
}
// Create lock file
$fh = fopen(__DIR__ . '/deploy.lock', 'w');
fclose($fh);
// Remove lock file
function removeLockFile() {
unlink(__DIR__ . '/deploy.lock');
}
// Check if there is a configuration file
if (file_exists(__DIR__ . '/deploy-config.php')) {
require_once __DIR__ . '/deploy-config.php';
} else {
removeLockFile();
die('File deploy-config.php does not exist');
}
// Check configuration errors
$err = array();
if (!defined('ACCESS_TOKEN')) $err[] = 'Access token is not configured';
if (!defined('REMOTE_REPOSITORY')) $err[] = 'Remote repository is not configured';
if (!defined('BRANCH')) $err[] = 'Branch is not configured';
if (!defined('GIT_DIR')) $err[] = 'Git directory is not configured';
if (!defined('TARGET_DIR')) $err[] = 'Target directory is not configured';
if (!defined('TIME_LIMIT')) define('TIME_LIMIT', 60);
// If there's authorization error, set the correct HTTP header.
if (!isset($_GET['t']) || $_GET['t'] !== ACCESS_TOKEN || ACCESS_TOKEN === '') {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403);
}
// Prevent caching
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex">
<title>PHP Git deploy script</title>
<style>
body { padding: 0 1em; background: #222; color: #fff; }
h2, .error { color: #c33; }
.prompt { color: #6be234; }
.command { color: #729fcf; }
.output { color: #999; }
</style>
</head>
<body>
<?php
if (!isset($_GET['t']) || $_GET['t'] !== ACCESS_TOKEN) {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403);
removeLockFile();
die('<h2>Access Denied</h2>');
}
if (count($err) || ACCESS_TOKEN === '' || REMOTE_REPOSITORY === '' || BRANCH === '' || GIT_DIR === '' || TARGET_DIR === '') {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403);
removeLockFile();
die("<h2>Configuration Error</h2>\n<pre>\n" . implode("\n", $err) . "\n</pre>");
}
?>
<pre>
<?php
// The branch
$branch = '';
// Process request headers
$headers = getallheaders();
if(isset($headers['X-Event-Key'])) {
// Bitbucket webhook
echo "\nBitbucket headers detected\n";
// Get payload
$payload = json_decode(file_get_contents('php://input'));
// Accept only push and pull request merge events
if($headers['X-Event-Key'] == 'repo:push') {
// Check branch
$branch = $payload->push->changes[0]->new->name;
} else if($headers['X-Event-Key'] == 'pullrequest:fulfilled') {
// Check branch
$branch = $payload->pullrequest->destination->branch->name;
} else {
echo "\nOnly push and merged pull request events are processed\n\nDone.\n</pre></body></html>";
removeLockFile();
exit;
}
} else if(isset($headers['X-GitHub-Event'])) {
// Github webhook
echo "\nGithub headers detected\n";
// Get payload
if($headers['Content-Type'] == 'application/json') $payload = json_decode(file_get_contents('php://input'));
else $payload = json_decode($_POST['payload']);
// Accept only push and pull request merge events
if($headers['X-GitHub-Event'] == 'push') {
// Check branch
$branch = explode('/', $payload->ref)[2];
} else if($headers['X-GitHub-Event'] == 'pull_request' && $payload->action == 'closed' && $payload->pull_request->merged == true) {
// Check branch
$branch = $payload->pull_request->head->ref;
} else {
echo "\nOnly push and merged pull request events are processed\n\nDone.\n</pre></body></html>";
removeLockFile();
exit;
}
}
// Branch from webhook?
if($branch) {
// Only main branch is allowed for webhook deployments
if($branch != unserialize(BRANCH)[0]) {
echo "\nBranch $branch not allowed, stopping execution.\n</pre></body></html>";
removeLockFile();
exit;
}
} else {
echo "\nNo Bitbucket or Github webhook headers detected. Assumming manual trigger.\n";
if(isset($_GET['b'])) {
$branch = $_GET['b'];
// Check if branch is allowed
if(!in_array($branch, unserialize(BRANCH))) {
echo "\nBranch $branch not allowed, stopping execution.\n</pre></body></html>";
removeLockFile();
exit;
}
} else {
$branch = unserialize(BRANCH)[0];
echo "No branch specified, assuming default branch $branch\n";
}
}
?>
Checking the environment ...
Running as <strong><?php echo trim(shell_exec('whoami')); ?></strong>.
<?php
// Check if the required programs are available
$requiredBinaries = array('git', 'rsync');
foreach ($requiredBinaries as $command) {
$path = trim(shell_exec('which '.$command));
if ($path == '') {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
removeLockFile();
die(sprintf('<div class="error"><b>%s</b> not available. It needs to be installed on the server for this script to work.</div>', $command));
} else {
$version = explode("\n", shell_exec($command.' --version'));
printf('<b>%s</b> : %s'."\n"
, $path
, $version[0]
);
}
}
?>
Environment OK.
Deploying : <?php echo REMOTE_REPOSITORY; ?> (<?php echo $branch; ?>)
to : <?php echo TARGET_DIR; ?>
<?php
// Runs shell commands in Git directory, outputs command and result
function cmd($command, $print = true) {
set_time_limit(TIME_LIMIT); // Reset the time limit for each command
if (file_exists(GIT_DIR) && is_dir(GIT_DIR)) {
chdir(GIT_DIR); // Ensure that we're in the right directory
}
$tmp = array();
exec($command.' 2>&1', $tmp, $return_code); // Execute the command
// Output the result
if($print) {
printf('
<span class="prompt">$</span> <span class="command">%s</span>
<span class="output">%s</span>
'
, htmlentities(trim($command))
, htmlentities(trim(implode("\n", $tmp)))
);
}
// Error handling and cleanup
if($print && $return_code !== 0) {
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
printf('<span class="error">Error encountered! Stopping the script to prevent possible data loss.
CHECK THE DATA IN YOUR TARGET DIR!</span>
'
);
break;
}
return $tmp;
}
// The commits
$commits = array();
// The checkout commit
$checkout = '';
// The current files version
$version = '';
// Check if there is a git directory
if (!is_dir(GIT_DIR)) {
// Clone the repository into the GIT_DIR
echo "\nGit directory not found, cloning repository\n";
cmd(sprintf(
'git clone --branch %s %s %s'
, $branch
, REMOTE_REPOSITORY
, GIT_DIR
));
// Checkout branch
echo "\nCheckout branch $branch\n";
cmd(sprintf(
'git --git-dir="%s.git" --work-tree="%s" checkout %s'
, GIT_DIR
, GIT_DIR
, $branch
));
} else {
// GIT_DIR exists and hopefully already contains the correct remote origin
// so we'll fetch the changes
// Checkout branch
echo "\nCheckout branch $branch\n";
cmd(sprintf(
'git --git-dir="%s.git" --work-tree="%s" checkout %s'
, GIT_DIR
, GIT_DIR
, $branch
));
echo "\nFetching repository from origin\n";
cmd(sprintf(
'git --git-dir="%s.git" --work-tree="%s" fetch --tags origin %s'
, GIT_DIR
, GIT_DIR
, $branch
));
}
// Get list of all commits
$commits = cmd(sprintf(
'git --no-pager --git-dir="%s.git" log --pretty=format:"%%h" origin/%s'
, GIT_DIR
, $branch)
, false);
// Set checkout commit
if(in_array($_GET['c'], $commits)) {
$checkout = $_GET['c'];
} else {
$checkout = reset($commits);
echo "\nPassed commit hash is blank or doesn't match existing commits. Assuming most recent commit in branch: $checkout\n";
}
// Checkout specific commit
echo "\nReset branch to commit $checkout in git directory\n";
cmd(sprintf(
'git --git-dir="%s.git" --work-tree="%s" reset --hard %s'
, GIT_DIR
, GIT_DIR
, $checkout
));
// Update the submodules
echo "\nUpdating git submodules in git directory\n";
cmd('git submodule update --init --recursive');
// Get current version or assume oldest commit
if(file_exists(TARGET_DIR . 'VERSION')) {
$version = trim(file_get_contents(TARGET_DIR . 'VERSION'));
if(!in_array($version, $commits)) {
$version = end($commits);
echo "WARNING: version file commit hash doesn't match existing commits, assuming oldest commit $version\n";
} else echo "Current target directory version is $version\n";
}
else {
$version = end($commits);
echo "No version file found, assuming current version is oldest commit\n";
}
// Get list of added, modified and deleted files
echo "\nGet list of files added, modified and deleted from $version to $checkout\n";
$files = cmd(sprintf(
'git --no-pager --git-dir="%s.git" diff --name-status %s %s'
, GIT_DIR
, $version
, $checkout
));
// Count files that were added or modified. Add removed files to array.
$added = $modified = 0;
$deleted = array();
foreach($files as $file) {
if(preg_match('/^([ADM])\s*(.*)/', $file, $matches)) {
switch($matches[1]) {
case 'A':
$added++;
break;
case 'M':
$modified++;
break;
case 'D':
$deleted[] = TARGET_DIR . $matches[2];
break;
}
}
}
printf(
"\nDeploying %d files:\n Added %d\n Modified %d\n Deleted %d\n"
, count($files)
, $added
, $modified
, count($deleted)
);
echo "\nNOTE: repository files that have been modfied or removed in target directory will be resynced with repository even if not listed in commits\n";
// rsync all added and modified files (no deletes, exclude .git directory)
cmd(sprintf(
'rsync -rltgoDzvO %s %s --exclude=.git'
, GIT_DIR
, TARGET_DIR
));
echo "\nDeleting files removed from repository\n";
// Delete files removed in commits
foreach($deleted as $file) unlink($file);
// Update version file to current commit
echo "\nUpdate target directory version file to commit $checkout\n";
cmd(sprintf(
'echo "%s" > %s'
, $checkout
, TARGET_DIR . 'VERSION'
));
// Remove lock file
removeLockFile();
?>
Done.
</pre>
</body>
</html>