From a52fefe51b0c015de5fc1c99d5259c338a4086ec Mon Sep 17 00:00:00 2001 From: SagePtr Date: Thu, 21 Jun 2018 15:31:16 +0200 Subject: [PATCH 1/3] Add EXCLUDE_FILES, RSYNC_FLAGS options EXCLUDE_FILES - specify files should not be copied to TARGET_DIR, default: .git RSYNC_FLAGS - specify flags for rsync commands, default: -rltgoDzvO If any of these options is not defined, default value will be used instead (the same as hardcoded before this commit) --- README.md | 2 ++ deploy-config.orig.php | 18 ++++++++++++++++++ deploy.php | 12 ++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0376517..7974d3d 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,8 @@ If you are using a public repository you can start here. * __LOG_FILE__: (optional) the full path of file to log all script output * __EMAIL_NOTIFICATIONS__: (optional) email address to which a copy of the script output will be sent * __TIME_LIMIT__: maximum time allowed for each command, in seconds. 60 should be fine unless your deployments are massive. Adjust if necessary. + * __EXCLUDE_FILES__: (optional) array of files or filename patterns which won't be copied to `TARGET_DIR` . By default it's `.git`. + * __RSYNC_FLAGS__: (optional) override rsync flags. By default, it's `-rltgoDzvO` . NOTE: do not include/track the files `deploy-config.php` and `VERSION` in your repository. diff --git a/deploy-config.orig.php b/deploy-config.orig.php index 85cba13..385ea20 100644 --- a/deploy-config.orig.php +++ b/deploy-config.orig.php @@ -64,3 +64,21 @@ /* TIME_LIMIT: Time limit for each command */ define('TIME_LIMIT', 60); + +/* EXCLUDE_FILES: + * Array of files excluded from rsync (they will appear in GIT_DIR, but not in TARGET_DIR) + * By default, only .git directory is excluded. + * It's recommended to leave '.git' excluded and add something more if needed. + * Example: define('EXCLUDE_FILES', serialize(array('.git', '.gitignore', '*.less', '*.scss'))); + * + */ +define('EXCLUDE_FILES', serialize(array('.git'))); + +/* RSYNC_FLAGS: + * Custom flags to run rsync with + * Default: '-rltgoDzvO' + * Do not change them if not necessary + * Example: '-rltDzvO' (don't changes owner:group of copied files, + * useful for vhosts than require separate group for document_root to be accessible by webserver) + */ +define('RSYNC_FLAGS', '-rltgoDzvO'); diff --git a/deploy.php b/deploy.php index f2a167d..46bc619 100644 --- a/deploy.php +++ b/deploy.php @@ -81,6 +81,8 @@ function endScript() { if (!defined('GIT_DIR') || GIT_DIR === '') $err[] = 'Git directory is not configured'; if (!defined('TARGET_DIR') || TARGET_DIR === '') $err[] = 'Target directory is not configured'; if (!defined('TIME_LIMIT')) define('TIME_LIMIT', 60); +if (!defined('EXCLUDE_FILES')) define('EXCLUDE_FILES', serialize(array('.git'))); +if (!defined('RSYNC_FLAGS')) define('RSYNC_FLAGS', '-rltgoDzvO'); // If there is a configuration error if (count($err)) { @@ -413,11 +415,17 @@ function cmd($command, $print = true) { ); 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) +// Build exclusion list +$exclude = unserialize(EXCLUDE_FILES); +array_unshift($exclude, ''); + +// rsync all added and modified files (by default: no deletes, exclude .git directory) cmd(sprintf( - 'rsync -rltgoDzvO %s %s --exclude=.git' + 'rsync %s %s %s %s' + , RSYNC_FLAGS , GIT_DIR , TARGET_DIR + , implode(' --exclude=', $exclude) )); echo "\nDeleting files removed from repository\n"; From 2a3d15e04e2088169bed0f9b04243f1dec2874cb Mon Sep 17 00:00:00 2001 From: SagePtr Date: Thu, 21 Jun 2018 17:28:25 +0200 Subject: [PATCH 2/3] Add custom commands options COMMANDS_BEFORE_RSYNC - these commands will be run before checkout and rsync (useful for running build, etc) COMMANDS_AFTER_RSYNC - these commands will be run after rsync (useful for cleaning cache folder on server, etc) Doesn't break existing configs if they lack defines --- README.md | 2 ++ deploy-config.orig.php | 16 ++++++++++++++++ deploy.php | 22 +++++++++++++++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7974d3d..9623322 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,8 @@ If you are using a public repository you can start here. * __TIME_LIMIT__: maximum time allowed for each command, in seconds. 60 should be fine unless your deployments are massive. Adjust if necessary. * __EXCLUDE_FILES__: (optional) array of files or filename patterns which won't be copied to `TARGET_DIR` . By default it's `.git`. * __RSYNC_FLAGS__: (optional) override rsync flags. By default, it's `-rltgoDzvO` . + * __COMMANDS_BEFORE_RSYNC__: (optional) array of commands executed between pulling remote repository and copying files to target directory. These commands are executed under `GIT_DIR` directory. + * __COMMANDS_AFTER_RSYNC__: (optional) array of commands executed after copying files to target directory. These commands are executed under `TARGET_DIR` directory. NOTE: do not include/track the files `deploy-config.php` and `VERSION` in your repository. diff --git a/deploy-config.orig.php b/deploy-config.orig.php index 385ea20..47a8e47 100644 --- a/deploy-config.orig.php +++ b/deploy-config.orig.php @@ -82,3 +82,19 @@ * useful for vhosts than require separate group for document_root to be accessible by webserver) */ define('RSYNC_FLAGS', '-rltgoDzvO'); + +/* COMMANDS_BEFORE_RSYNC: + * Run commands before running rsync. Default: empty array + * This commands will be run under GIT_DIR after checkout from remote repository + * Useful for running build tasks + * Example: define('COMMANDS_BEFORE_RSYNC', serialize(array('composer install'))); + */ +define('COMMANDS_BEFORE_RSYNC', serialize(array())); + +/* COMMANDS_AFTER_RSYNC: + * Run commands after running rsync. Default: empty array + * This commands will be run under TARGET_DIR after copying files from GIT_DIR + * Useful for doing some cleanups + * Example: define('COMMANDS_AFTER_RSYNC', serialize(array('rm cache/*.php -f'))); + */ +define('COMMANDS_AFTER_RSYNC', serialize(array())); diff --git a/deploy.php b/deploy.php index 46bc619..f7ee478 100644 --- a/deploy.php +++ b/deploy.php @@ -256,10 +256,10 @@ function endScript() { &1', $tmp, $return_code); // Execute the command @@ -415,6 +415,14 @@ function cmd($command, $print = true) { ); 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"; +// Run before rsync commands +if(defined('COMMANDS_BEFORE_RSYNC') && count(unserialize(COMMANDS_BEFORE_RSYNC))) { + echo "\nRunning before rsync commands\n"; + foreach(unserialize(COMMANDS_BEFORE_RSYNC) as $command) { + cmd($command); + } +} + // Build exclusion list $exclude = unserialize(EXCLUDE_FILES); array_unshift($exclude, ''); @@ -432,6 +440,14 @@ function cmd($command, $print = true) { // Delete files removed in commits foreach($deleted as $file) unlink($file); +// Run after rsync commands +if(defined('COMMANDS_AFTER_RSYNC') && count(unserialize(COMMANDS_AFTER_RSYNC))) { + echo "\nRunning after rsync commands\n"; + foreach(unserialize(COMMANDS_AFTER_RSYNC) as $command) { + cmd($command, true, TARGET_DIR); + } +} + // Update version file to current commit echo "\nUpdate target directory version file to commit $checkout\n"; cmd(sprintf( From 81a449090a358fd1b83c69e6ca251bd565a3cad7 Mon Sep 17 00:00:00 2001 From: SagePtr Date: Thu, 21 Jun 2018 18:00:30 +0200 Subject: [PATCH 3/3] Add cleanup work tree option Cleans git work tree after running custom commands. Does not clean TARGET_DIR --- README.md | 1 + deploy-config.orig.php | 8 ++++++++ deploy.php | 10 ++++++++++ 3 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 9623322..22c1574 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ If you are using a public repository you can start here. * __RSYNC_FLAGS__: (optional) override rsync flags. By default, it's `-rltgoDzvO` . * __COMMANDS_BEFORE_RSYNC__: (optional) array of commands executed between pulling remote repository and copying files to target directory. These commands are executed under `GIT_DIR` directory. * __COMMANDS_AFTER_RSYNC__: (optional) array of commands executed after copying files to target directory. These commands are executed under `TARGET_DIR` directory. + * __CLEANUP_WORK_TREE__: (optional) set to `true` if you want to clean `GIT_DIR` from intermediate files created by custom commands and rebuild project from scratch every time. Does not affect `TARGET_DIR` at all. NOTE: do not include/track the files `deploy-config.php` and `VERSION` in your repository. diff --git a/deploy-config.orig.php b/deploy-config.orig.php index 47a8e47..b6d580a 100644 --- a/deploy-config.orig.php +++ b/deploy-config.orig.php @@ -98,3 +98,11 @@ * Example: define('COMMANDS_AFTER_RSYNC', serialize(array('rm cache/*.php -f'))); */ define('COMMANDS_AFTER_RSYNC', serialize(array())); + +/* CLEANUP_WORK_TREE: + * Clean GIT_DIR from leftovers after custom commands + * Set to true if you wish to clean up GIT_DIR after running all custom commands + * Useful if your custom commands create intermediate files you want not to keep between deployments + * However, intermediate files would not be cleaned up from TARGET_DIR + */ +define('CLEANUP_WORK_TREE', false); diff --git a/deploy.php b/deploy.php index f7ee478..0feeb4f 100644 --- a/deploy.php +++ b/deploy.php @@ -448,6 +448,16 @@ function cmd($command, $print = true, $dir = GIT_DIR) { } } +// Cleanup work tree from build results, etc +if(defined('CLEANUP_WORK_TREE') && !empty(CLEANUP_WORK_TREE)){ + echo "\nCleanup work tree\n"; + cmd(sprintf( + 'git --git-dir="%s.git" --work-tree="%s" clean -dfx' + , GIT_DIR + , GIT_DIR + )); +} + // Update version file to current commit echo "\nUpdate target directory version file to commit $checkout\n"; cmd(sprintf(