Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Deploy Drupal updates and new features with Drush commands

Parent Feed: 
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.

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web