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
22 changes: 22 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,25 @@

// 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 ? $revision :
trim(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');
$isRemoteHeadExists = test("[ -f $headPath ]");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check really required, or could it be achieved in combination with the „cat“ line in a single command?
Every remote command has a big overhead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, cating non-existing file leads to STDERR.
I know, we could output the error to the 2>/dev/null but I think it's a hack, the right solution is to check whether the file exists or not.

Copy link
Contributor

@staabm staabm Nov 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And something along the lines of
run('test -f '. $headPath ' && cat '.$headPath);

?

Copy link
Contributor Author

@ahmadmayahi ahmadmayahi Nov 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could test become a part of the run command? I guess you mean the output of test right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


//check if HEAD file is exists and then compare it
if (true === $isRemoteHeadExists) {
$headContents = run('cat '.$headPath);
if ($headContents === $remoteHead) {
throw new GracefulShutdownException("Already up-to-date.");
}
}
}
run("cd {{deploy_path}} && echo ".$remoteHead.' > .dep/HEAD');
});