Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magento2 cache key id prefix #3388

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions docs/recipe/magento2.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,33 @@ Upgrades magento database.



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

Update cache id_prefix.

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

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#L260)

Magento2 deployment operations.

Expand All @@ -296,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#L207)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L268)

Magento2 build operations.

Expand All @@ -309,7 +326,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#L274)

Deploys your project.

Expand Down
61 changes: 61 additions & 0 deletions recipe/magento2.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,67 @@
}
});


/**
* 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 () {
$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 = "<?php return " . var_export($envConfig, true) . ";";
file_put_contents($tmpFilename, $envConfigStr);
//upload to server
upload($tmpFilename, "{{deploy_path}}/shared/$tmpConfigFilePath", ['progress_bar' => 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");
Expand Down