1 | <?php |
||
2 | |||
3 | namespace Deployer; |
||
4 | |||
5 | use Deployer\Exception\ConfigurationException; |
||
6 | use Symfony\Component\Console\Input\InputOption; |
||
7 | |||
8 | /** |
||
9 | * Determines which branch to deploy. Can be overridden with CLI option `--branch`. |
||
10 | * If not specified, will get current git HEAD branch as default branch to deploy. |
||
11 | */ |
||
12 | set('branch', 'HEAD'); |
||
13 | |||
14 | option('tag', null, InputOption::VALUE_REQUIRED, 'Tag to deploy'); |
||
15 | option('revision', null, InputOption::VALUE_REQUIRED, 'Revision to deploy'); |
||
16 | option('branch', null, InputOption::VALUE_REQUIRED, 'Branch to deploy'); |
||
17 | 4 | ||
18 | // The deploy target: a branch, a tag or a revision. |
||
19 | set('target', function () { |
||
20 | $target = ''; |
||
21 | |||
22 | 4 | $branch = get('branch'); |
|
23 | 4 | if (!empty($branch)) { |
|
24 | $target = $branch; |
||
25 | } |
||
26 | 4 | ||
27 | // Override target from CLI options. |
||
28 | if (input()->hasOption('branch') && !empty(input()->getOption('branch'))) { |
||
29 | $target = input()->getOption('branch'); |
||
30 | 4 | } |
|
31 | 8 | if (input()->hasOption('tag') && !empty(input()->getOption('tag'))) { |
|
32 | $target = input()->getOption('tag'); |
||
33 | } |
||
34 | if (input()->hasOption('revision') && !empty(input()->getOption('revision'))) { |
||
35 | $target = input()->getOption('revision'); |
||
36 | } |
||
37 | |||
38 | if (empty($target)) { |
||
39 | 4 | $target = "HEAD"; |
|
40 | 4 | } |
|
41 | 4 | return $target; |
|
42 | 4 | }); |
|
43 | |||
44 | // Sets deploy:update_code strategy. |
||
45 | // Can be one of: |
||
46 | 4 | // - archive |
|
47 | 8 | // - clone (if you need the origin repository `.git` dir in your {{release_path}}) |
|
48 | set('update_code_strategy', 'archive'); |
||
49 | 8 | ||
50 | // Sets environment variable _GIT_SSH_COMMAND_ for `git clone` command. |
||
51 | 4 | // If `StrictHostKeyChecking` flag is set to `accept-new` then ssh will |
|
52 | 4 | // automatically add new host keys to the user known hosts files, but |
|
53 | 4 | // will not permit connections to hosts with changed host keys. |
|
54 | 4 | set('git_ssh_command', 'ssh -o StrictHostKeyChecking=accept-new'); |
|
55 | 4 | ||
56 | 4 | /** |
|
57 | 4 | * Specifies a sub directory within the repository to deploy. |
|
58 | 4 | * Works only when [`update_code_strategy`](#update_code_strategy) is set to `archive` (default). |
|
59 | * |
||
60 | 4 | * Example: |
|
61 | 4 | * - set value to `src` if you want to deploy the folder that lives at `/src`. |
|
62 | * - set value to `src/api` if you want to deploy the folder that lives at `/src/api`. |
||
63 | * |
||
64 | * Note: do not use a leading `/`! |
||
65 | */ |
||
66 | 4 | set('sub_directory', false); |
|
67 | 4 | ||
68 | 4 | /** |
|
69 | * Update code at {{release_path}} on host. |
||
70 | */ |
||
71 | desc('Updates code'); |
||
72 | task('deploy:update_code', function () { |
||
73 | $git = get('bin/git'); |
||
74 | 4 | $repository = get('repository'); |
|
75 | 4 | $target = get('target'); |
|
76 | 4 | ||
77 | if (empty($repository)) { |
||
78 | throw new ConfigurationException("Missing 'repository' configuration."); |
||
79 | } |
||
80 | |||
81 | $targetWithDir = $target; |
||
82 | 4 | if (!empty(get('sub_directory'))) { |
|
83 | 4 | $targetWithDir .= ':{{sub_directory}}'; |
|
84 | } |
||
85 | |||
86 | $bare = parse('{{deploy_path}}/.dep/repo'); |
||
87 | 4 | $env = [ |
|
88 | 4 | 'GIT_TERMINAL_PROMPT' => '0', |
|
89 | 'GIT_SSH_COMMAND' => get('git_ssh_command'), |
||
90 | ]; |
||
91 | |||
92 | start: |
||
93 | // Clone the repository to a bare repo. |
||
94 | run("[ -d $bare ] || mkdir -p $bare"); |
||
95 | run("[ -f $bare/HEAD ] || $git clone --mirror $repository $bare 2>&1", env: $env); |
||
96 | |||
97 | 4 | cd($bare); |
|
98 | |||
99 | 3 | // If remote url changed, drop `.dep/repo` and reinstall. |
|
100 | if (run("$git config --get remote.origin.url") !== $repository) { |
||
101 | cd('{{deploy_path}}'); |
||
102 | 3 | run("rm -rf $bare"); |
|
103 | goto start; |
||
104 | } |
||
105 | |||
106 | 1 | run("$git remote update 2>&1", env: $env); |
|
107 | |||
108 | |||
109 | 4 | // Copy to release_path. |
|
110 | if (get('update_code_strategy') === 'archive') { |
||
111 | run("$git archive $targetWithDir | tar -x -f - -C {{release_path}} 2>&1"); |
||
112 | 8 | } elseif (get('update_code_strategy') === 'clone') { |
|
113 | cd('{{release_path}}'); |
||
114 | run("$git clone -l $bare ."); |
||
115 | run("$git remote set-url origin $repository", env: $env); |
||
116 | run("$git checkout --force $target"); |
||
117 | } else { |
||
118 | throw new ConfigurationException(parse("Unknown `update_code_strategy` option: {{update_code_strategy}}.")); |
||
119 | } |
||
120 | |||
121 | // Save git revision in REVISION file. |
||
122 | $rev = escapeshellarg(run("$git rev-list $target -1")); |
||
123 | run("echo $rev > {{release_path}}/REVISION"); |
||
124 | }); |
||
125 |