deployer/recipe/deploy/update_code.php

118 lines
3.7 KiB
PHP
Raw Normal View History

2016-11-19 15:13:32 +07:00
<?php
namespace Deployer;
2021-09-24 16:16:15 +02:00
use Deployer\Exception\ConfigurationException;
use Symfony\Component\Console\Input\InputOption;
2021-09-24 16:16:15 +02:00
/**
* Determines which branch to deploy. Can be overridden with CLI option `--branch`.
2020-10-02 00:11:13 +02:00
* If not specified, will get current git HEAD branch as default branch to deploy.
*/
set('branch', 'HEAD');
option('tag', null, InputOption::VALUE_REQUIRED, 'Tag to deploy');
option('revision', null, InputOption::VALUE_REQUIRED, 'Revision to deploy');
option('branch', null, InputOption::VALUE_REQUIRED, 'Branch to deploy');
// The deploy target: a branch, a tag or a revision.
set('target', function () {
$target = '';
$branch = get('branch');
if (!empty($branch)) {
$target = $branch;
}
// Override target from CLI options.
if (input()->hasOption('branch') && !empty(input()->getOption('branch'))) {
$target = input()->getOption('branch');
}
if (input()->hasOption('tag') && !empty(input()->getOption('tag'))) {
$target = input()->getOption('tag');
}
if (input()->hasOption('revision') && !empty(input()->getOption('revision'))) {
$target = input()->getOption('revision');
}
if (empty($target)) {
$target = "HEAD";
}
return $target;
});
2021-09-24 15:08:42 +02:00
2021-09-24 16:16:15 +02:00
// Sets deploy:update_code strategy.
2021-11-10 21:36:20 +00:00
// Can be one of:
2021-09-24 16:16:15 +02:00
// - archive
2021-10-12 22:49:34 +02:00
// - clone (if you need `.git` dir in your {{release_path}})
2021-09-24 16:16:15 +02:00
set('update_code_strategy', 'archive');
// Sets environment variable _GIT_SSH_COMMAND_ for `git clone` command.
// If `StrictHostKeyChecking` flag is set to `accept-new` then ssh will
// automatically add new host keys to the user known hosts files, but
// will not permit connections to hosts with changed host keys.
set('git_ssh_command', 'ssh -o StrictHostKeyChecking=accept-new');
/**
* Specifies a sub directory within the repository to deploy.
* Works only when [`update_code_strategy`](#update_code_strategy) is set to `archive` (default).
2022-03-17 20:07:19 +01:00
*
* Example:
* - set value to `src` if you want to deploy the folder that lives at `/src/api`.
* - set value to `src/api` if you want to deploy the folder that lives at `/src/api`.
2022-03-17 20:07:19 +01:00
*
* Note: do not use a leading `/`!
*/
2022-03-17 20:07:19 +01:00
set('sub_directory', false);
2020-10-02 00:11:13 +02:00
/**
* Update code at {{release_path}} on host.
*/
2021-11-08 22:59:39 +01:00
desc('Updates code');
2016-11-19 15:13:32 +07:00
task('deploy:update_code', function () {
$git = get('bin/git');
$repository = get('repository');
$target = get('target');
$targetWithDir = $target;
2022-03-17 20:07:19 +01:00
if (!empty(get('sub_directory'))) {
$targetWithDir .= ':{{sub_directory}}';
2022-03-17 20:07:19 +01:00
}
2017-11-27 11:18:22 +01:00
2021-03-15 01:35:06 +03:00
$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.
2021-03-15 01:35:06 +03:00
run("[ -d $bare ] || mkdir -p $bare");
run("[ -f $bare/HEAD ] || $git clone --mirror $repository $bare 2>&1", ['env' => $env]);
2016-11-19 15:13:32 +07:00
2021-03-15 01:35:06 +03:00
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;
}
2021-03-15 01:35:06 +03:00
run("$git remote update 2>&1", ['env' => $env]);
2021-09-24 16:16:15 +02:00
2021-11-10 21:36:20 +00:00
// Copy to release_path.
2021-09-24 16:16:15 +02:00
if (get('update_code_strategy') === 'archive') {
run("$git archive $targetWithDir | tar -x -f - -C {{release_path}} 2>&1");
2021-10-12 22:49:34 +02:00
} else if (get('update_code_strategy') === 'clone') {
cd('{{release_path}}');
run("$git clone -l $bare .");
run("$git checkout --force $target");
2021-09-24 16:16:15 +02:00
} else {
throw new ConfigurationException(parse("Unknown `update_code_strategy` option: {{update_code_strategy}}."));
}
2021-03-15 01:35:06 +03:00
2021-10-09 17:52:01 +02:00
// Save git revision in REVISION file.
$rev = escapeshellarg(run("$git rev-list $target -1"));
2021-09-23 02:09:55 +02:00
run("echo $rev > {{release_path}}/REVISION");
2016-11-19 15:13:32 +07:00
});