Skip to content

Instantly share code, notes, and snippets.

@colorfield
Last active March 30, 2021 10:53
Show Gist options
  • Save colorfield/628b5b6ac2a031ddee310c510ae70a51 to your computer and use it in GitHub Desktop.
Save colorfield/628b5b6ac2a031ddee310c510ae70a51 to your computer and use it in GitHub Desktop.
A better Drupal DX with and without Docker

A better Drupal DX with and without Docker

Drupal local setup

Without Docker

drush serve FTW

Why?

  • Less CPU / Ram usage = blazing fast setup 🚀
  • Quickly test a project feature with a clean install
  • Start a POC or test a distribution
  • Use SQLite or MySQL on local host
  • Quickly test against multiple PHP versions

Documentation

With Docker

Lando

Why?

  • The environment is exactly the same as stage and prod.
  • Run extra services like Varnish, Solr, Elasticsearch, Mailhog, ...

Switching between both

settings.php

if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
  include $app_root . '/' . $site_path . '/settings.local.php';
}

settings.local.php

// With MySQL on Lando.
if (getenv('LANDO_HOST_IP') === 'host.docker.internal' &&
  file_exists($app_root . '/' . $site_path . '/settings.lando.php')
) {
  $databases['default']['default'] = [
    'database' => 'drupal9',
    'username' => 'drupal9',
    'password' => 'drupal9',
    'prefix' => '',
    'host' => 'database',
    'port' => '3306',
    'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
    'driver' => 'mysql',
  ];
}
// With SQLite on local host.
else {
  $databases['default']['default'] = [
    'database' => 'sites/default/files/.sqlite',
    'prefix' => '',
    'namespace' => 'Drupal\\Core\\Database\\Driver\\sqlite',
    'driver' => 'sqlite',
  ];
}

Provisioning and deployment

Why?

As a developer, my favourite features are

  • Scalability
  • PR environments
  • CLI and UI
  • GraphQL API e.g. for project tasks, environment variables, ...
  • Simple "pipeline"
  • Simple local/prod Varnish, Solr, Redis, ...
  • Backups
  • Slack notifications about the builds, with logs link

Install in 2 commands

lando start
lando drush si -y

Example pre- and post- rollout (aka "pipeline")

tasks:
  pre-rollout:
    - run:
        name: drush sql-dump
        command: mkdir -p /app/web/sites/default/files/private/ && drush sql-dump --ordered-dump --gzip --result-file=/app/web/sites/default/files/private/pre-deploy-dump.sql.gz || true
        service: cli

  post-rollout:
    - run:
        name: drush cim
        command: drush -y cim
        service: cli
    - run:
        name: drush updb
        command: drush -y updb
        service: cli
    - run:
        name: drush cr
        command: drush -y cr
        service: cli

Default content

Default content

Why?

  • Install relevant (aka non Lorem ipsum) test content also from configuration, while running drush si
  • Supports entity reference (Group, File, ...)
  • Useful to scaffold a base for e.g. E2E or manual tests

Export

Nodes

nid_limit=5 # Presumes that node ids are 1 to 5.
for i in $(seq 1 $nid_limit); do drush dcer node $i --folder=modules/custom/my_default_content/content; done

Terms

tid_limit=5 # Presumes that term ids are 1 to 5.
for i in $(seq 1 $tid_limit); do drush dcer taxonomy_term $i --folder=modules/custom/my_default_content/content; done

Menu links

mlcid_limit=5 # Presumes that menu link content ids are 1 to 5.
for i in $(seq 1 $mlcid_limit); do drush dcer menu_link_content $i --folder=modules/custom/my_default_content/content; done

Import (SQLite local setup)

  • cd web && ../vendor/bin/drush si -y standard --sites-subdir default --account-name admin --account-pass admin --existing-config
  • ../vendor/bin/drush en nn_default_content -y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment