Post-install scripts
During the deployment process of a project, we execute a series of automated tasks through Composer's post-install-cmd
script. This script is designed to ensure that the necessary configurations are in place and that the site is optimized and ready for production. You can find these scripts at the end of the composer.json
file.
1. htaccess File Setup
This line checks for the existence of an environment-specific .htaccess
file in the web
directory. If it exists, it copies this file to web/.htaccess
. This allows for different .htaccess
configurations to be applied automatically based on the deployment environment (development, staging, production, etc.).
@php -r "define('HTSRC', 'web/.htaccess_' . getenv('ENVIRONMENT')); echo (file_exists(HTSRC) && copy(HTSRC, 'web/.htaccess')) ? HTSRC.' copied to web/.htaccess'.PHP_EOL : '';"
2. Clear Compiled Templates Cache
After ensuring that the Craft CMS installation is valid with craft install/check
, this command clears the compiled templates cache. The --interactive=0
option makes the process non-interactive, which is essential for automated scripts.
@php craft install/check && php craft clear-caches/compiled-templates --interactive=0 || exit 0
Database Updates:
This command also begins with a Craft CMS installation check, followed by the craft up/index
command which applies any pending database migrations. This ensures that the database schema is up to date. Similar to the previous command, the non-interactive flag is set to 0
to allow the script to run without manual intervention.
@php craft install/check && php craft up/index --interactive=0 || exit 0
These scripts are part of the automation that facilitates a smooth transition from development to production, and it's vital for maintaining consistency across different environments. Each step is crucial for ensuring that the correct settings are applied and that the site functions as intended after each deployment.