From 7fa09a2299ab8d9690ecdbb49d6c5dde3f33d5cb Mon Sep 17 00:00:00 2001 From: Tom Scott-Malden Date: Thu, 30 Mar 2023 16:34:07 +0100 Subject: [PATCH] Magento 2: Cache key id prefix (#3453) --- composer.json | 3 +- composer.lock | 75 ++++++++++++++++++++++- docs/recipe/magento2.md | 130 ++++++++++++++++++++++++---------------- recipe/magento2.php | 57 ++++++++++++++++++ 4 files changed, 212 insertions(+), 53 deletions(-) diff --git a/composer.json b/composer.json index 4a26ba956..ccf5d749b 100644 --- a/composer.json +++ b/composer.json @@ -58,7 +58,8 @@ "phpunit/php-code-coverage": "^9.2", "phpunit/phpunit": "^9.3", "slevomat/coding-standard": "^7.0", - "squizlabs/php_codesniffer": "^3.5" + "squizlabs/php_codesniffer": "^3.5", + "symfony/var-exporter": "^5.4" }, "config": { "sort-packages": true, diff --git a/composer.lock b/composer.lock index 3042e95e4..1a78969ad 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2e4a876ba583ea7b5ddf4bebd46c0262", + "content-hash": "a8fcdbe293cf508305b9865ac9821da4", "packages": [ { "name": "evenement/evenement", @@ -4775,6 +4775,79 @@ }, "time": "2021-12-12T21:44:58+00:00" }, + { + "name": "symfony/var-exporter", + "version": "v5.4.21", + "source": { + "type": "git", + "url": "https://github.com/symfony/var-exporter.git", + "reference": "be74908a6942fdd331554b3cec27ff41b45ccad4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/var-exporter/zipball/be74908a6942fdd331554b3cec27ff41b45ccad4", + "reference": "be74908a6942fdd331554b3cec27ff41b45ccad4", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/polyfill-php80": "^1.16" + }, + "require-dev": { + "symfony/var-dumper": "^4.4.9|^5.0.9|^6.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\VarExporter\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Allows exporting any serializable PHP data structure to plain PHP code", + "homepage": "https://symfony.com", + "keywords": [ + "clone", + "construct", + "export", + "hydrate", + "instantiate", + "serialize" + ], + "support": { + "source": "https://github.com/symfony/var-exporter/tree/v5.4.21" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2023-02-21T19:46:44+00:00" + }, { "name": "theseer/tokenizer", "version": "1.2.1", diff --git a/docs/recipe/magento2.md b/docs/recipe/magento2.md index db112eb35..4f2247660 100644 --- a/docs/recipe/magento2.md +++ b/docs/recipe/magento2.md @@ -115,7 +115,7 @@ The magento2 recipe is based on the [common](/docs/recipe/common.md) recipe. ## Configuration ### static_content_locales -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L23) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L26) By default setup:static-content:deploy uses `en_US`. To change that, simply put `set('static_content_locales', 'en_US de_DE');` @@ -127,7 +127,7 @@ in you deployer script. ### magento_themes -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L40) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L43) You can also set the themes to run against. By default it'll deploy all themes - `add('magento_themes', ['Magento/luma', 'Magento/backend']);` @@ -151,14 +151,14 @@ set('magento_themes', [ ### static_deploy_options -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L45) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L48) Static content deployment options, e.g. '--no-parent' ### split_static_deployment -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L48) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L51) Deploy frontend and adminhtml together as default @@ -168,7 +168,7 @@ false ### static_content_locales_backend -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L51) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L54) Use the default languages for the backend as default @@ -178,7 +178,7 @@ Use the default languages for the backend as default ### magento_themes_backend -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L55) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L58) backend themes to deploy. Only used if split_static_deployment=true This setting supports the same options/structure as [magento_themes](/docs/recipe/magento2.md#magento_themes) @@ -189,7 +189,7 @@ This setting supports the same options/structure as [magento_themes](/docs/recip ### static_content_jobs -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L61) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L64) Also set the number of conccurent jobs to run. The default is 1 Update using: `set('static_content_jobs', '1');` @@ -200,7 +200,7 @@ Update using: `set('static_content_jobs', '1');` ### content_version -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L63) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L66) @@ -210,7 +210,7 @@ return time(); ### magento_dir -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L68) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L71) Magento directory relative to repository root. Use "." (default) if it is not located in a subdirectory @@ -220,7 +220,7 @@ Magento directory relative to repository root. Use "." (default) if it is not lo ### shared_files -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L71) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L74) Overrides [shared_files](/docs/recipe/deploy/shared.md#shared_files) from `recipe/deploy/shared.php`. @@ -235,7 +235,7 @@ Overrides [shared_files](/docs/recipe/deploy/shared.md#shared_files) from `recip ### shared_dirs -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L75) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L78) Overrides [shared_dirs](/docs/recipe/deploy/shared.md#shared_dirs) from `recipe/deploy/shared.php`. @@ -261,7 +261,7 @@ Overrides [shared_dirs](/docs/recipe/deploy/shared.md#shared_dirs) from `recipe/ ### writable_dirs -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L90) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L93) Overrides [writable_dirs](/docs/recipe/deploy/writable.md#writable_dirs) from `recipe/deploy/writable.php`. @@ -279,7 +279,7 @@ Overrides [writable_dirs](/docs/recipe/deploy/writable.md#writable_dirs) from `r ### clear_paths -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L97) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L100) Overrides [clear_paths](/docs/recipe/deploy/clear_paths.md#clear_paths) from `recipe/deploy/clear_paths.php`. @@ -298,7 +298,7 @@ Overrides [clear_paths](/docs/recipe/deploy/clear_paths.md#clear_paths) from `re ### bin/magento -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L106) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L109) @@ -308,7 +308,7 @@ Overrides [clear_paths](/docs/recipe/deploy/clear_paths.md#clear_paths) from `re ### magento_version -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L108) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L111) @@ -321,7 +321,7 @@ return $matches[0] ?? '2.0'; ### config_import_needed -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L115) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L118) :::info Autogenerated @@ -332,7 +332,7 @@ The value of this configuration is autogenerated on access. ### database_upgrade_needed -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L129) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L132) :::info Autogenerated @@ -343,7 +343,7 @@ The value of this configuration is autogenerated on access. ### enable_zerodowntime -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L144) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L147) Deploy without setting maintenance mode if possible @@ -352,8 +352,18 @@ true ``` +### use_redis_cache_id +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L150) + +deploy with auto updating cache index_prefix + +```php title="Default value" +false +``` + + ### artifact_file -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L330) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L336) The file the artifact is saved to @@ -363,7 +373,7 @@ The file the artifact is saved to ### artifact_dir -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L333) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L339) The directory the artifact is saved in @@ -373,7 +383,7 @@ The directory the artifact is saved in ### artifact_excludes_file -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L337) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L343) Points to a file with a list of files to exclude from packaging. The format is as with the `tar --exclude-from=[file]` option @@ -384,7 +394,7 @@ The format is as with the `tar --exclude-from=[file]` option ### build_from_repo -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L340) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L346) If set to true, the artifact is built from a clean copy of the project repository instead of the current working directory @@ -394,7 +404,7 @@ false ### repository -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L343) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L349) Overrides [repository](/docs/recipe/common.md#repository) from `recipe/common.php`. @@ -406,7 +416,7 @@ null ### artifact_path -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L346) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L352) The relative path to the artifact file. If the directory does not exist, it will be created @@ -419,7 +429,7 @@ return get('artifact_dir') . '/' . get('artifact_file'); ### bin/tar -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L354) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L360) The location of the tar command. On MacOS you should have installed gtar, as it supports the required settings :::info Autogenerated @@ -430,14 +440,14 @@ The value of this configuration is autogenerated on access. ### additional_shared_files -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L426) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L432) Array of shared files that will be added to the default shared_files without overriding ### additional_shared_dirs -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L428) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L434) Array of shared directories that will be added to the default shared_dirs without overriding @@ -447,7 +457,7 @@ Array of shared directories that will be added to the default shared_dirs withou ## Tasks ### magento:compile -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L154) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L160) Compiles magento di. @@ -459,7 +469,7 @@ e.g. ### magento:deploy:assets -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L180) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L186) Deploys assets. @@ -486,7 +496,7 @@ in `app/etc/config.php`, e.g.: ### magento:deploy:assets:adminhtml -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L196) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L202) Deploys assets for backend only. @@ -494,7 +504,7 @@ Deploys assets for backend only. ### magento:deploy:assets:frontend -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L201) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L207) Deploys assets for frontend only. @@ -502,7 +512,7 @@ Deploys assets for frontend only. ### magento:sync:content_version -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L249) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L255) Syncs content version. @@ -510,7 +520,7 @@ Syncs content version. ### magento:maintenance:enable -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L259) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L265) Enables maintenance mode. @@ -518,7 +528,7 @@ Enables maintenance mode. ### magento:maintenance:disable -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L265) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L271) Disables maintenance mode. @@ -526,7 +536,7 @@ Disables maintenance mode. ### magento:maintenance:enable-if-needed -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L271) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L277) Set maintenance mode if needed. @@ -534,7 +544,7 @@ Set maintenance mode if needed. ### magento:config:import -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L278) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L284) Config Import. @@ -542,7 +552,7 @@ Config Import. ### magento:upgrade:db -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L287) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L293) Upgrades magento database. @@ -550,7 +560,7 @@ Upgrades magento database. ### magento:cache:flush -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L296) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L302) Flushes Magento Cache. @@ -558,7 +568,7 @@ Flushes Magento Cache. ### deploy:magento -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L301) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L307) Magento2 deployment operations. @@ -575,7 +585,7 @@ This task is group task which contains next tasks: ### magento:build -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L311) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L317) Magento2 build operations. @@ -588,7 +598,7 @@ This task is group task which contains next tasks: ### deploy -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L317) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L323) Deploys your project. @@ -604,7 +614,7 @@ This task is group task which contains next tasks: ### artifact:package -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L365) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L371) Packages all relevant files in an artifact. @@ -612,7 +622,7 @@ Packages all relevant files in an artifact. ### artifact:upload -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L375) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L381) Uploads artifact in release folder for extraction. @@ -620,7 +630,7 @@ Uploads artifact in release folder for extraction. ### artifact:extract -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L380) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L386) Extracts artifact in release path. @@ -628,7 +638,7 @@ Extracts artifact in release path. ### build:remove-generated -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L386) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L392) Clears generated files prior to building. @@ -636,7 +646,7 @@ Clears generated files prior to building. ### build:prepare -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L391) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L397) Prepare local artifact build. @@ -644,7 +654,7 @@ Prepare local artifact build. ### artifact:build -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L416) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L422) Builds an artifact. @@ -661,15 +671,33 @@ This task is group task which contains next tasks: ### deploy:additional-shared -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L432) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L438) Adds additional files and dirs to the list of shared files and dirs. +### magento:set_cache_prefix +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L449) + +Update cache id_prefix. + +Update cache id_prefix on deploy so that you are compiling against a fresh cache +Reference Issue: https://github.com/davidalger/capistrano-magento2/issues/151 +use set('use_redis_cache_id') in your deployer script to enable + + +### magento:cleanup_cache_prefix +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L482) + +Cleanup cache id_prefix env files. + +After successful deployment, move the tmp_env.php file to env.php ready for next deployment + + ### artifact:prepare -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L439) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L496) Prepares an artifact on the target server. @@ -689,7 +717,7 @@ This task is group task which contains next tasks: ### artifact:finish -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L452) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L509) Executes the tasks after artifact is released. @@ -704,7 +732,7 @@ This task is group task which contains next tasks: ### artifact:deploy -[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L460) +[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L517) Actually releases the artifact deployment. diff --git a/recipe/magento2.php b/recipe/magento2.php index b1cf028f6..a5cdff40f 100644 --- a/recipe/magento2.php +++ b/recipe/magento2.php @@ -8,10 +8,13 @@ use Deployer\Exception\GracefulShutdownException; use Deployer\Exception\RunException; use Deployer\Host\Host; +use Symfony\Component\VarExporter\VarExporter; const CONFIG_IMPORT_NEEDED_EXIT_CODE = 2; const DB_UPDATE_NEEDED_EXIT_CODE = 2; const MAINTENANCE_MODE_ACTIVE_OUTPUT_MSG = 'maintenance mode is active'; +const ENV_CONFIG_FILE_PATH = 'app/etc/env.php'; +const TMP_ENV_CONFIG_FILE_PATH = 'app/etc/env_tmp.php'; add('recipes', ['magento2']); @@ -143,6 +146,9 @@ // Deploy without setting maintenance mode if possible set('enable_zerodowntime', true); +//deploy with auto updating cache index_prefix +set('use_redis_cache_id', false); + // Tasks // To work correctly with artifact deployment, it is necessary to set the MAGE_MODE correctly in `app/etc/config.php` @@ -434,6 +440,57 @@ function magentoDeployAssetsSplit(string $area) add('shared_dirs', get('additional_shared_dirs')); }); +/** + * Update cache id_prefix on deploy so that you are compiling against a fresh cache + * Reference Issue: https://github.com/davidalger/capistrano-magento2/issues/151 + * use set('use_redis_cache_id') in your deployer script to enable + **/ +desc('Update cache id_prefix'); +task('magento:set_cache_prefix', function () { + //download current env config + $tmpConfigFile = tempnam(sys_get_temp_dir(), 'deployer_config'); + download('{{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH, $tmpConfigFile); + $envConfigArray = include($tmpConfigFile); + //set prefix to `alias_releasename_` + $prefixUpdate = get('alias') . '_' . get('release_name') . '_'; + + //update id_prefix to include release name + $envConfigArray['cache']['frontend']['default']['id_prefix'] = $prefixUpdate; + $envConfigArray['cache']['frontend']['page_cache']['id_prefix'] = $prefixUpdate; + + //Generate configuration array as string + $envConfigStr = '