Skip to content

Commit

Permalink
DD#0000: feat: Added an option to enable feature
Browse files Browse the repository at this point in the history
  • Loading branch information
valguss committed Mar 29, 2023
1 parent 82d2b1e commit b09740d
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions recipe/magento2.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,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`
Expand Down Expand Up @@ -439,27 +442,30 @@ 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
**/
desc('Update cache id_prefix');
task('magento:set_cache_prefix', function () {
//get current env config
$envConfigString = run('cat {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH);
$envConfig = eval('?>'.$envConfigString);
//set prefix to `alias_releasename_`
$prefixUpdate = get('alias') . '_' . get('release_name') . '_';

//update id_prefix to include release name
$envConfig['cache']['frontend']['default']['id_prefix'] = $prefixUpdate;
$envConfig['cache']['frontend']['page_cache']['id_prefix'] = $prefixUpdate;
//Generate configuration array as string
$envConfigStr = '<?php return ' . var_export($envConfig, true) . ';';
// Touch tmp config
run('[ -f {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH. ' ] || touch {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH);
run('echo $"' . $envConfigStr . '" > {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH);
//delete the symlink for env.php
run('rm {{release_or_current_path}}/' . ENV_CONFIG_FILE_PATH);
//link the env to the tmp version
run('{{bin/symlink}} {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH . ' {{release_path}}/' . ENV_CONFIG_FILE_PATH);
if (get('use_redis_cache_id')) {
$envConfigString = run('cat {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH);
$envConfig = eval('?>' . $envConfigString);
//set prefix to `alias_releasename_`
$prefixUpdate = get('alias') . '_' . get('release_name') . '_';

//update id_prefix to include release name
$envConfig['cache']['frontend']['default']['id_prefix'] = $prefixUpdate;
$envConfig['cache']['frontend']['page_cache']['id_prefix'] = $prefixUpdate;
//Generate configuration array as string
$envConfigStr = '<?php return ' . var_export($envConfig, true) . ';';
// Touch tmp config
run('[ -f {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH . ' ] || touch {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH);
run('echo $"' . $envConfigStr . '" > {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH);
//delete the symlink for env.php
run('rm {{release_or_current_path}}/' . ENV_CONFIG_FILE_PATH);
//link the env to the tmp version
run('{{bin/symlink}} {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH . ' {{release_path}}/' . ENV_CONFIG_FILE_PATH);
}
});
after('deploy:shared', 'magento:set_cache_prefix');

Expand All @@ -468,11 +474,13 @@ function magentoDeployAssetsSplit(string $area)
*/
desc('Cleanup cache id_prefix env files');
task('magento:cleanup_cache_prefix', function () {
run('rm {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH);
run('rm {{release_or_current_path}}/' . ENV_CONFIG_FILE_PATH);
run('mv {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH . ' {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH);
// Symlink shared dir to release dir
run('{{bin/symlink}} {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH . ' {{release_path}}/' . ENV_CONFIG_FILE_PATH);
if (get('use_redis_cache_id')) {
run('rm {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH);
run('rm {{release_or_current_path}}/' . ENV_CONFIG_FILE_PATH);
run('mv {{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH . ' {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH);
// Symlink shared dir to release dir
run('{{bin/symlink}} {{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH . ' {{release_path}}/' . ENV_CONFIG_FILE_PATH);
}
});
after('deploy:magento', 'magento:cleanup_cache_prefix');

Expand Down

0 comments on commit b09740d

Please sign in to comment.