Automate and Simplify Your Drupal Workflow with Bash Scripts for Shared Hosting
July 19, 2024Drupal Odyssey is supported by it's readers. When you purchase products or services using the links on this site, we may earn a small commission at no additional cost to you. Learn more
For the past several years, I've been working with enterprise level marketing Drupal websites. Those sites have several developers and a variety of version control systems and hosting providers. All of these sites have some sort of continuous integration and delivery systems built in as part of the deployment processes. This is great if you have a complex web application that consists of code from different repositories and a DevOps team that knows how these systems work and are set up.
While a more simple website with basic features, such as Drupal Odyssey, the benefits do not out weigh the complexity to use these systems and cost. Especially if you are not familiar with setting them up. I've tried to use a few CI/CD systems and they do work, but for one reason or another, I always come back to what I see is the most simple way to just get the job done ... a shell script.
If you don't have a lot of fancy-smancy integrations or complicated custom code that needs testing every time you make an update, such as a Drupal core update, CI/CD systems can be overkill in my opinion. In reality, Drupal website release processes overview would look like the following:
- Merge the dev branch into the master/main branch
- Pull/checkout the repository changes
- Run
composer install
- Run database updates
- Import configuration changes
- Clear caches
That's it! No build scripts. No unit or functional tests. Below is the script I cobbled together to just get the job done. To be able to use this script in your own deployments, you will first need to ensure you have password less ssh login set up to your server from your local machine.
#!/bin/bash
# Hostname or IP address of your server.
hostName='YOUR_HOSTING_IP_ADDRESS'
# Username with ssh access on the above host
remoteUser='YOUR_HOSTING_USERNAME'
# The ssh port to use.
port=YOUR_HOSTING_SSH_PORT
rootPath="/home/$remoteUser"
# Composer 2 path since hosting default is Composer 1.x
composerPath="$rootPath/bin/composer"
# Where Drupal is installed.
drupalRoot="$rootPath/domains/drupalodyssey.com/public_html"
# Where the Drush command is located.
drushPath="$drupalRoot/vendor/bin/drush"
echo "Connecting to host $hostName ..."
ssh -t -p $port -i $HOME/.ssh/id_rsa $remoteUser@$hostName << EOF
cd $drupalRoot;
echo "Putting website into maintenance mode ..."
$drushPath config:set system.maintenance message "Our servers are getting a quick tune-up. Your patience is appreciated!" -y
$drushPath state:set system.maintenance_mode 1 --input-format=integer
echo "Clearing Drupal caches ..."
$drushPath cr
echo "Pulling latest changes from the repository ..."
git pull
echo "Running composer install ..."
$composerPath install
echo "Clearing Drupal caches ..."
$drushPath cr
echo "Importing configuration changes ..."
$drushPath cim -y
echo "Updating database schema ..."
$drushPath updb -y
echo "Removing website from maintenance mode ..."
$drushPath state:set system.maintenance_mode 0 --input-format=integer
echo "Clearing Drupal caches ..."
$drushPath cr
EOF
echo "---"
echo "---"
echo "Deployment complete."
NOTICE: The above script is provided as-is with no warranty. I am not responsible for system damage or data loss. Use the above script at your own risk.
The bash script is pretty straight forward. The first two blocks simply define some variables to be used later. The third block is where all of the magic happens.
It starts by opening a ssh session to the hosting provider using public/private keys. Once that ssh session is open, several Drush commands are run to put the site into maintenance mode and clear the caches, followed by pulling the changes from the repository. Once that has completed, the composer install runs to ensure all of the dependencies are present. This is followed up by importing the configuration changes and running the database updates before finally disabling maintenance mode and clearing the caches to bring the site back online.
As you can see, this process is pretty simple with no bells and whistles. This script could be modified to be as complex as it needs to be. However, if there are more complex processes in your deployment process and you still don't want to use a CI/CD system, I'd at least try and set it up using Ansible. I was going to use Ansible myself, by my shared hosting plan did not have Python installed.
Drop a comment below and let me know how you handle simple deployments or if you have questions or comments about how I do it.
0 Comments
Login or Register to post comments.