From ed77fde5f5ad362e54f956fdf19c38f728d6a1a2 Mon Sep 17 00:00:00 2001 From: Caleb White Date: Tue, 1 Oct 2024 10:40:28 -0500 Subject: [PATCH 1/2] feat: add new `local_archive` update code strategy --- contrib/sentry.php | 20 ++++--- docs/recipe/deploy/update_code.md | 15 ++--- recipe/deploy/update_code.php | 94 ++++++++++++++++++------------- 3 files changed, 73 insertions(+), 56 deletions(-) diff --git a/contrib/sentry.php b/contrib/sentry.php index 93dae794c..43c5d3245 100644 --- a/contrib/sentry.php +++ b/contrib/sentry.php @@ -187,6 +187,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'); @@ -208,6 +209,7 @@ function getPreviousReleaseRevision() function getCurrentReleaseRevision() { switch (get('update_code_strategy')) { + case 'local_archive': case 'archive': return run('cat {{release_path}}/REVISION'); @@ -223,22 +225,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"')); }; } diff --git a/docs/recipe/deploy/update_code.md b/docs/recipe/deploy/update_code.md index 40962f253..2063c2972 100644 --- a/docs/recipe/deploy/update_code.md +++ b/docs/recipe/deploy/update_code.md @@ -35,12 +35,13 @@ 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#L48) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L49) 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' @@ -48,7 +49,7 @@ Can be one of: ### git_ssh_command -[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L54) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L55) Sets environment variable _GIT_SSH_COMMAND_ for `git clone` command. 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. ### sub_directory -[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L66) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L67) 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`. @@ -81,7 +82,7 @@ false ## Tasks ### deploy\:update_code {#deploy-update_code} -[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L72) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/deploy/update_code.php#L73) Updates code. diff --git a/recipe/deploy/update_code.php b/recipe/deploy/update_code.php index f539d29a5..9deee3309 100644 --- a/recipe/deploy/update_code.php +++ b/recipe/deploy/update_code.php @@ -43,8 +43,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. @@ -55,7 +56,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`. @@ -70,55 +71,68 @@ */ desc('Updates code'); task('deploy:update_code', function () { - $git = get('bin/git'); - $repository = get('repository'); + $strategy = get('update_code_strategy'); $target = get('target'); - if (empty($repository)) { - throw new ConfigurationException("Missing 'repository' configuration."); - } - $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'), - ]; - - 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); + if ($strategy === 'local_archive') { + $host = currentHost()->connectionString(); + // Copy to release_path. + runLocally(<<&1" + BASH); - // Copy to release_path. - if (get('update_code_strategy') === 'archive') { - run("$git archive $targetWithDir | tar -x -f - -C {{release_path}} 2>&1"); - } elseif (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'); + + if (empty($repository)) { + throw new ConfigurationException("Missing 'repository' configuration."); + } + + $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"); + } elseif ($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"); }); From 922317aa224bf3bdd8baa4ad7c660bd4c2a65bb3 Mon Sep 17 00:00:00 2001 From: Caleb White Date: Mon, 10 Feb 2025 13:07:44 -0600 Subject: [PATCH 2/2] chore: use upload function --- recipe/deploy/update_code.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/recipe/deploy/update_code.php b/recipe/deploy/update_code.php index 9deee3309..f9e2ea5ce 100644 --- a/recipe/deploy/update_code.php +++ b/recipe/deploy/update_code.php @@ -73,6 +73,7 @@ task('deploy:update_code', function () { $strategy = get('update_code_strategy'); $target = get('target'); + $git = get('bin/git'); $targetWithDir = $target; if (!empty(get('sub_directory'))) { @@ -80,16 +81,14 @@ } if ($strategy === 'local_archive') { - $host = currentHost()->connectionString(); - - // Copy to release_path. - runLocally(<<&1" - BASH); + runLocally("$git archive $targetWithDir -o archive.tar"); + upload('archive.tar', '{{release_path}}/archive.tar'); + run("tar -xf {{release_path}}/archive.tar -C {{release_path}}"); + run("rm {{release_path}}/archive.tar"); + unlink('archive.tar'); $rev = escapeshellarg(runLocally("git rev-list $target -1")); } else { - $git = get('bin/git'); $repository = get('repository'); if (empty($repository)) {