Pantheon Community

Ensure Remote Drush commands use the correct URI

This tip shows how to execute remote Drush commands with the correct HTTPS url provided by default for each environment leveraging drushrc.php.

Terminus is a very helpful tool that offers ways to execute site maintenance tasks on the command line, using Drush.

terminus remote:drush <site>.<env> -- <command> [options]

Or shorthand:

terminus drush <site>.<env> -- <command> [options]

Running remote tasks in Pantheon Multidev, Dev, Test and Live environments sometimes requires the correct domain name be provided to the Drush/Drupal PHP runtime. On Pantheon, the environment URI provided to the Drupal PHP runtime via Drush defaults to the Non-SSL domain: http://<env>-<site>.pantheonsite.io. But in the days of HTTPS Everywhere, this can have unintended consequences.

1. Manually provision drush with the --uri option

On can manually override the --uri option on the command line:

$ terminus drush <site>.<env> -- <command> --uri=https://<env>-<site>.pantheonsite.io
$ terminus drush <site>.<env> -- <command> --uri=https://dev.vanitydomain.com
$ terminus drush <site>.<env> -- <command> --uri=https://test.vanitydomain.com
$ terminus drush <site>.<env> -- <command> --uri=https://www.vanitydomain.com

However, using --uri for all remote drush commands makes documenting and/or running scripts in different environments cumbersome and error prone.

2. Automatically provision the uri option per environment

Drush has a way to store default options like the --uri option in a drushrc.php file, and if you commit the drushrc.php file in the correct location, then remote drush commands via terminus will take the file into account.

To ensure the URI is correct for all environments add the following snippet of code to the drushrc.php file:

<?php

/**
 * Explicitly provision an SSL endpoint for Drush's --uri option in
 * Pantheon environments.
 */
if (isset($_ENV['PANTHEON_ENVIRONMENT'])) {
  switch ($_ENV['PANTHEON_ENVIRONMENT']) {
    case 'live':
      $uri = 'https://www.example.com';
      break;

    // Uncomment to use custom vanity domain on Test environment.
    # case 'test':
    #   $uri = 'https://test.vanitydomain.com';
    #   break; 

     // Uncomment to use custom vanity domain on Dev environment.
    # case 'dev':
    #   $uri = 'https://dev.vanitydomain.com';
    #   break;

    // Lando environment
    case 'lando':
      $uri = '';  // @todo
      break;

    // Multidev environments
    default:
      $uri = "https://{$_ENV['PANTHEON_ENVIRONMENT']}-{$_ENV['PANTHEON_SITE_NAME']}.pantheonsite.io";
      break;
  }
  $options['uri'] = $uri;
}

Where to commit the drushrc.php file?

For Drupal 7 on Pantheon, the file can exist in any of the following locations:

/sites/all/drush/drushrc.php (recommended)
/sites/default/drushrc.php
/drush/drushrc.php

We typically avoid committing the /sites/default/drushrc.php location, so each developer can use this file for their own needs on their local environment.

For Drupal 8 projects on Pantheon, where your document root is typically not the repository root, the file can exist in any of the following locations:

/drush/drushrc.php  (recommended)
/__DOCROOT__/drush/drushrc.php
/__DOCROOT__/sites/default/drushrc.php

See also Pantheon documentation on Known Limitations with drush.

Feedback appreciated

So far, I’ve implemented this for a Drupal 7 project and as such, I haven’t had a chance to test this yet for Drupal 8/9 and so I would appreciate your feedback in that arena. Thanks!

A note about Lando:
People have told me that $_ENV['PANTHEON_ENVIRONMENT'] may be also set on local Lando environments, so I’ve added a line for this in the code snippet above. However, because I don’t use Lando, I’m not sure what values would be provisioned for the domain name there, or if Lando creates its own drushrc.php file in /sites/default/drushrc.php which would override any value set here.

2 Likes

This is so helpful! Thank you!

1 Like