Skip to content

Commit

Permalink
Merge pull request #4 from SagePtr/master
Browse files Browse the repository at this point in the history
Added lot of options for advanced deployment scenario
  • Loading branch information
lyquix-owner authored Aug 17, 2018
2 parents 4497a46 + 81a4490 commit 6189a40
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ 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` .
* __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.

Expand Down
42 changes: 42 additions & 0 deletions deploy-config.orig.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,45 @@

/* 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');

/* 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()));

/* 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);
44 changes: 39 additions & 5 deletions deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -254,10 +256,10 @@ function endScript() {

<?php
// Runs shell commands in Git directory, outputs command and result
function cmd($command, $print = true) {
function cmd($command, $print = true, $dir = GIT_DIR) {
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
if (file_exists($dir) && is_dir($dir)) {
chdir($dir); // Ensure that we're in the right directory
}
$tmp = array();
exec($command.' 2>&1', $tmp, $return_code); // Execute the command
Expand Down Expand Up @@ -413,17 +415,49 @@ 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)
// 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, '');

// 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";

// 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);
}
}

// 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(
Expand Down

0 comments on commit 6189a40

Please # to comment.