2019-08-12 20:59:59 +03:00
|
|
|
<?php
|
|
|
|
namespace Deployer;
|
|
|
|
|
2020-02-01 11:15:53 -08:00
|
|
|
use Deployer\Exception\Exception;
|
2019-08-12 20:59:59 +03:00
|
|
|
use Deployer\Exception\GracefulShutdownException;
|
|
|
|
|
2020-02-01 11:15:53 -08:00
|
|
|
// Cancel deployment if there would be no change to the codebase.
|
|
|
|
// This avoids unnecessary releases if the latest commit has already been deployed.
|
2021-11-08 22:59:39 +01:00
|
|
|
desc('Checks remote head');
|
2019-08-12 20:59:59 +03:00
|
|
|
task('deploy:check_remote', function () {
|
2019-10-26 14:30:22 +03:00
|
|
|
$repository = get('repository');
|
2020-02-01 11:15:53 -08:00
|
|
|
|
|
|
|
// Skip if there is no current deployment to compare
|
2021-09-24 00:17:11 +02:00
|
|
|
if (!test('[ -d {{current_path}}/.git ]')) {
|
2019-08-12 20:59:59 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-01 11:15:53 -08:00
|
|
|
// 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';
|
|
|
|
}
|
2020-10-10 21:58:06 +02:00
|
|
|
$remoteLs = runLocally("git ls-remote $opt $repository $ref");
|
2020-02-01 11:15:53 -08:00
|
|
|
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'.");
|
2019-08-12 20:59:59 +03:00
|
|
|
}
|
2020-02-01 11:15:53 -08:00
|
|
|
$targetRevision = substr($remoteLs, 0, strpos($remoteLs, "\t"));
|
2019-08-12 20:59:59 +03:00
|
|
|
}
|
|
|
|
|
2020-02-01 11:15:53 -08:00
|
|
|
// Compare commit hashes. We use strpos to support short versions.
|
|
|
|
$targetRevision = trim($targetRevision);
|
2021-09-24 00:17:11 +02:00
|
|
|
$lastDeployedRevision = run('cat {{current_path}}/REVISION');
|
2020-02-01 11:15:53 -08:00
|
|
|
if ($targetRevision && strpos($lastDeployedRevision, $targetRevision) === 0) {
|
|
|
|
throw new GracefulShutdownException("Already up-to-date.");
|
|
|
|
}
|
2020-10-10 21:58:06 +02:00
|
|
|
|
|
|
|
info("deployed different version");
|
2019-08-12 20:59:59 +03:00
|
|
|
});
|