diff --git a/CHANGELOG.md b/CHANGELOG.md index a47fbf3c6..0ae89a987 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,16 +1,17 @@ # Changelog -## v6.7.4 -[v6.7.3...v6.7.4](https://github.com/deployphp/deployer/compare/v6.7.3...v6.7.4) +## master +[v6.7.3...master](https://github.com/deployphp/deployer/compare/v6.7.3...master) ### Added - Documented check_remote task usage - Speedup deploy:clear_paths ### Fixed -- Fixed Silverstripe CMS recipe assets path -- Fixed check_remote task errors +- Fixed Silverstripe CMS recipe assets path [#1989] +- Fixed check_remote task errors [#1990] +- Fixed check_remote task revision resolution [#1994] ## v6.7.3 @@ -557,6 +558,9 @@ - Fixed remove of shared dir on first deploy +[#1994]: https://github.com/deployphp/deployer/issues/1994 +[#1990]: https://github.com/deployphp/deployer/issues/1990 +[#1989]: https://github.com/deployphp/deployer/issues/1989 [#1971]: https://github.com/deployphp/deployer/pull/1971 [#1969]: https://github.com/deployphp/deployer/issues/1969 [#1899]: https://github.com/deployphp/deployer/pull/1899 diff --git a/recipe/deploy/check_remote.php b/recipe/deploy/check_remote.php index ab8998caa..820e8865f 100644 --- a/recipe/deploy/check_remote.php +++ b/recipe/deploy/check_remote.php @@ -7,29 +7,53 @@ namespace Deployer; +use Deployer\Exception\Exception; use Deployer\Exception\GracefulShutdownException; -// 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). +// Cancel deployment if there would be no change to the codebase. +// This avoids unnecessary releases if the latest commit has already been deployed. desc('Check remote head'); task('deploy:check_remote', function () { $repository = get('repository'); if (empty($repository)) { + throw new Exception("You need to specify a repository."); + } + + // Skip if there is no current deployment to compare + if (! test('[ -d {{deploy_path}}/current/.git ]')) { return; } - $revision = input()->getOption('revision') ?? null; - $remoteHead = $revision ?? runLocally(sprintf('%s ls-remote %s HEAD | tr -d "HEAD"', get('bin/git'), $repository)); + // Determine the hash of the remote revision about to be deployed + $targetRevision = input()->getOption('revision'); + + if (!$targetRevision) { + $ref = 'HEAD'; + $opt = ''; + + if ($tag = input()->getOption('tag')) { + $ref = $tag; + $opt = '--tags'; + } elseif ($branch = get('branch')) { + $ref = $branch; + $opt = '--heads'; + } - if (null == input()->getOption('tag')) { - // Init HEAD file if it doesn't exist, then compare - $headPath = get('deploy_path') . '/.dep/HEAD'; - run("touch $headPath"); - $headContents = run("cat $headPath"); - if (trim($headContents) === trim($remoteHead)) { - throw new GracefulShutdownException("Already up-to-date."); + $remoteLs = runLocally(sprintf("%s ls-remote $opt $repository $ref", get('bin/git'))); + if (strstr($remoteLs, "\n")) { + throw new Exception("Could not determine target revision. '$ref' matched multiple commits."); + } + if (!$remoteLs) { + throw new Exception("Could not resolve a revision from '$ref'."); } + + $targetRevision = substr($remoteLs, 0, strpos($remoteLs, "\t")); } - run("cd {{deploy_path}} && echo $remoteHead > .dep/HEAD"); + // Compare commit hashes. We use strpos to support short versions. + $targetRevision = trim($targetRevision); + $lastDeployedRevision = trim(run(sprintf('cd {{deploy_path}}/current && %s rev-parse HEAD', get('bin/git')))); + if ($targetRevision && strpos($lastDeployedRevision, $targetRevision) === 0) { + throw new GracefulShutdownException("Already up-to-date."); + } });