7

I'm using Drush 9. With the old drushrc.php I could easily include a local.drushrc.php that dynamically set the site url. How can I do it now?

This is similar to How can i set site url dynamically in Drush-9?. However I don't want to pass the url in every Drush command. I think of something like below in ../drush/drush.yml:

drush:
  paths:
    config:
      - ${env.home}/local.drush.yml

But I'd need another variable that point to the Drupal site directory (sites/default in a monosite, sites/CURRENT-SITE in a multisite). Does such a variable exist?

2 Answers 2

8

Here's what I do on my sites and it works:

Step 1: sites/default/drush.local.yml

options:
  uri: 'https://dev.example.com'

You can name this file however you want. Also, since this file varies from env to env, I ignore it with .gitignore. At the same time, I commit an example.drush.local.yml.

Step 2: drush/drush.yml

  • One level above Drupal installation, .i.e. where Drupal's index.php is located, I have my composer.json and vendor directories.
  • In this directory, I create a directory named drush and in it, I place a drush.yml with these contents:
drush:
  paths:
    config:
      - web/sites/default/drush.local.yml

After this, I running drush st from that directory, will show you that the drush.local.yml has been detected and the site URI is set correctly.

3
  • This the correct answer. I have read a few posts on using drushrc.php and sites.php, but they did not work for me with Drupal 9.X. +1 Thanks Jan 23, 2023 at 19:22
  • I second that, and what is awesome is you can use environment variables in the yml using ${env.VAR_NAME}. I wanted this to be in my repo, so I created a file for each like env-${env.DEPLOY_ENV}.drush.yml so env-stage.drush.yml and within each file put the domain name I wanted to use. You could also try uri: 'https://${env.VIRTUAL_HOST}' and a single drush file if you for example had access to your URI in that manner. That would make it super dynamic. Mar 23, 2023 at 19:54
  • 1
    Great answer, I was wondering how to do it in the last few days. May 8, 2023 at 20:40
1

Reading https://github.com/drush-ops/drush/blob/master/examples/example.drush.yml it seems you can put a site specific drush.yml in a sites directory. It is merged with the global file(s).

PS! I haven't tested this myself.

7
  • 1
    Tested but it didn't work. I agree that it should have worked.
    – jcisio
    Jun 29, 2018 at 6:49
  • Try checking your own example.drush.yml file in case you have an older version. The local config filename used to be different.
    – Andy
    Jun 29, 2018 at 9:14
  • I'm already on Drush 9.3.0. The file sites/default/drush.yml is not automatically detected. I thought this was a problem with lightning, but I try with a standard distribution and I have the same result.
    – jcisio
    Jul 5, 2018 at 13:35
  • Confirming that I'm on Drush 9.5.2 and the documentation suggests that the first place it checks is sites/default/drush.yml but this doesnt seem to be the case, because it is not picking up the default --uri option i have configured in that file options: uri: http://mysite.test when I run drush uli. Not sure what's wrong. Oct 26, 2018 at 16:41
  • 2
    Ok, I've figured this out: For sites/default/drush.yml to be recognized by drush you have to run drush from the drupal root folder. This used to not be a problem in drush 8 with sites/default/drushrc.php Oct 26, 2018 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.