1 | <?php |
||
2 | namespace Deployer; |
||
3 | |||
4 | use Deployer\Exception\RunException; |
||
5 | |||
6 | /** |
||
7 | * Determines which branch to deploy. Can be overridden with cli option `--branch`. |
||
8 | * If not specified, will get current git HEAD branch as default branch to deploy. |
||
9 | */ |
||
10 | set('branch', function () { |
||
11 | try { |
||
12 | $branch = runLocally('git rev-parse --abbrev-ref HEAD'); |
||
13 | } catch (\Throwable $exception) { |
||
14 | $branch = null; |
||
15 | } |
||
16 | |||
17 | 4 | if ($branch === 'HEAD') { |
|
18 | $branch = null; // Travis-CI fix |
||
19 | } |
||
20 | |||
21 | if (input()->hasOption('branch') && !empty(input()->getOption('branch'))) { |
||
22 | 4 | $branch = input()->getOption('branch'); |
|
23 | 4 | } |
|
24 | |||
25 | return $branch; |
||
26 | 4 | }); |
|
27 | |||
28 | /** |
||
29 | * This config option will if set to true will instructs git to use previous release files, |
||
30 | 4 | * and download only changed files from server. |
|
31 | 8 | * |
|
32 | * You don't need to set this option, it will automatically detect if your git supports this feature. |
||
33 | * |
||
34 | * Faster cloning by borrowing objects from existing clones. |
||
35 | */ |
||
36 | set('git_cache', function () { |
||
37 | $gitVersion = run('{{bin/git}} version'); |
||
38 | $regs = []; |
||
39 | 4 | if (preg_match('/((\d+\.?)+)/', $gitVersion, $regs)) { |
|
40 | 4 | $version = $regs[1]; |
|
41 | 4 | } else { |
|
42 | 4 | $version = "1.0.0"; |
|
43 | } |
||
44 | return version_compare($version, '2.3', '>='); |
||
45 | }); |
||
46 | 4 | ||
47 | 8 | /** |
|
48 | * Update code at {{release_path}} on host. |
||
49 | 8 | */ |
|
50 | desc('Update code'); |
||
51 | 4 | task('deploy:update_code', function () { |
|
52 | 4 | $repository = get('repository'); |
|
53 | 4 | $branch = get('branch'); |
|
54 | 4 | $git = get('bin/git'); |
|
55 | 4 | $gitCache = get('git_cache'); |
|
56 | 4 | $recursive = get('git_recursive', true) ? '--recursive' : ''; |
|
57 | 4 | $dissociate = get('git_clone_dissociate', true) ? '--dissociate' : ''; |
|
58 | 4 | $quiet = output()->isQuiet() ? '-q' : ''; |
|
59 | $depth = $gitCache ? '' : '--depth 1'; |
||
60 | 4 | ||
61 | 4 | $at = ''; |
|
62 | if (!empty($branch)) { |
||
63 | $at = "-b \"$branch\""; |
||
64 | } |
||
65 | |||
66 | 4 | // If option `tag` is set |
|
67 | 4 | if (input()->hasOption('tag')) { |
|
68 | 4 | $tag = input()->getOption('tag'); |
|
69 | if (!empty($tag)) { |
||
70 | $at = "-b \"$tag\""; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | 4 | // If option `tag` is not set and option `revision` is set |
|
75 | 4 | if (empty($tag) && input()->hasOption('revision')) { |
|
76 | 4 | $revision = input()->getOption('revision'); |
|
77 | if (!empty($revision)) { |
||
78 | $depth = ''; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | 4 | // Enter deploy_path if present |
|
83 | 4 | if (has('deploy_path')) { |
|
84 | cd('{{deploy_path}}'); |
||
85 | } |
||
86 | |||
87 | 4 | // Populate known hosts |
|
88 | 4 | preg_match('/.*(@|\/\/)([^\/:]+).*/', $repository, $match); |
|
89 | if (isset($match[2])) { |
||
90 | $repositoryHostname = $match[2]; |
||
91 | try { |
||
92 | run("ssh-keygen -F $repositoryHostname"); |
||
93 | } catch (RunException $exception) { |
||
94 | run("ssh-keyscan -H $repositoryHostname >> ~/.ssh/known_hosts"); |
||
95 | } |
||
96 | } |
||
97 | 4 | ||
98 | if ($gitCache && has('previous_release')) { |
||
99 | 3 | try { |
|
100 | run("$git clone $at $recursive $quiet --reference {{previous_release}} $dissociate $repository {{release_path}} 2>&1"); |
||
101 | } catch (\Throwable $exception) { |
||
102 | 3 | // If {{deploy_path}}/releases/{$releases[1]} has a failed git clone, is empty, shallow etc, git would throw error and give up. So we're forcing it to act without reference in this situation |
|
103 | run("$git clone $at $recursive $quiet $repository {{release_path}} 2>&1"); |
||
104 | } |
||
105 | } else { |
||
106 | 1 | // if we're using git cache this would be identical to above code in catch - full clone. If not, it would create shallow clone. |
|
107 | run("$git clone $at $depth $recursive $quiet $repository {{release_path}} 2>&1"); |
||
108 | } |
||
109 | 4 | ||
110 | if (!empty($revision)) { |
||
111 | run("cd {{release_path}} && $git checkout $revision"); |
||
112 | 8 | } |
|
113 | }); |
||
114 |