@@ -26,6 +26,8 @@ function removeLockFile() {
26
26
removeLockFile ();
27
27
die ('File deploy-config.php does not exist ' );
28
28
}
29
+
30
+ // Check configuration errors
29
31
$ err = array ();
30
32
if (!defined ('ACCESS_TOKEN ' )) $ err [] = 'Access token is not configured ' ;
31
33
if (!defined ('REMOTE_REPOSITORY ' )) $ err [] = 'Remote repository is not configured ' ;
@@ -38,6 +40,7 @@ function removeLockFile() {
38
40
if (!isset ($ _GET ['t ' ]) || $ _GET ['t ' ] !== ACCESS_TOKEN || ACCESS_TOKEN === '' ) {
39
41
header ($ _SERVER ['SERVER_PROTOCOL ' ] . ' 403 Forbidden ' , true , 403 );
40
42
}
43
+
41
44
// Prevent caching
42
45
header ("Cache-Control: no-store, no-cache, must-revalidate, max-age=0 " );
43
46
header ("Cache-Control: post-check=0, pre-check=0 " , false );
@@ -74,6 +77,7 @@ function removeLockFile() {
74
77
<?php
75
78
// The branch
76
79
$ branch = '' ;
80
+
77
81
// Process request headers
78
82
$ headers = getallheaders ();
79
83
if (isset ($ headers ['X-Event-Key ' ])) {
@@ -111,7 +115,8 @@ function removeLockFile() {
111
115
removeLockFile ();
112
116
exit ;
113
117
}
114
- }
118
+ }
119
+
115
120
// Branch from webhook?
116
121
if ($ branch ) {
117
122
// Only main branch is allowed for webhook deployments
@@ -197,10 +202,14 @@ function cmd($command, $print = true) {
197
202
198
203
// The commits
199
204
$ commits = array ();
205
+
200
206
// The checkout commit
201
207
$ checkout = '' ;
208
+
202
209
// The current files version
203
210
$ version = '' ;
211
+
212
+ // Check if there is a git directory
204
213
if (!is_dir (GIT_DIR )) {
205
214
// Clone the repository into the GIT_DIR
206
215
echo "\nGit directory not found, cloning repository \n" ;
@@ -237,19 +246,22 @@ function cmd($command, $print = true) {
237
246
, $ branch
238
247
));
239
248
}
249
+
240
250
// Get list of all commits
241
251
$ commits = cmd (sprintf (
242
252
'git --no-pager --git-dir="%s.git" log --pretty=format:"%%h" origin/%s '
243
253
, GIT_DIR
244
254
, $ branch )
245
255
, false );
256
+
246
257
// Set checkout commit
247
258
if (in_array ($ _GET ['c ' ], $ commits )) {
248
259
$ checkout = $ _GET ['c ' ];
249
260
} else {
250
261
$ checkout = reset ($ commits );
251
262
echo "\nPassed commit hash is blank or doesn't match existing commits. Assuming most recent commit in branch: $ checkout \n" ;
252
263
}
264
+
253
265
// Checkout specific commit
254
266
echo "\nReset branch to commit $ checkout in git directory \n" ;
255
267
cmd (sprintf (
@@ -258,9 +270,11 @@ function cmd($command, $print = true) {
258
270
, GIT_DIR
259
271
, $ checkout
260
272
));
273
+
261
274
// Update the submodules
262
275
echo "\nUpdating git submodules in git directory \n" ;
263
276
cmd ('git submodule update --init --recursive ' );
277
+
264
278
// Get current version or assume oldest commit
265
279
if (file_exists (TARGET_DIR . 'VERSION ' )) {
266
280
$ version = trim (file_get_contents (TARGET_DIR . 'VERSION ' ));
@@ -273,6 +287,7 @@ function cmd($command, $print = true) {
273
287
$ version = end ($ commits );
274
288
echo "No version file found, assuming current version is oldest commit \n" ;
275
289
}
290
+
276
291
// Get list of added, modified and deleted files
277
292
echo "\nGet list of files added, modified and deleted from $ version to $ checkout \n" ;
278
293
$ files = cmd (sprintf (
@@ -281,6 +296,7 @@ function cmd($command, $print = true) {
281
296
, $ version
282
297
, $ checkout
283
298
));
299
+
284
300
// Count files that were added or modified. Add removed files to array.
285
301
$ added = $ modified = 0 ;
286
302
$ deleted = array ();
@@ -307,15 +323,18 @@ function cmd($command, $print = true) {
307
323
, count ($ deleted )
308
324
);
309
325
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" ;
326
+
310
327
// rsync all added and modified files (no deletes, exclude .git directory)
311
328
cmd (sprintf (
312
329
'rsync -rltgoDzvO %s %s --exclude=.git '
313
330
, GIT_DIR
314
331
, TARGET_DIR
315
332
));
316
333
echo "\nDeleting files removed from repository \n" ;
334
+
317
335
// Delete files removed in commits
318
336
foreach ($ deleted as $ file ) unlink ($ file );
337
+
319
338
// Update version file to current commit
320
339
echo "\nUpdate target directory version file to commit $ checkout \n" ;
321
340
cmd (sprintf (
0 commit comments