From b4f7385a13d79de6c9ca659268bd659239d9ce00 Mon Sep 17 00:00:00 2001 From: Miljenko Muha Date: Wed, 23 Aug 2023 15:38:29 +0200 Subject: [PATCH 1/5] NGGW-103 first draft --- composer.json | 4 ++-- deploy.php | 17 ++++++++++------- deploy/hosts.php | 12 ++++++------ deploy/tasks/assets.php | 12 ++++++------ deploy/tasks/git.php | 2 +- deploy/tasks/graphql.php | 4 ++-- deploy/tasks/rsync.php | 4 ++-- deploy/tasks/server.php | 2 +- deploy/tasks/slack.php | 2 +- 9 files changed, 31 insertions(+), 28 deletions(-) diff --git a/composer.json b/composer.json index 0da432140..6080064c2 100644 --- a/composer.json +++ b/composer.json @@ -60,13 +60,13 @@ "symfony/web-profiler-bundle": "5.4.*", "netgen/git-hooks": "^2.3", - "deployer/recipes": "^6.2", "phpstan/phpstan": "^1.9", "phpstan/extension-installer": "^1.2", "phpstan/phpstan-strict-rules": "^1.4", "phpstan/phpstan-symfony": "^1.2", "phpstan/phpstan-doctrine": "^1.3", - "php-cs-fixer/shim": "^3.20" + "php-cs-fixer/shim": "^3.20", + "deployer/deployer": "^7.3" }, "config": { "allow-plugins": { diff --git a/deploy.php b/deploy.php index 6c4847d6a..88e719911 100644 --- a/deploy.php +++ b/deploy.php @@ -2,11 +2,8 @@ namespace Deployer; -require 'recipe/symfony4.php'; -require 'vendor/deployer/recipes/recipe/cachetool.php'; -require 'vendor/deployer/recipes/recipe/rsync.php'; -require 'vendor/deployer/recipes/recipe/sentry.php'; -require 'vendor/deployer/recipes/recipe/slack.php'; +require 'recipe/symfony.php'; +require 'contrib/rsync.php'; require __DIR__ . '/deploy/hosts.php'; require __DIR__ . '/deploy/tasks/server.php'; @@ -24,6 +21,8 @@ // optional: slack integration //require __DIR__ . '/deploy/tasks/slack.php'; +putenv("DEPLOYER_ROOT=. vendor/bin/dep taskname`"); + /** Parameters */ set('git_tty', true); @@ -39,6 +38,10 @@ 'commits' => getCommitsInformation() ]); +set('writable_recursive', true); + +set('update_code_strategy', 'clone'); + /** Execution */ task('deploy', [ 'deploy:confirm', @@ -74,13 +77,13 @@ 'cachetool:clear:opcache', // Cleanup and finish the deploy 'deploy:unlock', - 'cleanup', + 'deploy:cleanup', ])->desc('Deploy your project'); // after successful deploy after('deploy', 'httpcache:invalidate'); after('deploy', 'deploy:log:remote'); -after('deploy', 'success'); +after('deploy', 'deploy:success'); // If deploy fails automatically unlock. after('deploy:failed', 'deploy:unlock'); diff --git a/deploy/hosts.php b/deploy/hosts.php index 89f9d5206..64dd3586b 100644 --- a/deploy/hosts.php +++ b/deploy/hosts.php @@ -3,20 +3,20 @@ namespace Deployer; host('prod') - ->hostname('some.server.com') + ->setHostname('some.server.com') ->set('deploy_path', '/home/some/example/sites/example.com/htdocs') - ->stage('prod') - ->user('exampleweb') + ->setLabels(['stage' => 'prod']) + ->setRemoteUser('exampleweb') ->set('http_user', 'exampleweb') ->set('cachetool', '/var/run/php/exampleweb.sock') ->add('varnish_ban_hosts', ['www.example.com']) ; host('stage') - ->hostname('192.0.2.10') + ->setHostname('192.0.2.10') ->set('deploy_path', '/var/www/my/project') - ->stage('stage') - ->user('deployer') + ->setLabels(['stage' => 'stage']) + ->setRemoteUser('deployer') ->set('http_user', 'www-data') // ->set('branch', 'test-branch') // by default, master is used, this is the way to set up different branch per host ->set('cachetool', '/var/run/php/exampleweb.sock') diff --git a/deploy/tasks/assets.php b/deploy/tasks/assets.php index 9b62c6bc1..2e966628d 100644 --- a/deploy/tasks/assets.php +++ b/deploy/tasks/assets.php @@ -31,19 +31,19 @@ writeln('Checking for changes in asset files. If this fails, commit or stash your changes before deploying.'); $assetResourcePaths = get('asset_resource_paths'); foreach ($assetResourcePaths as $resourcePath) { - run("git diff --exit-code {$resourcePath}"); + runLocally("git diff --exit-code {$resourcePath}"); } $installCmd = get('asset_install_command'); - run($installCmd); + runLocally($installCmd); $buildCmd = get('asset_build_command'); - run($buildCmd); -})->local(); + runLocally($buildCmd); +})->once(); task('app:assets:ibexa:build', function() { - run("{{asset_ibexa_build_command}}"); -})->local(); + runLocally("{{asset_ibexa_build_command}}"); +})->once(); task('app:assets:upload', function() { $assetPaths = get('asset_build_paths'); diff --git a/deploy/tasks/git.php b/deploy/tasks/git.php index 944ed4872..4fbd8d87d 100644 --- a/deploy/tasks/git.php +++ b/deploy/tasks/git.php @@ -18,4 +18,4 @@ runLocally("$git tag -a $tag -m '" . $message . "' " . trim($commitHash)); runLocally("$git push origin $tag"); -})->onStage('prod'); +})->select('stage=prod'); diff --git a/deploy/tasks/graphql.php b/deploy/tasks/graphql.php index 3ebb0eefb..cc4e57493 100644 --- a/deploy/tasks/graphql.php +++ b/deploy/tasks/graphql.php @@ -10,8 +10,8 @@ ]); task('app:graphql:generate', function () { - run("{{graphql_generate_command}}"); -})->local(); + runLocally("{{graphql_generate_command}}"); +})->once(); task('app:graphql:upload', function () { diff --git a/deploy/tasks/rsync.php b/deploy/tasks/rsync.php index c9ac0b55e..e6717b703 100644 --- a/deploy/tasks/rsync.php +++ b/deploy/tasks/rsync.php @@ -39,10 +39,10 @@ return; } - $host = $server->getRealHostname(); + $host = $server->getHostname(); $port = $server->getPort() ? ' -p' . $server->getPort() : ''; $sshArguments = $server->getSshArguments(); - $user = !$server->getUser() ? '' : $server->getUser() . '@'; + $user = !$server->getRemoteUser() ? '' : $server->getRemoteUser() . '@'; runLocally("rsync -{$config['flags']} -e 'ssh$port $sshArguments' {{rsync_options}}{{rsync_includes}}{{rsync_excludes}}{{rsync_filter}} '$user$host:$src/' '$dst/'", $config); }); diff --git a/deploy/tasks/server.php b/deploy/tasks/server.php index 2f80fec17..ea6cd6fd5 100644 --- a/deploy/tasks/server.php +++ b/deploy/tasks/server.php @@ -30,7 +30,7 @@ if(!askConfirmation($question)) { die("Ok, no deploy then.\n"); } -})->onStage('prod'); +})->select('stage=prod'); desc('Upload appropriate .env.local file to the server'); task('server:upload_env', function() { diff --git a/deploy/tasks/slack.php b/deploy/tasks/slack.php index 32df6b83c..2df388c53 100644 --- a/deploy/tasks/slack.php +++ b/deploy/tasks/slack.php @@ -8,5 +8,5 @@ /** execution */ before('deploy', 'slack:notify'); -after('success', 'slack:notify:success'); +after('deploy:success', 'slack:notify:success'); after('deploy:failed', 'slack:notify:failure'); From bd6b725cc111df90dad9189bdf9894a18ec00b08 Mon Sep 17 00:00:00 2001 From: Miljenko Muha Date: Wed, 23 Aug 2023 17:12:05 +0200 Subject: [PATCH 2/5] NGGW-103 unnecessary env variable removed --- deploy.php | 1 - 1 file changed, 1 deletion(-) diff --git a/deploy.php b/deploy.php index 88e719911..2fd336990 100644 --- a/deploy.php +++ b/deploy.php @@ -21,7 +21,6 @@ // optional: slack integration //require __DIR__ . '/deploy/tasks/slack.php'; -putenv("DEPLOYER_ROOT=. vendor/bin/dep taskname`"); /** Parameters */ set('git_tty', true); From ae97bddd0810df4238b9dbef49ef6404013c8f8e Mon Sep 17 00:00:00 2001 From: Miljenko Muha Date: Wed, 23 Aug 2023 19:34:40 +0200 Subject: [PATCH 3/5] NGGW-103 recipes required, deploy:cache:warmup commented out as it no longer exists in vendor --- deploy.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/deploy.php b/deploy.php index 2fd336990..b6b67b136 100644 --- a/deploy.php +++ b/deploy.php @@ -4,6 +4,8 @@ require 'recipe/symfony.php'; require 'contrib/rsync.php'; +require 'contrib/sentry.php'; +require 'contrib/cachetool.php'; require __DIR__ . '/deploy/hosts.php'; require __DIR__ . '/deploy/tasks/server.php'; @@ -64,7 +66,7 @@ 'deploy:sentry', 'git:tag:add', 'deploy:cache:clear', - 'deploy:cache:warmup', + //'deploy:cache:warmup', 'deploy:writable', // Migrate database before symlink new release. // 'database:migrate', From da1a7076cec3eb73b746526455e7f2bf7b10d856 Mon Sep 17 00:00:00 2001 From: Miljenko Muha Date: Tue, 5 Sep 2023 18:17:24 +0200 Subject: [PATCH 4/5] NGGW-103 deploy task works until cachetool:clear:opcache task --- deploy.php | 7 ++----- deploy/tasks/assets.php | 4 ++-- deploy/tasks/overrides.php | 7 +++++++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/deploy.php b/deploy.php index b6b67b136..23fadff40 100644 --- a/deploy.php +++ b/deploy.php @@ -27,6 +27,8 @@ /** Parameters */ set('git_tty', true); +set('git_ssh_command', 'ssh'); + add('copy_dirs', ['vendor']); set('use_relative_symlink', false); @@ -50,11 +52,7 @@ 'server:upload_env', 'deploy:info', 'deploy:prepare', - 'deploy:lock', - 'deploy:release', - 'deploy:update_code', 'deploy:clear_paths', - 'deploy:shared', // build and upload assets 'app:assets:deploy', 'app:graphql:deploy', @@ -66,7 +64,6 @@ 'deploy:sentry', 'git:tag:add', 'deploy:cache:clear', - //'deploy:cache:warmup', 'deploy:writable', // Migrate database before symlink new release. // 'database:migrate', diff --git a/deploy/tasks/assets.php b/deploy/tasks/assets.php index 2e966628d..aacc08bcd 100644 --- a/deploy/tasks/assets.php +++ b/deploy/tasks/assets.php @@ -42,7 +42,7 @@ })->once(); task('app:assets:ibexa:build', function() { - runLocally("{{asset_ibexa_build_command}}"); + runLocally("{{bin/php}} {{asset_ibexa_build_command}}"); })->once(); task('app:assets:upload', function() { @@ -51,7 +51,7 @@ $config = []; foreach ($excludedPaths as $excludedPath) { - $config['options'][] = "--exclude {$excludedPath}"; + $config['options'][] = "--exclude= {$excludedPath}"; } foreach ($assetPaths as $path) { diff --git a/deploy/tasks/overrides.php b/deploy/tasks/overrides.php index fad8e3210..bec635747 100644 --- a/deploy/tasks/overrides.php +++ b/deploy/tasks/overrides.php @@ -27,3 +27,10 @@ writeln("No more releases you can revert to."); } }); + +task('deploy:vendors', function () { + if (!commandExist('unzip')) { + warning('To speed up composer installation setup "unzip" command with PHP zip extension.'); + } + run('cd {{release_or_current_path}} && {{bin/php}} {{bin/composer}} {{composer_action}} {{composer_options}} 2>&1'); +}); From 7590dc606ab2cb9b51ccb397e3b5a629222153fe Mon Sep 17 00:00:00 2001 From: Miljenko Muha Date: Wed, 6 Sep 2023 16:45:46 +0200 Subject: [PATCH 5/5] NGGW-103 deployment works --- deploy/files/.env.local.prod | 8 ++++---- deploy/hosts.php | 20 +++++++++++--------- deploy/tasks/assets.php | 2 +- deploy/tasks/database.php | 2 +- deploy/tasks/logs.php | 2 +- deploy/tasks/overrides.php | 16 +++------------- deploy/tasks/server.php | 14 ++++++++------ 7 files changed, 29 insertions(+), 35 deletions(-) diff --git a/deploy/files/.env.local.prod b/deploy/files/.env.local.prod index b19707977..abc7d425a 100644 --- a/deploy/files/.env.local.prod +++ b/deploy/files/.env.local.prod @@ -1,12 +1,12 @@ APP_ENV=prod APP_SECRET=ThisIbexaPlatformTokenIsNotSoSecret_PleaseChangeIt SERVER_ENVIRONMENT=prod -DATABASE_URL=mysql://root@localhost/ibexa +DATABASE_URL=mysql://root@127.0.0.1/ibexa SEARCH_ENGINE=legacy HTTPCACHE_PURGE_TYPE=local -HTTPCACHE_PURGE_SERVER=http://localhost:6081 +HTTPCACHE_PURGE_SERVER=http://127.0.0.1:6081 CACHE_POOL=cache.tagaware.filesystem -SOLR_DSN=http://localhost:8983/solr -MAILER_DSN=smtp://localhost +SOLR_DSN=http://127.0.0.1:8983/solr +MAILER_DSN=smtp://127.0.0.1 SENTRY_DSN= MAILER_LITE_API_KEY= diff --git a/deploy/hosts.php b/deploy/hosts.php index 64dd3586b..bb536d1b3 100644 --- a/deploy/hosts.php +++ b/deploy/hosts.php @@ -5,20 +5,22 @@ host('prod') ->setHostname('some.server.com') ->set('deploy_path', '/home/some/example/sites/example.com/htdocs') - ->setLabels(['stage' => 'prod']) + ->set('labels', ['stage' => 'prod']) ->setRemoteUser('exampleweb') ->set('http_user', 'exampleweb') - ->set('cachetool', '/var/run/php/exampleweb.sock') + ->set('cachetool', '/run/php/php8.1-fpm.sock') ->add('varnish_ban_hosts', ['www.example.com']) + ->set('symfony_env', 'prod') ; host('stage') - ->setHostname('192.0.2.10') - ->set('deploy_path', '/var/www/my/project') - ->setLabels(['stage' => 'stage']) - ->setRemoteUser('deployer') - ->set('http_user', 'www-data') + ->setHostname('some.server.com') + ->set('deploy_path', '/home/some/example/sites/example.com/htdocs') + ->set('labels', ['stage' => 'stage']) + ->setRemoteUser('exampleweb') + ->set('http_user', 'exampleweb') // ->set('branch', 'test-branch') // by default, master is used, this is the way to set up different branch per host - ->set('cachetool', '/var/run/php/exampleweb.sock') - ->add('varnish_ban_hosts', ['stage.example.com']) + ->set('cachetool', '/run/php/php8.1-fpm.sock') + ->add('varnish_ban_hosts', ['www.example.com']) + ->set('symfony_env', 'stage') ; diff --git a/deploy/tasks/assets.php b/deploy/tasks/assets.php index aacc08bcd..b1e11978e 100644 --- a/deploy/tasks/assets.php +++ b/deploy/tasks/assets.php @@ -42,7 +42,7 @@ })->once(); task('app:assets:ibexa:build', function() { - runLocally("{{bin/php}} {{asset_ibexa_build_command}}"); + runLocally('{{asset_ibexa_build_command}}'); })->once(); task('app:assets:upload', function() { diff --git a/deploy/tasks/database.php b/deploy/tasks/database.php index dc2776e67..fcc7ebd92 100644 --- a/deploy/tasks/database.php +++ b/deploy/tasks/database.php @@ -4,5 +4,5 @@ desc('Execute kaliop migrations'); task('database:kaliop:migrate', function () { - run('{{bin/php}} {{bin/console}} kaliop:migration:migrate --env={{symfony_env}}'); + run('{{bin/console}} kaliop:migration:migrate --env={{symfony_env}}'); }); diff --git a/deploy/tasks/logs.php b/deploy/tasks/logs.php index 295777502..0a41b297d 100644 --- a/deploy/tasks/logs.php +++ b/deploy/tasks/logs.php @@ -8,5 +8,5 @@ $result = run('cat '.$file); - write($result); + writeln($result); }); diff --git a/deploy/tasks/overrides.php b/deploy/tasks/overrides.php index bec635747..8dc01c3be 100644 --- a/deploy/tasks/overrides.php +++ b/deploy/tasks/overrides.php @@ -2,9 +2,6 @@ namespace Deployer; -set('bin/cachetool', function(){ - return 'cachetool-7.0.0.phar'; -}); // overridden to make sure public folder is correctly symlinked (L79) desc('Rollback to previous release'); @@ -20,17 +17,10 @@ // Remove release run("rm -rf {{deploy_path}}/releases/{$releases[0]}"); - if (isVerbose()) { - writeln("Rollback to `{$releases[1]}` release was successful."); - } + + writeln("Rollback to `{$releases[1]}` release was successful."); + } else { writeln("No more releases you can revert to."); } }); - -task('deploy:vendors', function () { - if (!commandExist('unzip')) { - warning('To speed up composer installation setup "unzip" command with PHP zip extension.'); - } - run('cd {{release_or_current_path}} && {{bin/php}} {{bin/composer}} {{composer_action}} {{composer_options}} 2>&1'); -}); diff --git a/deploy/tasks/server.php b/deploy/tasks/server.php index ea6cd6fd5..a80894d22 100644 --- a/deploy/tasks/server.php +++ b/deploy/tasks/server.php @@ -17,8 +17,9 @@ } $stage = null; - if (input()->hasArgument('stage')) { - $stage = input()->getArgument('stage'); + $labels = get('labels'); + if (array_key_exists('stage', $labels)) { + $stage = $labels['stage']; } $question = 'Please confirm deploy'; @@ -35,8 +36,9 @@ desc('Upload appropriate .env.local file to the server'); task('server:upload_env', function() { $stage = 'dev'; - if (input()->hasArgument('stage')) { - $stage = input()->getArgument('stage'); + $labels = get('labels'); + if (array_key_exists('stage', $labels)) { + $stage = $labels['stage']; } $path = get('deploy_path'); @@ -79,7 +81,7 @@ function invalidateFOSHttpCache($tag = null) { $tag = !empty($tag) ? $tag : get('http_invalidate_tag'); - run("{{bin/php}} {{bin/console}} fos:httpcache:invalidate:tag {$tag} {{console_options}}"); + run("{{bin/console}} fos:httpcache:invalidate:tag {$tag} {{console_options}}"); } desc('Invalidate content on varnish per host'); @@ -124,7 +126,7 @@ function invalidateFOSHttpCache($tag = null) task('crontab:list', function () { $result = run('crontab -l'); - write($result); + writeln($result); }); desc('Reindex solr');