From 4bc92927f7ede6613e549c71575768c41b8d9be6 Mon Sep 17 00:00:00 2001 From: Tom Scott-Malden Date: Mon, 21 Nov 2022 11:57:39 +0000 Subject: [PATCH 1/3] DD#0000: feat: Created magento2 deploy code to update cacheKeyId Referece Issue: https://github.com/davidalger/capistrano-magento2/issues/151 --- docs/recipe/magento2.md | 7 +++++++ recipe/magento2.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/docs/recipe/magento2.md b/docs/recipe/magento2.md index ff123ec8e..f1099f292 100644 --- a/docs/recipe/magento2.md +++ b/docs/recipe/magento2.md @@ -35,6 +35,7 @@ The [deploy](#deploy) task of **Magento 2** consists of: * [deploy:release](/docs/recipe/deploy/release.md#deployrelease) – Prepares release * [deploy:update_code](/docs/recipe/deploy/update_code.md#deployupdate_code) – Updates code * [deploy:shared](/docs/recipe/deploy/shared.md#deployshared) – Creates symlinks for shared files and dirs + * [magento:set_cache_prefix](/docs/recipe/magento2.md#magentoset_cache_prefix) * [deploy:writable](/docs/recipe/deploy/writable.md#deploywritable) – Makes writable dirs * [deploy:vendors](/docs/recipe/deploy/vendors.md#deployvendors) – Installs vendors * [deploy:clear_paths](/docs/recipe/deploy/clear_paths.md#deployclear_paths) – Cleanup files and/or directories @@ -216,6 +217,12 @@ true ## Tasks +### magento:set_cache_prefix +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L194) + +Copy env.php and update the cache id_prefix so that your deployment runs on a fresh cache. +Developed from this issue surfaced in capistrano: [capistrano issue](https://github.com/davidalger/capistrano-magento2/issues/151) + ### magento:compile [Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L92) diff --git a/recipe/magento2.php b/recipe/magento2.php index 84667bffb..635cf94bd 100644 --- a/recipe/magento2.php +++ b/recipe/magento2.php @@ -190,6 +190,34 @@ } }); +// Deploy without setting maintenance mode if possible +desc('Update cache id_prefix'); +task('magento:set_cache_prefix', function () { + //check and only run if env.php is a symlink + if (test('[ -L {{release_or_current_path}}/app/etc/env.php ]')) { + //get local temp file name + $tmpFilename = tempnam(sys_get_temp_dir(), 'tmp_settings_'); + //download env.php to local temp file + download("{{deploy_path}}/shared/app/etc/env.php", $tmpFilename, ['progress_bar' => false]); + $envConfig = include($tmpFilename); + $prefixUpdate = get('release_name') . '_'; + //update id_prefix to include release name + $envConfig["cache"]["frontend"]["default"]["id_prefix"] = $envConfig["cache"]["frontend"]["default"]["id_prefix"] . $prefixUpdate; + $envConfig["cache"]["frontend"]["page_cache"]["id_prefix"] = $envConfig["cache"]["frontend"]["page_cache"]["id_prefix"] . $prefixUpdate; + + //create temporary config file locally + $envConfigStr = " false]); + //delete local temp file + unlink($tmpFilename); + } +}); +after('deploy:shared', 'magento:set_cache_prefix'); + desc('Flushes Magento Cache'); task('magento:cache:flush', function () { run("{{bin/php}} {{release_or_current_path}}/bin/magento cache:flush"); From 2789436fd26d2c5423731938b4da56b6758668f7 Mon Sep 17 00:00:00 2001 From: Tom Scott-Malden Date: Mon, 21 Nov 2022 12:01:28 +0000 Subject: [PATCH 2/3] DD#0000: feat: Updated docs --- docs/recipe/magento2.md | 24 +++++++++++++----------- recipe/magento2.php | 5 ++++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/docs/recipe/magento2.md b/docs/recipe/magento2.md index f1099f292..0c713dc5c 100644 --- a/docs/recipe/magento2.md +++ b/docs/recipe/magento2.md @@ -35,7 +35,6 @@ The [deploy](#deploy) task of **Magento 2** consists of: * [deploy:release](/docs/recipe/deploy/release.md#deployrelease) – Prepares release * [deploy:update_code](/docs/recipe/deploy/update_code.md#deployupdate_code) – Updates code * [deploy:shared](/docs/recipe/deploy/shared.md#deployshared) – Creates symlinks for shared files and dirs - * [magento:set_cache_prefix](/docs/recipe/magento2.md#magentoset_cache_prefix) * [deploy:writable](/docs/recipe/deploy/writable.md#deploywritable) – Makes writable dirs * [deploy:vendors](/docs/recipe/deploy/vendors.md#deployvendors) – Installs vendors * [deploy:clear_paths](/docs/recipe/deploy/clear_paths.md#deployclear_paths) – Cleanup files and/or directories @@ -217,12 +216,6 @@ true ## Tasks -### magento:set_cache_prefix -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L194) - -Copy env.php and update the cache id_prefix so that your deployment runs on a fresh cache. -Developed from this issue surfaced in capistrano: [capistrano issue](https://github.com/davidalger/capistrano-magento2/issues/151) - ### magento:compile [Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L92) @@ -279,8 +272,17 @@ Upgrades magento database. +### magento:set_cache_prefix +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L198) + +Update cache id_prefix. + +Update cache ip_prefix on deploy so that your are compiling against a fresh cache +Reference Issue: https://github.com/davidalger/capistrano-magento2/issues/151 + + ### magento:cache:flush -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L194) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L225) Flushes Magento Cache. @@ -288,7 +290,7 @@ Flushes Magento Cache. ### deploy:magento -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L199) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L230) Magento2 deployment operations. @@ -303,7 +305,7 @@ This task is group task which contains next tasks: ### magento:build -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L207) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L238) Magento2 build operations. @@ -316,7 +318,7 @@ This task is group task which contains next tasks: ### deploy -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L213) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L244) Deploys your project. diff --git a/recipe/magento2.php b/recipe/magento2.php index 635cf94bd..5b2687527 100644 --- a/recipe/magento2.php +++ b/recipe/magento2.php @@ -190,7 +190,10 @@ } }); -// Deploy without setting maintenance mode if possible +/** + * Update cache ip_prefix on deploy so that your are compiling against a fresh cache + * Reference Issue: https://github.com/davidalger/capistrano-magento2/issues/151 +**/ desc('Update cache id_prefix'); task('magento:set_cache_prefix', function () { //check and only run if env.php is a symlink From d79137cf29bf72af51275bf2c35b8a5c3df284e9 Mon Sep 17 00:00:00 2001 From: Tom Scott-Malden Date: Tue, 22 Nov 2022 14:37:24 +0000 Subject: [PATCH 3/3] DD#0000: feat: Updated code so that config updates persist between each deployment --- docs/recipe/magento2.md | 20 +++++++---- recipe/magento2.php | 76 ++++++++++++++++++++++++++++------------- 2 files changed, 67 insertions(+), 29 deletions(-) diff --git a/docs/recipe/magento2.md b/docs/recipe/magento2.md index 0c713dc5c..7192c9bae 100644 --- a/docs/recipe/magento2.md +++ b/docs/recipe/magento2.md @@ -273,16 +273,24 @@ Upgrades magento database. ### magento:set_cache_prefix -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L198) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L199) Update cache id_prefix. -Update cache ip_prefix on deploy so that your are compiling against a fresh cache +Update cache ip_prefix on deploy so that you are compiling against a fresh cache Reference Issue: https://github.com/davidalger/capistrano-magento2/issues/151 +### magento:cleanup_cache_prefix +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L241) + +Cleanup cache id_prefix env files. + +After successful deployment, move the tmp_env.php file to env.php ready for next deployment + + ### magento:cache:flush -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L225) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L255) Flushes Magento Cache. @@ -290,7 +298,7 @@ Flushes Magento Cache. ### deploy:magento -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L230) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L260) Magento2 deployment operations. @@ -305,7 +313,7 @@ This task is group task which contains next tasks: ### magento:build -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L238) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L268) Magento2 build operations. @@ -318,7 +326,7 @@ This task is group task which contains next tasks: ### deploy -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L244) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L274) Deploys your project. diff --git a/recipe/magento2.php b/recipe/magento2.php index 5b2687527..8d669982c 100644 --- a/recipe/magento2.php +++ b/recipe/magento2.php @@ -190,37 +190,67 @@ } }); + /** - * Update cache ip_prefix on deploy so that your are compiling against a fresh cache + * Update cache ip_prefix on deploy so that you are compiling against a fresh cache * Reference Issue: https://github.com/davidalger/capistrano-magento2/issues/151 -**/ + **/ desc('Update cache id_prefix'); task('magento:set_cache_prefix', function () { - //check and only run if env.php is a symlink - if (test('[ -L {{release_or_current_path}}/app/etc/env.php ]')) { - //get local temp file name - $tmpFilename = tempnam(sys_get_temp_dir(), 'tmp_settings_'); - //download env.php to local temp file - download("{{deploy_path}}/shared/app/etc/env.php", $tmpFilename, ['progress_bar' => false]); - $envConfig = include($tmpFilename); - $prefixUpdate = get('release_name') . '_'; - //update id_prefix to include release name - $envConfig["cache"]["frontend"]["default"]["id_prefix"] = $envConfig["cache"]["frontend"]["default"]["id_prefix"] . $prefixUpdate; - $envConfig["cache"]["frontend"]["page_cache"]["id_prefix"] = $envConfig["cache"]["frontend"]["page_cache"]["id_prefix"] . $prefixUpdate; - - //create temporary config file locally - $envConfigStr = " false]); - //delete local temp file - unlink($tmpFilename); + $actualConfigFilePath = 'app/etc/env.php'; + $tmpConfigFilePath = 'app/etc/env_tmp.php'; + //get local temp file name + $tmpFilename = tempnam(sys_get_temp_dir(), 'tmp_settings_'); + //download env.php to local temp file + download("{{deploy_path}}/shared/$actualConfigFilePath", $tmpFilename, ['progress_bar' => false]); + $envConfig = include($tmpFilename); + $prefixUpdate = get('release_name') . '_'; + + //create original id_prefix so that we're not continually adding to the same string + if (!isset($envConfig["cache"]["frontend"]["default"]["orig_id_prefix"])) { + $envConfig["cache"]["frontend"]["default"]["orig_id_prefix"] = $envConfig["cache"]["frontend"]["default"]["id_prefix"]; + } + if (!isset($envConfig["cache"]["frontend"]["page_cache"]["orig_id_prefix"])) { + $envConfig["cache"]["frontend"]["page_cache"]["orig_id_prefix"] = $envConfig["cache"]["frontend"]["page_cache"]["id_prefix"]; } + //update id_prefix to include release name + $envConfig["cache"]["frontend"]["default"]["id_prefix"] = $envConfig["cache"]["frontend"]["default"]["orig_id_prefix"] . $prefixUpdate; + $envConfig["cache"]["frontend"]["page_cache"]["id_prefix"] = $envConfig["cache"]["frontend"]["page_cache"]["orig_id_prefix"] . $prefixUpdate; + + //create temporary config file locally + $envConfigStr = " false]); + //delete local temp file + unlink($tmpFilename); + //delete the remote symlink for env.php + run("rm {{release_or_current_path}}/$actualConfigFilePath"); + //link the env to the tmp version + // Touch shared + run("[ -f {{deploy_path}}/shared/$tmpConfigFilePath ] || touch {{deploy_path}}/shared/$tmpConfigFilePath"); + // Symlink shared dir to release dir + run("{{bin/symlink}} {{deploy_path}}/shared/$tmpConfigFilePath {{release_path}}/$actualConfigFilePath"); }); after('deploy:shared', 'magento:set_cache_prefix'); +/** + * After successful deployment, move the tmp_env.php file to env.php ready for next deployment + */ +desc('Cleanup cache id_prefix env files'); +task('magento:cleanup_cache_prefix', function () { + $actualConfigFilePath = 'app/etc/env.php'; + $tmpConfigFilePath = 'app/etc/env_tmp.php'; + run("rm {{deploy_path}}/shared/$actualConfigFilePath"); + run("rm {{release_or_current_path}}/$actualConfigFilePath"); + run("mv {{deploy_path}}/shared/$tmpConfigFilePath {{deploy_path}}/shared/$actualConfigFilePath"); + // Touch shared + run("[ -f {{deploy_path}}/shared/$actualConfigFilePath ] || touch {{deploy_path}}/shared/$actualConfigFilePath"); + // Symlink shared dir to release dir + run("{{bin/symlink}} {{deploy_path}}/shared/$actualConfigFilePath {{release_path}}/$actualConfigFilePath"); +}); +after('deploy:magento', 'magento:cleanup_cache_prefix'); + desc('Flushes Magento Cache'); task('magento:cache:flush', function () { run("{{bin/php}} {{release_or_current_path}}/bin/magento cache:flush");