Skip to content

Commit 551e516

Browse files
committed
feat: add new local_archive update code strategy
1 parent ca82aeb commit 551e516

File tree

3 files changed

+73
-56
lines changed

3 files changed

+73
-56
lines changed

contrib/sentry.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ static function (&$value) use ($config) {
187187
function getPreviousReleaseRevision()
188188
{
189189
switch (get('update_code_strategy')) {
190+
case 'local_archive':
190191
case 'archive':
191192
if (has('previous_release')) {
192193
return run('cat {{previous_release}}/REVISION');
@@ -208,6 +209,7 @@ function getPreviousReleaseRevision()
208209
function getCurrentReleaseRevision()
209210
{
210211
switch (get('update_code_strategy')) {
212+
case 'local_archive':
211213
case 'archive':
212214
return run('cat {{release_path}}/REVISION');
213215

@@ -223,22 +225,22 @@ function getCurrentReleaseRevision()
223225
function getReleaseGitRef(): Closure
224226
{
225227
return static function ($config = []): string {
226-
if (get('update_code_strategy') === 'archive') {
227-
if (isset($config['git_version_command'])) {
228-
cd('{{deploy_path}}/.dep/repo');
229-
230-
return trim(run($config['git_version_command']));
231-
}
228+
$strategy = get('update_code_strategy');
232229

233-
return run('cat {{current_path}}/REVISION');
230+
if ($strategy === 'archive') {
231+
cd('{{deploy_path}}/.dep/repo');
232+
} else {
233+
cd('{{release_path}}');
234234
}
235235

236-
cd('{{release_path}}');
237-
238236
if (isset($config['git_version_command'])) {
239237
return trim(run($config['git_version_command']));
240238
}
241239

240+
if ($strategy !== 'clone') {
241+
return run('cat {{current_path}}/REVISION');
242+
}
243+
242244
return trim(run('git log -n 1 --format="%h"'));
243245
};
244246
}

docs/recipe/deploy/update_code.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,21 @@ The value of this configuration is autogenerated on access.
3535

3636

3737
### update_code_strategy
38-
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L48)
38+
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L49)
3939

4040
Sets deploy:update_code strategy.
4141
Can be one of:
42-
- archive
43-
- clone (if you need the origin repository `.git` dir in your [release_path](/docs/recipe/deploy/release.md#release_path))
42+
- local_archive (copies the repository from local machine)
43+
- archive (default, fetches the code from the remote repository)
44+
- clone (if you need the origin repository `.git` dir in your [release_path](/docs/recipe/deploy/release.md#release_path), clones from remote repository)
4445

4546
```php title="Default value"
4647
'archive'
4748
```
4849

4950

5051
### git_ssh_command
51-
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L54)
52+
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L55)
5253

5354
Sets environment variable _GIT_SSH_COMMAND_ for `git clone` command.
5455
If `StrictHostKeyChecking` flag is set to `accept-new` then ssh will
@@ -61,10 +62,10 @@ will not permit connections to hosts with changed host keys.
6162

6263

6364
### sub_directory
64-
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L66)
65+
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L67)
6566

6667
Specifies a sub directory within the repository to deploy.
67-
Works only when [`update_code_strategy`](#update_code_strategy) is set to `archive` (default).
68+
Works only when [`update_code_strategy`](#update_code_strategy) is set to `archive` (default) or `local_archive`.
6869

6970
Example:
7071
- set value to `src` if you want to deploy the folder that lives at `/src`.
@@ -81,7 +82,7 @@ false
8182
## Tasks
8283

8384
### deploy\:update_code {#deploy-update_code}
84-
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L72)
85+
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L73)
8586

8687
Updates code.
8788

recipe/deploy/update_code.php

+54-40
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@
4343

4444
// Sets deploy:update_code strategy.
4545
// Can be one of:
46-
// - archive
47-
// - clone (if you need the origin repository `.git` dir in your {{release_path}})
46+
// - local_archive (copies the repository from local machine)
47+
// - archive (default, fetches the code from the remote repository)
48+
// - clone (if you need the origin repository `.git` dir in your {{release_path}}, clones from remote repository)
4849
set('update_code_strategy', 'archive');
4950

5051
// Sets environment variable _GIT_SSH_COMMAND_ for `git clone` command.
@@ -55,7 +56,7 @@
5556

5657
/**
5758
* Specifies a sub directory within the repository to deploy.
58-
* Works only when [`update_code_strategy`](#update_code_strategy) is set to `archive` (default).
59+
* Works only when [`update_code_strategy`](#update_code_strategy) is set to `archive` (default) or `local_archive`.
5960
*
6061
* Example:
6162
* - set value to `src` if you want to deploy the folder that lives at `/src`.
@@ -70,55 +71,68 @@
7071
*/
7172
desc('Updates code');
7273
task('deploy:update_code', function () {
73-
$git = get('bin/git');
74-
$repository = get('repository');
74+
$strategy = get('update_code_strategy');
7575
$target = get('target');
7676

77-
if (empty($repository)) {
78-
throw new ConfigurationException("Missing 'repository' configuration.");
79-
}
80-
8177
$targetWithDir = $target;
8278
if (!empty(get('sub_directory'))) {
8379
$targetWithDir .= ':{{sub_directory}}';
8480
}
8581

86-
$bare = parse('{{deploy_path}}/.dep/repo');
87-
$env = [
88-
'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-
cd($bare);
98-
99-
// If remote url changed, drop `.dep/repo` and reinstall.
100-
if (run("$git config --get remote.origin.url") !== $repository) {
101-
cd('{{deploy_path}}');
102-
run("rm -rf $bare");
103-
goto start;
104-
}
105-
106-
run("$git remote update 2>&1", env: $env);
82+
if ($strategy === 'local_archive') {
83+
$host = currentHost()->connectionString();
10784

85+
// Copy to release_path.
86+
runLocally(<<<BASH
87+
git archive {$targetWithDir} | ssh {$host} "tar -x -f - -C {{release_path}} 2>&1"
88+
BASH);
10889

109-
// 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-
} 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");
90+
$rev = escapeshellarg(runLocally("git rev-list $target -1"));
11791
} else {
118-
throw new ConfigurationException(parse("Unknown `update_code_strategy` option: {{update_code_strategy}}."));
92+
$git = get('bin/git');
93+
$repository = get('repository');
94+
95+
if (empty($repository)) {
96+
throw new ConfigurationException("Missing 'repository' configuration.");
97+
}
98+
99+
$bare = parse('{{deploy_path}}/.dep/repo');
100+
$env = [
101+
'GIT_TERMINAL_PROMPT' => '0',
102+
'GIT_SSH_COMMAND' => get('git_ssh_command'),
103+
];
104+
105+
start:
106+
// Clone the repository to a bare repo.
107+
run("[ -d $bare ] || mkdir -p $bare");
108+
run("[ -f $bare/HEAD ] || $git clone --mirror $repository $bare 2>&1", env: $env);
109+
110+
cd($bare);
111+
112+
// If remote url changed, drop `.dep/repo` and reinstall.
113+
if (run("$git config --get remote.origin.url") !== $repository) {
114+
cd('{{deploy_path}}');
115+
run("rm -rf $bare");
116+
goto start;
117+
}
118+
119+
run("$git remote update 2>&1", env: $env);
120+
121+
// Copy to release_path.
122+
if ($strategy === 'archive') {
123+
run("$git archive $targetWithDir | tar -x -f - -C {{release_path}} 2>&1");
124+
} else if ($strategy === 'clone') {
125+
cd('{{release_path}}');
126+
run("$git clone -l $bare .");
127+
run("$git remote set-url origin $repository", env: $env);
128+
run("$git checkout --force $target");
129+
} else {
130+
throw new ConfigurationException(parse("Unknown `update_code_strategy` option: {{update_code_strategy}}."));
131+
}
132+
133+
$rev = escapeshellarg(run("$git rev-list $target -1"));
119134
}
120135

121136
// Save git revision in REVISION file.
122-
$rev = escapeshellarg(run("$git rev-list $target -1"));
123137
run("echo $rev > {{release_path}}/REVISION");
124138
});

0 commit comments

Comments
 (0)