Skip to content

Commit

Permalink
Deployer 7 nutzen, Optimierung für CI-Nutzung
Browse files Browse the repository at this point in the history
  • Loading branch information
gharlan committed Sep 29, 2023
1 parent a974a3e commit bb248bd
Show file tree
Hide file tree
Showing 17 changed files with 1,087 additions and 671 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
},

"require-dev": {
"deployer/deployer": "^6.9",
"deployer/deployer": "^7.3.1",
"friendsofphp/php-cs-fixer": "^3.34.0",
"redaxo/php-cs-fixer-config": "^2.2.0",
"redaxo/source": "^5.14.3"
"redaxo/php-cs-fixer-config": "^2.2.0"
},

"autoload": {
Expand Down
1,092 changes: 835 additions & 257 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use RuntimeException;

$version = ltrim(Deployer::get()->getConsole()->getVersion(), 'v');
if (6 !== (int) $version) {
throw new RuntimeException('YDeploy 1.x requires Deployer 6.x, but Deployer ' . $version . ' is used');
if (7 !== (int) $version) {
throw new RuntimeException('YDeploy 2.x requires Deployer 7.x, but Deployer ' . $version . ' is used');
}

require 'recipe/common.php';
Expand Down
59 changes: 51 additions & 8 deletions deployer/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,51 @@

use Deployer\Task\Context;

use Symfony\Component\Console\Input\ArgvInput;

use function dirname;
use function in_array;
use function strlen;

$baseDir = dirname(__DIR__, 5);
$rootPath = dirname(DEPLOYER_DEPLOY_FILE); // @phpstan-ignore-line
$command = (new ArgvInput())->getFirstArgument();
$localBuildDir = 'setup' !== $command && !getenv('CI');

Deployer::get()->hosts = new class($rootPath, $localBuildDir) extends Host\HostCollection {
public function __construct(
private readonly string $rootPath,
private readonly bool $buildDir,
) {}

public function has(string $name): bool
{
if ('local' === $name) {
return true;
}

return parent::has($name);
}

public function get(string $name): Host\Host
{
if ('local' === $name && !parent::has($name)) {
localhost('local')
->set('root_path', $this->rootPath)
->set('deploy_path', $this->rootPath . ($this->buildDir ? '/.build' : ''))
->set('release_path', $this->rootPath . ($this->buildDir ? '/.build/release' : ''))
->set('current_path', '{{release_path}}')
;
}

return parent::get($name);
}
};

if (in_array($command, ['build', 'setup', 'worker'], true)) {
host('local');
}

$baseDir = $rootPath;
if (str_starts_with($baseDir, getcwd())) {
$baseDir = substr($baseDir, strlen(getcwd()));
$baseDir = ltrim($baseDir . '/', '/');
Expand Down Expand Up @@ -42,26 +83,28 @@
]);

set('clear_paths', [
'.idea',
'gulpfile.js',
'node_modules',
'.gitignore',
'.gitlab-ci.yml',
'deploy.php',
'.php-cs-fixer.dist.php',
'package.json',
'README.md',
'webpack.config.js',
'yarn.lock',
'REVISION',
]);

set('url', static function () {
return 'https://' . Context::get()->getHost()->getRealHostname();
return 'https://' . Context::get()->getHost()->getHostname();
});

set('allow_anonymous_stats', false);

after('deploy:failed', 'deploy:unlock');

set('bin/mysql', static function () {
return locateBinaryPath('mysql');
return which('mysql');
});

set('bin/mysqldump', static function () {
return locateBinaryPath('mysqldump');
return which('mysqldump');
});
29 changes: 12 additions & 17 deletions deployer/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
namespace YDeploy;

use Deployer\Host\Host;
use Deployer\Host\Localhost;
use Deployer\Task\Context;

use function Deployer\cd;
use function Deployer\download;
use function Deployer\get;
use function Deployer\on;
use function Deployer\upload;

function uploadContent(string $destination, string $content): void
{
if (!empty($workingPath = get('working_path', ''))) {
$destination = "$workingPath/$destination";
} else {
$destination = "{{release_or_current_path}}/$destination";
}

$path = tempnam(getcwd() . '/' . get('data_dir') . '/addons/ydeploy', 'tmp');
file_put_contents($path, $content);

try {
upload($path, $destination);
upload($path, $destination, ['progress_bar' => false]);
} finally {
unlink($path);
}
Expand All @@ -31,11 +31,13 @@ function downloadContent(string $source): string
{
if (!empty($workingPath = get('working_path', ''))) {
$source = "$workingPath/$source";
} else {
$source = "{{release_or_current_path}}/$source";
}

$path = tempnam(getcwd() . '/' . get('data_dir') . '/addons/ydeploy', 'tmp');

download($source, $path);
download($source, $path, ['progress_bar' => false]);
$content = file_get_contents($path);
unlink($path);

Expand All @@ -44,18 +46,11 @@ function downloadContent(string $source): string

function onHost(Host $host, callable $callback)
{
$input = Context::has() ? Context::get()->getInput() : null;
$output = Context::has() ? Context::get()->getOutput() : null;
$return = null;

Context::push(new Context($host, $input, $output));
on($host, static function () use ($callback, &$return) {
$return = $callback();
});

try {
if (!$host instanceof Localhost) {
cd('{{release_path}}');
}

return $callback($host);
} finally {
Context::pop();
}
return $return;
}
21 changes: 7 additions & 14 deletions deployer/tasks/build.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@

namespace Deployer;

desc('Prepare the next release in local subdir ".build"');
task('build', static function () {
set('deploy_path', getcwd() . '/.build');
set('keep_releases', 1);

invoke('build:info');
invoke('deploy:prepare');
invoke('deploy:release');
invoke('deploy:update_code');
invoke('build:assets');
invoke('deploy:clear_paths');
invoke('deploy:symlink');
invoke('cleanup');
})->shallow()->local();
desc('Prepare the next release locally');
task('build', [
'deploy:info',
'build:setup',
'build:assets',
'deploy:clear_paths',
])->once();
28 changes: 20 additions & 8 deletions deployer/tasks/build/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@

namespace Deployer;

set('yarn', false);
set('gulp', false);
set('gulp_options', '');
set('assets_install', false);
set('assets_build', false);

desc('Load (yarn) and build (gulp) assets');
desc('Load and build assets');
task('build:assets', static function () {
$install = get('assets_install');

if (!$install) {
return;
}

cd('{{release_path}}');

if (get('yarn')) {
run('yarn');
$isLocal = !getenv('CI');
if ($isLocal && test('[ -d {{deploy_path}}/.node_modules ]')) {
run('mv {{deploy_path}}/.node_modules node_modules');
}

run($install);

if ($build = get('assets_build')) {
run($build);
}

if (get('gulp')) {
run('gulp {{gulp_options}}');
if ($isLocal) {
run('mv node_modules {{deploy_path}}/.node_modules');
}
});
27 changes: 0 additions & 27 deletions deployer/tasks/build/info.php

This file was deleted.

22 changes: 22 additions & 0 deletions deployer/tasks/build/setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Deployer;

use Deployer\Task\Context;
use RuntimeException;

desc('Prepare build step');
task('build:setup', static function () {
if ('local' !== Context::get()->getHost()->getAlias()) {
throw new RuntimeException('Task "build" can only be called on host "local"');
}

if (getenv('CI')) {
return;
}

run('rm -rf {{release_path}}');
run('mkdir -p {{release_path}}');

invoke('deploy:update_code');
});
6 changes: 5 additions & 1 deletion deployer/tasks/deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

desc('Deploy (build and release) to server');
task('deploy', [
'build',
'build:start',
'release',
]);

task('build:start', static function () {
on(host('local'), static fn () => invoke('build'));
})->once()->hidden();
2 changes: 1 addition & 1 deletion deployer/tasks/deploy/dump_info.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
desc('Dump info about current deployment');
task('deploy:dump_info', static function () {
$infos = [
'host' => get('hostname'),
'host' => get('alias'),
'stage' => get('stage', false) ?: null,
'timestamp' => time(),
'branch' => runLocally('{{bin/git}} -C .build rev-parse --abbrev-ref HEAD'),
Expand Down
Loading

0 comments on commit bb248bd

Please sign in to comment.