Deploying Drupal sites can be tricky and involve many steps that can go wrong. While you could manually run updates and clear caches, there's a better way: Drush. This command-line tool turns complex deployment tasks into simple commands, and when combined with the right automation, it can make your deployments reliable and repeatable.
Let's talk about how you can use Drush for shell deployments with Drupal and finally automate your deployments with DeployHQ!
Using Drush for Deployments
When you're ready to update your Drupal site, you'll want to use the drush deploy
command. This command takes care of the usual stuff you would to manually:
Update the Database: The first thing the deploy command does is run
drush updatedb
. This command updates your site's database to match the latest version of your code. It's important because it makes sure all the new features and changes work correctly.Import Configuration: Next, it runs
drush config:import
. This imports any new or changed configuration from your codebase into your site. This step is crucial for keeping your site's settings up to date.Rebuild the Cache: After that, it runs
drush cache:rebuild
. This clears and rebuilds the cache, which helps your site run smoothly with the new updates.Run Deploy Hooks: Finally, it runs
drush deploy:hook
. This step allows you to run custom code after the deployment is complete. It's useful for any final tweaks or checks you want to do.
You can find the full documentation for the drush deploy
command here.
Customizing the Deploy Command
If you need to change how the deploy command works, you can do that by adjusting the settings for its subcommands. For example, you can customize how updatedb
or config:import
behave by changing their Drush configurations.
Example of a Deployment Script
Now when you're ready to deploy your Drupal site on your server, all you need to do is pull the latest code and run the deploy command:
# Update the codebase
git pull origin main
# Update dependencies
composer install --no-progress
# Run the deploy command
drush deploy
Of course you could run this manually on every deployment, but you probably have better things to do! Let's take a look at how you can automate this with DeployHQ.
This guide assumes you already have SSH access to a server where Drupal is installed. If you need help setting up Drupal, check out the installation guide on the Drupal website.
Setting Up Your Project in DeployHQ
First, you'll need to sign up and log into DeployHQ. Once you're in, head over to the Projects section and click on New Project. You'll be guided through a setup wizard where you'll need to choose your repository. If you run into any issues connecting your repository, DeployHQ has a support page that can help you out.
Adding Your Server to DeployHQ
Next, you'll need to tell DeployHQ where to send your Drupal files. Go to Servers and click New Server. Give your server a name you'll remember, and choose SSH as the protocol. Enter your server's IP address in the Hostname field, and use deployhq
as the Username.
To set up SSH key authentication, you'll need to run a few commands on your server. Start by switching to the deployhq
user with su - deployhq
, then create and secure the SSH directory with mkdir ~/.ssh
and chmod 700 ~/.ssh
. Open the authorized_keys
file with nano ~/.ssh/authorized_keys
, paste in DeployHQ's public key, and save the file by pressing Ctrl + X
, then y
, and Enter
. Finally, secure the authorized_keys
file with chmod 600 ~/.ssh/authorized_keys
.
Back in DeployHQ, set the Deployment Path to /var/www/example.com
and turn on the Perform zero-downtime deployments option. This ensures your site stays up while DeployHQ works on the new version. Remember, this means your files will actually go into a path like /var/www/example.com/current
, so you'll need to adjust your web server settings accordingly.
In the Environment field, enter production
and make sure the automatic deployment option is enabled. Once everything looks good, hit Save. If you run into any trouble connecting to your server, the DeployHQ support page has more information to help you out.
Setting Up the Build Pipeline
DeployHQ's build pipeline lets you run commands during deployment, like installing dependencies or compiling assets. To set this up, go to Build Pipeline and click New command. Choose Composer as the template, which will automatically set the command to composer install --no-progress
. If you need to run this from a subdirectory, just add a line above to change directories, like cd path/to/subdirectory
.
Make sure to keep the option to stop the deployment if the command fails, then click Create Build Command. Since we're using Composer, you can also set up caching to speed things up. Click Build Configuration, go to Cached Files, and add vendor/**
as your first cached file. This keeps a copy of your vendor
directory between deployments, so you don't have to download everything from scratch each time.
Setting Up the Deploy Command
DeployHQ has another feature called SSH commands. You can configure commands to run on your server using SSH here, such as to start or stop running services. In our case, we will want to run the drush deploy
command after our code has been updated. For that, simply add the following command to your project:
Deploying Your Drupal Site
With everything set up, you're ready to deploy. Click Deploy Project in the header, make sure your server is selected, and check the start and end revisions. When you're ready, scroll down and click Deploy. Sit back and watch as DeployHQ takes care of the rest. Once it's done, you'll see a completion screen, and you can feel good knowing you've just made your deployment process a lot easier :)
If you've got other projects you want to automate, DeployHQ has plenty of guides for different frameworks and systems to help you out.
FAQ
You got questions? We got answers!
Can I run Drush commands without SSH access?
No, Drush requires direct server access via SSH or similar command-line interface.
Will my site go down during database updates?
No, DeployHQ's zero-downtime deployment ensures your site stays up while updates are being applied.
Can I roll back a failed deployment?
Yes, DeployHQ keeps previous versions and allows you to quickly roll back to any previous deployment.
How much does DeployHQ cost?
You can get started for free! All DeployHQ plans cover the basics of deploying Drupal. Here's a quick overview:
Looking for more? Our Business ($39/mo) and Enterprise ($99/mo) plans include additional features like custom domains, priority deployments, and the ability to deploy behind firewalls.