Skip to content

Commit

Permalink
DD#0000: feat: Magento2: Add support for preload keys (#3567)
Browse files Browse the repository at this point in the history
* DD#0000: feat: Magento2: Add support for preload keys

- Also updated to remove the config set and instead manually add the hocks to your deployer file
- And move var-exporter into the main composer require
- #3453 (comment)

* DD#0000: feat: Reverted composer updates

* DD#0000: feat: Updated docs

* DD#0000: feat: Updated replace to use preg to make sure it only checks beginning of string
Updated readme use better grammer

* DD#0000: feat: Updated md docs
  • Loading branch information
valguss authored Jul 4, 2023
1 parent 80dcf8d commit 48193c6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 58 deletions.
86 changes: 40 additions & 46 deletions docs/recipe/magento2.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,8 @@ 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#L336)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L333)

The file the artifact is saved to

Expand All @@ -373,7 +363,7 @@ The file the artifact is saved to


### artifact_dir
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L339)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L336)

The directory the artifact is saved in

Expand All @@ -383,7 +373,7 @@ The directory the artifact is saved in


### artifact_excludes_file
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L343)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L340)

Points to a file with a list of files to exclude from packaging.
The format is as with the `tar --exclude-from=[file]` option
Expand All @@ -394,7 +384,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#L346)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L343)

If set to true, the artifact is built from a clean copy of the project repository instead of the current working directory

Expand All @@ -404,7 +394,7 @@ false


### repository
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L349)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L346)

Overrides [repository](/docs/recipe/common.md#repository) from `recipe/common.php`.

Expand All @@ -416,7 +406,7 @@ null


### artifact_path
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L352)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L349)

The relative path to the artifact file. If the directory does not exist, it will be created

Expand All @@ -429,7 +419,7 @@ return get('artifact_dir') . '/' . get('artifact_file');


### bin/tar
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L360)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L357)

The location of the tar command. On MacOS you should have installed gtar, as it supports the required settings
:::info Autogenerated
Expand All @@ -440,14 +430,14 @@ The value of this configuration is autogenerated on access.


### additional_shared_files
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L432)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L429)

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#L434)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L431)

Array of shared directories that will be added to the default shared_dirs without overriding

Expand All @@ -457,7 +447,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#L160)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L157)

Compiles magento di.

Expand All @@ -469,7 +459,7 @@ e.g.


### magento:deploy:assets
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L186)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L183)

Deploys assets.

Expand All @@ -496,79 +486,79 @@ in `app/etc/config.php`, e.g.:


### magento:deploy:assets:adminhtml
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L202)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L199)

Deploys assets for backend only.




### magento:deploy:assets:frontend
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L207)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L204)

Deploys assets for frontend only.




### magento:sync:content_version
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L255)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L252)

Syncs content version.




### magento:maintenance:enable
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L265)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L262)

Enables maintenance mode.




### magento:maintenance:disable
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L271)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L268)

Disables maintenance mode.




### magento:maintenance:enable-if-needed
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L277)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L274)

Set maintenance mode if needed.




### magento:config:import
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L284)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L281)

Config Import.




### magento:upgrade:db
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L293)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L290)

Upgrades magento database.




### magento:cache:flush
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L302)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L299)

Flushes Magento Cache.




### deploy:magento
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L307)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L304)

Magento2 deployment operations.

Expand All @@ -585,7 +575,7 @@ This task is group task which contains next tasks:


### magento:build
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L317)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L314)

Magento2 build operations.

Expand All @@ -598,7 +588,7 @@ This task is group task which contains next tasks:


### deploy
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L323)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L320)

Deploys your project.

Expand All @@ -614,47 +604,47 @@ This task is group task which contains next tasks:


### artifact:package
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L371)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L368)

Packages all relevant files in an artifact.




### artifact:upload
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L381)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L378)

Uploads artifact in release folder for extraction.




### artifact:extract
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L386)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L383)

Extracts artifact in release path.




### build:remove-generated
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L392)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L389)

Clears generated files prior to building.




### build:prepare
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L397)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L394)

Prepare local artifact build.




### artifact:build
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L422)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L419)

Builds an artifact.

Expand All @@ -671,33 +661,37 @@ This task is group task which contains next tasks:


### deploy:additional-shared
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L438)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L435)

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)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L450)

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
To use this feature, add the following to your deployer scripts:
```php
after('deploy:shared', 'magento:set_cache_prefix');
after('deploy:magento', 'magento:cleanup_cache_prefix');
```


### magento:cleanup_cache_prefix
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L482)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L490)

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#L496)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L500)

Prepares an artifact on the target server.

Expand All @@ -717,7 +711,7 @@ This task is group task which contains next tasks:


### artifact:finish
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L509)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L513)

Executes the tasks after artifact is released.

Expand All @@ -732,7 +726,7 @@ This task is group task which contains next tasks:


### artifact:deploy
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L518)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L522)

Actually releases the artifact deployment.

Expand Down
28 changes: 16 additions & 12 deletions recipe/magento2.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@
// 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`
Expand Down Expand Up @@ -443,7 +440,11 @@ function magentoDeployAssetsSplit(string $area)
/**
* 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
* To use this feature, add the following to your deployer scripts:
* ```php
* after('deploy:shared', 'magento:set_cache_prefix');
* after('deploy:magento', 'magento:cleanup_cache_prefix');
* ```
**/
desc('Update cache id_prefix');
task('magento:set_cache_prefix', function () {
Expand All @@ -454,6 +455,17 @@ function magentoDeployAssetsSplit(string $area)
//set prefix to `alias_releasename_`
$prefixUpdate = get('alias') . '_' . get('release_name') . '_';

//check for preload keys and update
if (isset($envConfigArray['cache']['frontend']['default']['backend_options']['preload_keys'])) {
$oldPrefix = $envConfigArray['cache']['frontend']['default']['id_prefix'];
$preloadKeys = $envConfigArray['cache']['frontend']['default']['backend_options']['preload_keys'];
$newPreloadKeys = [];
foreach ($preloadKeys as $preloadKey) {
$newPreloadKeys[] = preg_replace('/^' . $oldPrefix . '/', $prefixUpdate, $preloadKey);
}
$envConfigArray['cache']['frontend']['default']['backend_options']['preload_keys'] = $newPreloadKeys;
}

//update id_prefix to include release name
$envConfigArray['cache']['frontend']['default']['id_prefix'] = $prefixUpdate;
$envConfigArray['cache']['frontend']['page_cache']['id_prefix'] = $prefixUpdate;
Expand All @@ -470,10 +482,6 @@ function magentoDeployAssetsSplit(string $area)
//link the env to the tmp version
run('{{bin/symlink}} {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH . ' {{release_path}}/' . ENV_CONFIG_FILE_PATH);
});
//get current env config
if (get('use_redis_cache_id')) {
after('deploy:shared', 'magento:set_cache_prefix');
}

/**
* After successful deployment, move the tmp_env.php file to env.php ready for next deployment
Expand All @@ -486,10 +494,6 @@ function magentoDeployAssetsSplit(string $area)
// Symlink shared dir to release dir
run('{{bin/symlink}} {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH . ' {{release_path}}/' . ENV_CONFIG_FILE_PATH);
});
//get current env config
if (get('use_redis_cache_id')) {
after('deploy:magento', 'magento:cleanup_cache_prefix');
}


desc('Prepares an artifact on the target server');
Expand Down

0 comments on commit 48193c6

Please sign in to comment.