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

Improve magento:set_cache_prefix & magento:cleanup_cache_prefix Tasks #3865

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 11 additions & 8 deletions docs/recipe/magento2.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,11 +669,14 @@ 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#L462)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L465)

Update cache id_prefix.

Update cache id_prefix on deploy so that you are compiling against a fresh cache
Update cache id_prefix on deploy to ensure compilation against a fresh cache
The cache ID prefix is generated using the magento_cache_id_prefix variable if it is set.
If magento_cache_id_prefix is not set, it falls back to using the alias.
This ensures that the cache prefix is unique for each deployment, preventing conflicts and ensuring that the latest version of the cache is used.
Reference Issue: https://github.com/davidalger/capistrano-magento2/issues/151
To use this feature, add the following to your deployer scripts:
```php
Expand All @@ -683,15 +686,15 @@ after('deploy:magento', 'magento:cleanup_cache_prefix');


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

Cleanup cache id_prefix env files.

After successful deployment, move the tmp_env.php file to env.php ready for next deployment


### magento:cron:stop
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L518)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L526)

Remove cron from crontab and kill running cron jobs.

Expand All @@ -703,7 +706,7 @@ To use this feature, add the following to your deployer scripts:


### magento:cron:install
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L534)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L542)

Install cron in crontab.

Expand All @@ -715,7 +718,7 @@ To use this feature, add the following to your deployer scripts:


### artifact:prepare
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L540)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/magento2.php#L548)

Prepares an artifact on the target server.

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


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

Executes the tasks after artifact is released.

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


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

Actually releases the artifact deployment.

Expand Down
46 changes: 27 additions & 19 deletions recipe/magento2.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,10 @@ function magentoDeployAssetsSplit(string $area)
});

/**
* Update cache id_prefix on deploy so that you are compiling against a fresh cache
* Update cache id_prefix on deploy to ensure compilation against a fresh cache
* The cache ID prefix is generated using the magento_cache_id_prefix variable if it is set.
* If magento_cache_id_prefix is not set, it falls back to using the alias.
* This ensures that the cache prefix is unique for each deployment, preventing conflicts and ensuring that the latest version of the cache is used.
* Reference Issue: https://github.com/davidalger/capistrano-magento2/issues/151
* To use this feature, add the following to your deployer scripts:
* ```php
Expand All @@ -460,14 +463,14 @@ function magentoDeployAssetsSplit(string $area)
**/
desc('Update cache id_prefix');
task('magento:set_cache_prefix', function () {
//download current env config
// Download current env config
$tmpConfigFile = tempnam(sys_get_temp_dir(), 'deployer_config');
download('{{deploy_path}}/shared/' . ENV_CONFIG_FILE_PATH, $tmpConfigFile);
download('{{deploy_path}}/shared/{{magento_dir}}/' . ENV_CONFIG_FILE_PATH, $tmpConfigFile);
$envConfigArray = include($tmpConfigFile);
//set prefix to `alias_releasename_`
$prefixUpdate = get('alias') . '_' . get('release_name') . '_';

//check for preload keys and update
$prefixUpdate = (get('magento_cache_id_prefix') ?? 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'];
Expand All @@ -478,33 +481,38 @@ function magentoDeployAssetsSplit(string $area)
$envConfigArray['cache']['frontend']['default']['backend_options']['preload_keys'] = $newPreloadKeys;
}

//update id_prefix to include 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
// Generate configuration array as string
$envConfigStr = '<?php return ' . var_export($envConfigArray, true) . ';';
file_put_contents($tmpConfigFile, $envConfigStr);
//upload updated config to server
upload($tmpConfigFile, '{{deploy_path}}/shared/' . TMP_ENV_CONFIG_FILE_PATH);
//cleanup tmp file

// Upload updated config to server
upload($tmpConfigFile, '{{deploy_path}}/shared/{{magento_dir}}/' . TMP_ENV_CONFIG_FILE_PATH);

// Cleanup tmp file
unlink($tmpConfigFile);
//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);

// Delete the symlink for env.php
run('rm {{release_or_current_path}}/{{magento_dir}}/' . ENV_CONFIG_FILE_PATH);

// Link the env to the tmp version
run('{{bin/symlink}} {{deploy_path}}/shared/{{magento_dir}}/' . TMP_ENV_CONFIG_FILE_PATH . ' {{release_path}}/{{magento_dir}}/' . ENV_CONFIG_FILE_PATH);

});

/**
* 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);
run('rm {{deploy_path}}/shared/{{magento_dir}}/' . ENV_CONFIG_FILE_PATH);
run('rm {{release_or_current_path}}/{{magento_dir}}/' . ENV_CONFIG_FILE_PATH);
run('mv {{deploy_path}}/shared/{{magento_dir}}/' . TMP_ENV_CONFIG_FILE_PATH . ' {{deploy_path}}/shared/{{magento_dir}}/' . 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);
run('{{bin/symlink}} {{deploy_path}}/shared/{{magento_dir}}/' . ENV_CONFIG_FILE_PATH . ' {{release_path}}/{{magento_dir}}/' . ENV_CONFIG_FILE_PATH);
});

/**
Expand Down
Loading