Screencast - Develop your Drupal 8 site with Drush, Composer and Platform.sh

Augustin Delaporte
General Manager China
21 Sep 2017

In this tutorial we will see how to quickly deploy a new Drupal 8 site and build a couple of features, in a repeatable, testable and automated way, using a combination of Drupal’s configuration management, Drush, and Platform.sh’s great site management tools.

Drupal 8 makes it very easy to import and export the configuration of your site, allowing you to store settings, that normally live in your database, in code. This is great for managing  configuration changes from development to production. In addition, the combination of Drush, Composer and Platform.sh make it very easy to automate your development and maintenance workflow. Drush provides the ability to control your Drupal site, while the Platform.sh CLI manages your environments.

Before you start

Before you start, you will need the following installed locally:

  1. The Platform.sh CLI
  2. The latest stable PHP
  3. MySQL or MariaDB

Plan

In this guide, we will:

  1. Create a project and get a local copy
  2. Setup the local database
  3. Install Drupal remotely using the Platform.sh CLI
  4. Test and deploy a configuration change
  5. Switch to a new theme
  6. Add a new module
  7. Deploy to production
  8. Synchronise code and data to development environments

Create your project and get a local copy

Let’s start by creating a Platform.sh project from the [web interface](https://platform.sh/free-trial) of directly from the Platform.sh CLI (warning: this feature requires the experimental tag in your configuration).

platform project:create --title ‘Drupal 8’

When the project is ready, access the Web UI, click Next to confirm the project name, select Create a blank site from a template and then choose the Drupal 8 stack.

This is the only time we’ll be using the Platform.sh UI in this tutorial.

From here on we’ll be working with the CLI only.

Clone and build the project locally:

platform get jsqpqkmu4axfm
cd drupal-8
platform build

This will download all our dependencies via Composer and organize our local codebase.

Setup your local database

Let’s create a local MySQL database for our local Drupal installation.

Platform.sh automatically loads a local settings file to ensure that we don’t mix local configuration with remote environments. Edit _www/sites/default/settings.local.php to match your local database configuration:

$databases['default']['default'] = array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'username' => 'root',
'password' => 'root',
'database' => 'drupal_8',
'prefix' => '',
);
$settings["hash_salt"] = "abc";

Now it’s time for the fun part...

Install Drupal using the Platform.sh CLI

We can install the remote site from our terminal using the Platform.sh CLI, which can run any Drush command on the remote environment:

platform drush si

Once the site is installed on the remote environment, you can synchronize it with your local site simply by running:

drush sql-sync @drupal-8.master @drupal-8._local

Test a configuration change

To see how to make a simple configuration change, we  can use Drush to change the site name and export the configuration, using drush config-export (or drush cex).

You will need to run this from the web root of your Drupal site, which in a Platform.sh local build is under an alias at ./_www:

cd _www
drush config-set system.site name “MyDrupal 8 Site”
drush config-export

Now we can use Git to automatically deploy this new configuration to our remote site:

cd ..
git add --all
git commit -m “Initial configuration and set site name”
git push platform master

Platform.sh will automatically redeploy our remote site with these configuration changes. You can see the commands that are triggered during the deployment in the .platform.app.yaml file:

hooks:
# The deploy hook runs after your application has been deployed and started.
deploy: |
cd web
drush -y cache-rebuild
drush -y updatedb
drush -y config-import
drush -y entup

Start a new theme

In our case, we want to use another theme for our site. But we don’t want to mess up our master environment, which eventually is your production site, so  we will create a specific remote environment to test our new theme.

Platform.sh makes it very easy and fast to clone any existing environment into an exact copy that we can use to implement any new feature:

platform environment:branch new-theme

Now, let’s use Composer to download the starter Bootstrap theme and enable it in our local site:

composer require drupal/bootstrap
drush en bootstrap -y
drush config-set system.theme default bootstrap

This will automatically update the composer.json and composer.lock files and set the Bootstrap theme as our new default.

Again, we want to export this configuration and deploy it to our remote theme environment.

drush config-export
git add --all
git commit -m “Add and enable Bootstrap theme”
git push

This will automatically redeploy the remote new-theme environment with these changes. You now have a live environment to test your theme and continue to work on it.

Add a new module

This site is going to be an eCommerce site, so we’re going to add Drupal Commerce. Again, we don’t want to mess up the existing master environment, so we will create another copy of our master environment to enable the Commerce module:

platform environment:branch commerce

We need to make sure that our local database is clean and doesn’t include previous configuration:

drush config-import

We can then use Composer (again) to download the Commerce module and enable it in our local site:

composer require drupal/commerce
drush en commerce -y

Export the configuration and deploy it to our remote commerce environment.

drush config-export
git add --all
git commit -m “Enable Commerce module”
git push

We now have the Commerce module enabled on your remote commerce environment and start configuring it and extending it following the same workflow.

Deploy to production

Let’s say we are happy with our Bootstrap theme and we want to deploy it to production (also known as the master environment).

Platform.sh makes it easy to deploy a feature to production following the exact same workflow.

platform checkout theme
platform environment:merge

This will redeploy the master site with the content and configuration of our theme environment.

Synchronise data to development environments

Let’s not forget to update our commerce environment so that it is up to date with what has been deployed to production. Platform.sh makes it easy to bring your development environments up to date with your production data using the environment:synchronise command:

platform checkout commerce
platform environment:synchronize

 

That's it, time for you to continue building your site from here. If you have any questions, feel free to ask in the comments section or reach out to us in our public Slack channel.