Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check_remote_head option to avoid unnecessary new releases by che… #1759

Merged
merged 11 commits into from
Aug 6, 2019
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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]

### Changed
Expand Down Expand Up @@ -422,6 +423,7 @@


[#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');
});