Skip to content

Commit

Permalink
Update Craft CMS deploy recipe (#3839)
Browse files Browse the repository at this point in the history
* let the Craft deploy fail when a command fails

* add craft cache, setup and garbage collection commands

* generate docs
  • Loading branch information
ThomasDeMarez authored May 22, 2024
1 parent db2deb4 commit 03ffe14
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 19 deletions.
12 changes: 10 additions & 2 deletions docs/recipe/craftcms.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,18 @@ Overrides [writable_dirs](/docs/recipe/deploy/writable.md#writable_dirs) from `r

## Tasks

### craft:gc
[Source](https://github.com/deployphp/deployer/blob/master/recipe/craftcms.php#L120)

Runs garbage collection.




### deploy
[Source](https://github.com/deployphp/deployer/blob/master/recipe/craftcms.php#L64)
[Source](https://github.com/deployphp/deployer/blob/master/recipe/craftcms.php#L127)

deploy.
Deploys Craft CMS.



Expand Down
98 changes: 81 additions & 17 deletions recipe/craftcms.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
'web/cpresources',
]);


/**
* Run a craft command.
*
* Supported options:
* - 'showOutput': Show the output of the command if given.
* - 'interactive': Don't append the --interactive=0 flag to the command.
*
* @param string $command The craft command (with cli options if any).
* @param array $options The options that define the behaviour of the command.
*
* @return callable A function that can be used as a task.
*/
function craft($command, $options = [])
Expand All @@ -39,34 +41,96 @@ function craft($command, $options = [])
throw new \Exception('Your .env file is empty! Cannot proceed.');
}

try {
$output = run("cd {{release_path}} && {{bin/php}} craft $command");
// By default we don't want any command to be interactive
if(! in_array('interactive', $options)) {
$command .= ' --interactive=0';
}

$output = run("{{bin/php}} {{release_path}}/craft $command");

if (in_array('showOutput', $options)) {
writeln("<info>$output</info>");
}
} catch (\Throwable $e) {
writeln("<error>{$e->getMessage()}</error>");
if (in_array('showOutput', $options)) {
writeln("<info>$output</info>");
}
};
}

desc('Execute craft migrate/all');
task('craft:migrate/all', craft('migrate/all --interactive=0'))->once();
/*
* Migrations
*/

desc('Runs all pending Craft, plugin, and content migrations');
task('craft:migrate/all', craft('migrate/all'));

desc('Upgrades Craft by applying new migrations');
task('craft:migrate/up', craft('migrate/up'));

/*
* Generate keys
*/

desc('Generates a new application ID and saves it in the `.env` file');
task('craft:setup/app-id', craft('setup/app-id'));

desc('Generates a new security key and saves it in the `.env` file');
task('craft:setup/security-key', craft('setup/security-key'));

/*
* Project config
*/

desc('Applies project config file changes.');
task('craft:project-config/apply', craft('project-config/apply'));

/*
* Caches
*/

desc('Execute craft project-config/apply');
task('craft:project-config/apply', craft('project-config/apply --interactive=0'))->once();
desc('Flushes all caches registered in the system');
task('craft:cache/flush-all', craft('cache/flush-all'));

desc('Execute craft clear-caches/all');
task('craft:clear-caches/all', craft('clear-caches/all --interactive=0'))->once();
desc('Clear all caches');
task('craft:clear-caches/all', craft('clear-caches/all'));

desc('deploy');
desc('Clear all Asset caches');
task('craft:clear-caches/asset', craft('clear-caches/asset'));

desc('Clear all Asset indexing data');
task('craft:clear-caches/asset-indexing-data', craft('clear-caches/asset-indexing-data'));

desc('Clear all compiled classes');
task('craft:clear-caches/compiled-classes', craft('clear-caches/compiled-classes'));

desc('Clear all compiled templates');
task('craft:clear-caches/compiled-templates', craft('clear-caches/compiled-templates'));

desc('Clear all control panel resources');
task('craft:clear-caches/cp-resources', craft('clear-caches/cp-resources'));

desc('Clear all data caches');
task('craft:clear-caches/data', craft('clear-caches/data'));

desc('Clear all temp files');
task('craft:clear-caches/temp-files', craft('clear-caches/temp-files'));

/*
* Garbage collection
*/

desc('Runs garbage collection');
task('craft:gc', craft('gc --delete-all-trashed=1 --silent-exit-on-exception=1', ['showOutput']));

/*
* Main deploy
*/

desc('Deploys Craft CMS');
task('deploy', [
'deploy:prepare',
'deploy:vendors',
'craft:clear-caches/all',
'craft:clear-caches/compiled-classes',
'craft:migrate/all',
'craft:project-config/apply',
'craft:gc',
'craft:clear-caches/all',
'deploy:publish',
]);

0 comments on commit 03ffe14

Please sign in to comment.