deployer/recipe/deploy/update_code.php

107 lines
3.2 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;
/**
2020-10-02 00:11:13 +02:00
* Determines which branch to deploy. Can be overridden with cli option `--branch`.
* If not specified, will get current git HEAD branch as default branch to deploy.
*/
set('branch', function () {
if (input()->hasOption('branch') && !empty(input()->getOption('branch'))) {
return input()->getOption('branch');
}
return null;
});
2021-09-24 15:08:42 +02:00
// Automatically populate `known_hosts` file based on {{repository}} config.
set('auto_ssh_keygen', true);
2021-09-24 16:16:15 +02:00
// Sets deploy:update_code strategy.
// Can we one of:
// - 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');
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 () {
2019-08-12 20:59:59 +03:00
$repository = get('repository');
2016-11-19 15:13:32 +07:00
$branch = get('branch');
$git = get('bin/git');
$at = 'HEAD';
2016-11-19 15:13:32 +07:00
if (!empty($branch)) {
2021-03-15 01:35:06 +03:00
$at = $branch;
2016-11-19 15:13:32 +07:00
}
2021-03-15 01:35:06 +03:00
// If option `tag` is set.
2016-11-19 15:13:32 +07:00
if (input()->hasOption('tag')) {
$tag = input()->getOption('tag');
if (!empty($tag)) {
2021-03-15 01:35:06 +03:00
$at = $tag;
2016-11-19 15:13:32 +07:00
}
}
2021-03-15 01:35:06 +03:00
// If option `tag` is not set and option `revision` is set.
if (empty($tag)) {
2016-11-19 15:13:32 +07:00
$revision = input()->getOption('revision');
if (!empty($revision)) {
2021-03-15 01:35:06 +03:00
$at = $revision;
2016-11-19 15:13:32 +07:00
}
}
2017-11-27 11:18:22 +01:00
2021-09-24 15:08:42 +02:00
if (get('auto_ssh_keygen')) {
$url = parse_url($repository);
if (isset($url['scheme']) && $url['scheme'] === 'ssh') {
$host = $url['host'];
$port = $url['port'] ?? '22';
} else if (preg_match('/(?:@|\/\/)([^\/:]+)(?:\:(\d{1,5}))?/', $repository, $matches)) {
$host = $matches[1];
$port = $matches[2] ?? '22';
} else {
2021-09-24 15:43:01 +02:00
warning("Can't parse repository url ($repository).");
2021-09-24 15:08:42 +02:00
}
if (isset($host) && isset($port)) {
run("ssh-keygen -F $host:$port || ssh-keyscan -p $port -H $host >> ~/.ssh/known_hosts");
} else {
2021-09-24 15:41:55 +02:00
warning("Please, make sure your server can clone the repo.");
2020-04-25 23:00:08 +03:00
}
}
2021-03-15 01:35:06 +03:00
$bare = parse('{{deploy_path}}/.dep/repo');
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");
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");
2021-09-24 16:16:15 +02:00
2021-10-12 22:49:34 +02:00
// Copy to release_path.a
2021-09-24 16:16:15 +02:00
if (get('update_code_strategy') === 'archive') {
run("$git archive $at | 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 $at");
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.
2021-09-23 02:09:55 +02:00
$rev = escapeshellarg(run("$git rev-list $at -1"));
run("echo $rev > {{release_path}}/REVISION");
2016-11-19 15:13:32 +07:00
});