Picture
Minnur Yunusov Senior Drupal Developer Follow
October 11, 2016

How to use Drush Commands
In this blog post I will provide an example of a method I use to deploy changes on the projects that I work on my own time. This method is very useful when there is a need to make a lot of manual changes to the site when there is a new release. You could use this method to automate deployment process of your Drupal sites. In fact this method can be used for any other non-Drupal sites too.

For this blog post I am using Drush commands.

Lets start form a very simple example. I will use Acquia's directory structure to descibe where I am storing release scripts.

acquia-utils/
docroot/
hooks/
...
releases/
  - code/
    - release-2/
      - permissions.php
      - users.php
  - release-1.sh
  - release-2.sh
  - release-3.sh

As you can see releases/ directory is on the same level a docroot/. That's where the scripts would locate.

release-1.sh - A very simple release script that runs database updates and reverts features.

#!/bin/bash

# Database updates.
drush updb -y

# Revert features.
drush fr my_feature1 my_feature2 my_feature3 -y

release-2.sh - Release script with examples of how to run SQL queries and run PHP code.

#!/bin/bash

# Change weight of the module.
drush sql-query 'UPDATE system SET weight = 100 WHERE name = "my_custom_module"' -y

# Execute PHP code

# Make changes to permissions
drush php-script 'permissions.php' --script-path="../releases/code/release-2/" --user=[Drupal Admin Username]

# Make changes to users.
drush php-script 'users.php' --script-path="../releases/code/release-2/" --user=[Drupal Admin Username]

release-3.sh - Release script with additional examples.

#!/bin/bash

# Set variables
drush vset cache_lifetime 86400 -y
drush vset page_cache_maximum_age 86400 -y
drush vset error_level 0 -y
drush vset --always-set cache 1

# Execute inline PHP code

# Set permissions
drush php-eval "user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('my_module_name' => 'permission name'));"

# Set variable
drush php-eval "variable_set('my_variable_name', array('key' => 'value'));" -y

# Clear Drupal cache
drush cc all

And finally, you would need to ssh to the server and run each release script.

What else you could do:

  • You could add backup logic so every time you run the script you would create database and files backups.
  • Make clean Drupal install locally, download database from production and restore it locally with Drush.
  • Run content migrations.

Use your imagination and please share with us the methods you use to improve your deployment process.