diff --git a/contrib/sentry.php b/contrib/sentry.php index f84450950..721345ea0 100644 --- a/contrib/sentry.php +++ b/contrib/sentry.php @@ -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'); @@ -207,6 +208,7 @@ function getPreviousReleaseRevision() function getCurrentReleaseRevision() { switch (get('update_code_strategy')) { + case 'local_archive': case 'archive': return run('cat {{release_path}}/REVISION'); @@ -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"')); }; } diff --git a/docs/recipe/deploy/update_code.md b/docs/recipe/deploy/update_code.md index 893df44c2..083f8807b 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#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' @@ -48,7 +49,7 @@ Can be one of: ### 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 @@ -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`. @@ -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. diff --git a/recipe/deploy/update_code.php b/recipe/deploy/update_code.php index dc211aca5..9149dc697 100644 --- a/recipe/deploy/update_code.php +++ b/recipe/deploy/update_code.php @@ -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. @@ -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`. @@ -69,8 +70,7 @@ */ 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; @@ -78,42 +78,56 @@ $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(<<&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"); });