Skip to content

Commit 1e99850

Browse files
Ensure bin/magento commands run on last secussfull release on failed deployment.
1 parent bdb600b commit 1e99850

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

recipe/magento2.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
'{{magento_dir}}/var/view_preprocessed/*',
107107
]);
108108

109+
// WARNING: Do not use {{bin/magento}} in deploy:failed handlers - release_or_current_path points to the failed
110+
// release during failure, not the live one. Use {{current_path}} explicitly instead.
111+
// See: config_import_needed_on_current, magento:config:import:on-current, magento:maintenance:enable/disable.
109112
set('bin/magento', '{{release_or_current_path}}/{{magento_dir}}/bin/magento');
110113

111114
set('magento_version', function () {
@@ -129,6 +132,21 @@
129132
return false;
130133
});
131134

135+
set('config_import_needed_on_current', function () {
136+
// detect if app:config:import is needed on the current (live) release
137+
// do not use {{bin/magento}} as it resolves via release_or_current_path which is unreliable in failure scenarios
138+
try {
139+
run('{{bin/php}} {{current_path}}/{{magento_dir}}/bin/magento app:config:status');
140+
} catch (RunException $e) {
141+
if ($e->getExitCode() == CONFIG_IMPORT_NEEDED_EXIT_CODE) {
142+
return true;
143+
}
144+
145+
throw $e;
146+
}
147+
return false;
148+
});
149+
132150
set('database_upgrade_needed', function () {
133151
// detect if db upgrade is needed
134152
try {
@@ -318,6 +336,16 @@ function magentoDeployAssetsSplit(string $area)
318336
}
319337
});
320338

339+
desc('Config Import on current release');
340+
task('magento:config:import:on-current', function () {
341+
if (get('config_import_needed_on_current')) {
342+
// do not use {{bin/magento}} as it must run on the current (last successful) release in failure scenarios
343+
run('{{bin/php}} {{current_path}}/{{magento_dir}}/bin/magento app:config:import --no-interaction');
344+
} else {
345+
writeln('App config is up to date => import skipped');
346+
}
347+
});
348+
321349
desc('Upgrades magento database');
322350
task('magento:upgrade:db', function () {
323351
if (get('database_upgrade_needed')) {
@@ -374,7 +402,7 @@ function magentoDeployAssetsSplit(string $area)
374402

375403
desc('Run magento post deployment failure tasks.');
376404
task('deploy:magento:failed', [
377-
'magento:config:import',
405+
'magento:config:import:on-current',
378406
'magento:maintenance:disable',
379407
]);
380408

0 commit comments

Comments
 (0)