1 | <?php |
||
2 | /* (c) Anton Medvedev <[email protected]> |
||
3 | * |
||
4 | * For the full copyright and license information, please view the LICENSE |
||
5 | * file that was distributed with this source code. |
||
6 | */ |
||
7 | |||
8 | namespace Deployer; |
||
9 | |||
10 | use Deployer\Exception\Exception; |
||
11 | use Deployer\Exception\GracefulShutdownException; |
||
12 | |||
13 | // Cancel deployment if there would be no change to the codebase. |
||
14 | // This avoids unnecessary releases if the latest commit has already been deployed. |
||
15 | 8 | desc('Check remote head'); |
|
16 | task('deploy:check_remote', function () { |
||
17 | $repository = get('repository'); |
||
18 | if (empty($repository)) { |
||
19 | throw new Exception("You need to specify a repository."); |
||
20 | } |
||
21 | |||
22 | // Skip if there is no current deployment to compare |
||
23 | if (! test('[ -d {{deploy_path}}/current/.git ]')) { |
||
24 | return; |
||
25 | } |
||
26 | |||
27 | // Determine the hash of the remote revision about to be deployed |
||
28 | $targetRevision = input()->getOption('revision'); |
||
29 | |||
30 | if (!$targetRevision) { |
||
31 | $ref = 'HEAD'; |
||
32 | $opt = ''; |
||
33 | |||
34 | if ($tag = input()->getOption('tag')) { |
||
35 | $ref = $tag; |
||
36 | $opt = '--tags'; |
||
37 | } elseif ($branch = get('branch')) { |
||
38 | $ref = $branch; |
||
39 | $opt = '--heads'; |
||
40 | } |
||
41 | |||
42 | $remoteLs = null; |
||
43 | on(localhost(), function () use (& $remoteLs, $opt, $repository, $ref) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
44 | $getRemoteRevisionCmd = sprintf("%s ls-remote $opt $repository $ref", get('bin/git')); |
||
45 | $remoteLs = run($getRemoteRevisionCmd); |
||
46 | }); |
||
47 | |||
48 | if (strstr($remoteLs, "\n")) { |
||
49 | throw new Exception("Could not determine target revision. '$ref' matched multiple commits."); |
||
50 | } |
||
51 | if (!$remoteLs) { |
||
52 | throw new Exception("Could not resolve a revision from '$ref'."); |
||
53 | } |
||
54 | |||
55 | $targetRevision = substr($remoteLs, 0, strpos($remoteLs, "\t")); |
||
56 | } |
||
57 | |||
58 | // Compare commit hashes. We use strpos to support short versions. |
||
59 | 8 | $targetRevision = trim($targetRevision); |
|
60 | $lastDeployedRevision = trim(run(sprintf('cd {{deploy_path}}/current && %s rev-parse HEAD', get('bin/git')))); |
||
61 | if ($targetRevision && strpos($lastDeployedRevision, $targetRevision) === 0) { |
||
62 | throw new GracefulShutdownException("Already up-to-date."); |
||
63 | } |
||
64 | }); |
||
65 |