Skip to content

Commit

Permalink
DD#0000: feat: Updated magento2 config to generate a new cache id eac…
Browse files Browse the repository at this point in the history
…h deployment
  • Loading branch information
valguss committed Mar 6, 2023
1 parent b565fdc commit ce6a81a
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions recipe/magento2.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
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']);

Expand Down Expand Up @@ -367,6 +369,47 @@
add('shared_dirs', get('additional_shared_dirs'));
});

/**
* 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 () {
//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);
});
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 () {
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');


desc('Prepares an artifact on the target server');
task('artifact:prepare', [
Expand Down

0 comments on commit ce6a81a

Please sign in to comment.