Skip to content

Commit

Permalink
feat: add new local_archive update code strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Oct 1, 2024
1 parent 15ce6fe commit a4663d1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 52 deletions.
20 changes: 11 additions & 9 deletions contrib/sentry.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ static function (&$value) use ($config) {
function getPreviousReleaseRevision()
{
switch (get('update_code_strategy')) {
case 'local_archive':
case 'archive':
if (has('previous_release')) {
return run('cat {{previous_release}}/REVISION');
Expand All @@ -207,6 +208,7 @@ function getPreviousReleaseRevision()
function getCurrentReleaseRevision()
{
switch (get('update_code_strategy')) {
case 'local_archive':
case 'archive':
return run('cat {{release_path}}/REVISION');

Expand All @@ -222,22 +224,22 @@ function getCurrentReleaseRevision()
function getReleaseGitRef(): Closure
{
return static function ($config = []): string {
if (get('update_code_strategy') === 'archive') {
if (isset($config['git_version_command'])) {
cd('{{deploy_path}}/.dep/repo');

return trim(run($config['git_version_command']));
}
$strategy = get('update_code_strategy');

return run('cat {{current_path}}/REVISION');
if ($strategy === 'archive') {
cd('{{deploy_path}}/.dep/repo');
} else {
cd('{{release_path}}');
}

cd('{{release_path}}');

if (isset($config['git_version_command'])) {
return trim(run($config['git_version_command']));
}

if ($strategy !== 'clone') {
return run('cat {{current_path}}/REVISION');
}

return trim(run('git log -n 1 --format="%h"'));
};
}
Expand Down
15 changes: 8 additions & 7 deletions docs/recipe/deploy/update_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,21 @@ The value of this configuration is autogenerated on access.


### update_code_strategy
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L47)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L48)

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

```php title="Default value"
'archive'
```


### git_ssh_command
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L53)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L54)

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


### sub_directory
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L65)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L66)

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

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

### deploy:update_code
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L71)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L72)

Updates code.

Expand Down
86 changes: 50 additions & 36 deletions recipe/deploy/update_code.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@

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

// Sets environment variable _GIT_SSH_COMMAND_ for `git clone` command.
Expand All @@ -54,7 +55,7 @@

/**
* Specifies a sub directory within the repository to deploy.
* Works only when [`update_code_strategy`](#update_code_strategy) is set to `archive` (default).
* Works only when [`update_code_strategy`](#update_code_strategy) is set to `archive` (default) or `local_archive`.
*
* Example:
* - set value to `src` if you want to deploy the folder that lives at `/src`.
Expand All @@ -69,51 +70,64 @@
*/
desc('Updates code');
task('deploy:update_code', function () {
$git = get('bin/git');
$repository = get('repository');
$strategy = get('update_code_strategy');
$target = get('target');

$targetWithDir = $target;
if (!empty(get('sub_directory'))) {
$targetWithDir .= ':{{sub_directory}}';
}

$bare = parse('{{deploy_path}}/.dep/repo');
$env = [
'GIT_TERMINAL_PROMPT' => '0',
'GIT_SSH_COMMAND' => get('git_ssh_command')
];
if ($strategy === 'local_archive') {
$host = currentHost()->connectionString();

start:
// Clone the repository to a bare repo.
run("[ -d $bare ] || mkdir -p $bare");
run("[ -f $bare/HEAD ] || $git clone --mirror $repository $bare 2>&1", ['env' => $env]);
// Copy to release_path.
runLocally(<<<BASH
git archive {$targetWithDir} | ssh {$host} "tar -x -f - -C {{release_path}} 2>&1"
BASH);

cd($bare);

// If remote url changed, drop `.dep/repo` and reinstall.
if (run("$git config --get remote.origin.url") !== $repository) {
cd('{{deploy_path}}');
run("rm -rf $bare");
goto start;
}

run("$git remote update 2>&1", ['env' => $env]);


// Copy to release_path.
if (get('update_code_strategy') === 'archive') {
run("$git archive $targetWithDir | tar -x -f - -C {{release_path}} 2>&1");
} else if (get('update_code_strategy') === 'clone') {
cd('{{release_path}}');
run("$git clone -l $bare .");
run("$git remote set-url origin $repository", ['env' => $env]);
run("$git checkout --force $target");
$rev = escapeshellarg(runLocally("git rev-list $target -1"));
} else {
throw new ConfigurationException(parse("Unknown `update_code_strategy` option: {{update_code_strategy}}."));
$git = get('bin/git');
$repository = get('repository');

$bare = parse('{{deploy_path}}/.dep/repo');
$env = [
'GIT_TERMINAL_PROMPT' => '0',
'GIT_SSH_COMMAND' => get('git_ssh_command')
];

start:
// Clone the repository to a bare repo.
run("[ -d $bare ] || mkdir -p $bare");
run("[ -f $bare/HEAD ] || $git clone --mirror $repository $bare 2>&1", ['env' => $env]);

cd($bare);

// If remote url changed, drop `.dep/repo` and reinstall.
if (run("$git config --get remote.origin.url") !== $repository) {
cd('{{deploy_path}}');
run("rm -rf $bare");
goto start;
}

run("$git remote update 2>&1", ['env' => $env]);

// Copy to release_path.
if ($strategy === 'archive') {
run("$git archive $targetWithDir | tar -x -f - -C {{release_path}} 2>&1");
} else if ($strategy === 'clone') {
cd('{{release_path}}');
run("$git clone -l $bare .");
run("$git remote set-url origin $repository", ['env' => $env]);
run("$git checkout --force $target");
} else {
throw new ConfigurationException(parse("Unknown `update_code_strategy` option: {{update_code_strategy}}."));
}

$rev = escapeshellarg(run("$git rev-list $target -1"));
}

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

0 comments on commit a4663d1

Please sign in to comment.