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

Fix check_remote task revision resolution #1995

Merged
merged 1 commit into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
48 changes: 36 additions & 12 deletions recipe/deploy/check_remote.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
});