Automating Post-Installation Tasks with Composer in Drupal (Including .htaccess Rewrite, Shell Script & Environment Variables)
Introduction
Composer is essential for dependency management in Drupal, but some tasks such as setting up environment-specific configurations, running shell commands, or managing environment variables need to be automated. By using Composer’s post-install scripts, you can easily run custom commands after composer install. This includes tasks like modifying the .htaccess file or running custom shell scripts.
This guide will show you how to add these tasks into your Composer workflow for seamless deployment, including setting environment variables in settings.php and using them for .htaccess modifications.
1. Adding Post-Install Scripts for .htaccess Rewrite
In some environments, you may need to modify the .htaccess file automatically after running composer install, for example, to set the RewriteBase for Drupal. You can use environment variables set in settings.php to handle this configuration dynamically.
1.1 Example: Adding RewriteBase URL to .htaccess Based on Environment Variable
To modify the .htaccess file after installation, you can add a custom script to composer.json. Here’s an example that checks the environment variable set in settings.php and updates .htaccess accordingly.
Step 1: Set the Environment Variable in settings.php
In settings.php, you can define the environment variable based on the environment:
$env = getenv('DRUPAL_ENV') ?: 'development'; // Default to 'development' if not set
This will allow you to set the DRUPAL_ENV environment variable, which could be development, staging, or production, based on your environment.
Step 2: Add Post-Install Script to composer.json
Next, in your composer.json, you can use the DRUPAL_ENV variable to modify .htaccess automatically:
{
"scripts": {
"post-install-cmd": [
"@drupal-scaffold",
"cp web/sites/default/default.settings.php web/sites/default/settings.php",
"cp web/.htaccess web/.htaccess",
"bash ./scripts/update-htaccess.sh"
],
"post-update-cmd": [
"@drupal-scaffold"
]
}
}
Step 3: Write the Script to Update .htaccess Based on Environment
In the update-htaccess.sh shell script, you can add logic to update the .htaccess file based on the DRUPAL_ENV variable:
#!/bin/bash
# Get the environment variable
ENV=$(php -r "echo getenv('DRUPAL_ENV');")
if [ "$ENV" == "production" ]; then
Recommended by LinkedIn
sed -i 's|# RewriteBase /|RewriteBase /prod-path|' web/.htaccess
elif [ "$ENV" == "staging" ]; then
sed -i 's|# RewriteBase /|RewriteBase /staging-path|' web/.htaccess
else
sed -i 's|# RewriteBase /|RewriteBase /dev-path|' web/.htaccess
fi
Explanation of the Commands:
This will automatically adjust the .htaccess file’s base URL based on the environment whenever you run composer install.
2. Running Shell Scripts in Post-Install Script
In addition to modifying .htaccess files, you might need to execute shell scripts as part of your deployment process. For example, you could want to run a custom shell script to configure environment variables or clear cache after installation.
2.1 Example: Running Shell Script After Composer Install
{
"scripts": {
"post-install-cmd": [
"@drupal-scaffold",
"cp web/sites/default/default.settings.php web/sites/default/settings.php",
"cp web/.htaccess web/.htaccess",
"bash ./scripts/custom-script.sh"
],
"post-update-cmd": [
"@drupal-scaffold"
]
}
}
Explanation of the Commands:
This allows you to run any shell commands automatically after your composer install process finishes.
3. Conclusion
Using Composer’s post-install script functionality, you can automate crucial deployment tasks such as updating .htaccess configurations, managing environment variables in settings.php, and running shell scripts. This makes deployments more efficient and consistent, and ensures that your environment is configured correctly every time.
Start automating your Drupal Composer workflows today and simplify your deployment process with post-install scripts!