Skip to content

Commit

Permalink
Add check_remote_head option to avoid unnecessary new releases by che… (
Browse files Browse the repository at this point in the history
#1759)

* Add check_remote_head option to avoid unnecessary new releases by checking the remote git HEAD without cloning the repo.

* Add check_remote_head into the CHANGELOG

* Add the pull request link to CHANGELOG

* Update CHANGELOG.md

Removed extra spaces: #1759

* Fix changelog. Improve check_remote_head

* Use GrasefulSutdown exception instead of exit(0).

* Change bash if statement in check_remote_head.

* Remove redundant code.

* Replace if statement with test.
  • Loading branch information
ahmadmayahi authored and antonmedv committed Aug 6, 2019
1 parent 86c407b commit bb4d41c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
- Support to define remote shell path via host-config [#1708] [#1709] [#1709]
- Added `horizon:terminate` to the Laravel recipe
- Added `migrations_config` option to the Symfony recipes to specify Doctrine migration configuration to use
- Added `check_remote_head` option, by setting this to true, deployer will avoid unnecessary new releases by checking the remote git HEAD without cloning the repo [#1755]
- Added recipe for sulu 2.0 [#1758]
- Added recipe for sulu 1.x and improve sulu 2.0 recipe [#1764]
- Added `become` option for rsync upload
Expand Down Expand Up @@ -501,6 +502,7 @@
[#1775]: https://github.com/deployphp/deployer/pull/1775
[#1764]: https://github.com/deployphp/deployer/pull/1764
[#1758]: https://github.com/deployphp/deployer/pull/1758
[#1755]: https://github.com/deployphp/deployer/pull/1755
[#1709]: https://github.com/deployphp/deployer/issues/1709
[#1708]: https://github.com/deployphp/deployer/pull/1708
[#1677]: https://github.com/deployphp/deployer/pull/1677
Expand Down
2 changes: 2 additions & 0 deletions recipe/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
*/

set('keep_releases', 5);
// By setting this to true, deployer will avoid unnecessary new release by checking the remote git HEAD without cloning the repo.
set('check_remote_head', false);

set('repository', ''); // Repository to deploy.

Expand Down
17 changes: 17 additions & 0 deletions recipe/deploy/prepare.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Deployer;

use Deployer\Exception\GracefulShutdownException;
use function Deployer\Support\str_contains;

desc('Preparing host for deploy');
Expand Down Expand Up @@ -36,4 +37,20 @@

// Create shared dir.
run("cd {{deploy_path}} && if [ ! -d shared ]; then mkdir shared; fi");

// Check and save the remote HEAD/revision and compare it with the existing saved one (if any)
// This avoid unnecessary releases when the last commit id matches the existing one (HEAD)
$repository = trim(get('repository'));
$revision = input()->getOption('revision') ?? null;
$remoteHead = $revision ?? run(sprintf('%s ls-remote %s HEAD | tr -d "HEAD"', get('bin/git'), $repository));

if (true === get('check_remote_head') && null == input()->getOption('tag')) {
$headPath = trim(get('deploy_path').'/.dep/HEAD');
$headContents = run(sprintf('test -e %s && cat %1$s', $headPath));
//check if HEAD file is exists and then compare it
if (trim($headContents) === trim($remoteHead)) {
throw new GracefulShutdownException("Already up-to-date.");
}
}
run("cd {{deploy_path}} && echo ".$remoteHead.' > .dep/HEAD');
});

0 comments on commit bb4d41c

Please sign in to comment.