Zum Inhalt springen

Drupal 8 Website Developer Environments

Aus Computech CMS Wiki

Managing Development Environments

This section describes how to use composer, drush, git, git flow and Drupal Configuration Management to move changes between development environments.

Using Drush Aliases

Drush site aliases have been created for each development environment. To get a list of available Drush site aliases:

drush sa

To see the information contained in a Drush site alias:

drush sa @meos.d8

To list the status of a Drush site:

drush st @meos.d8

Commands can be run using a drush alias even when you are not inside the folders of a Drupal website. To clear the registry on a Drupal 8 site from anywhere:

drush @meos.d8 cr

Export main development site configuration to sync directory

The location of site configuration sync directory should be configured in the settings.php file or settings.local.php. Many are using ../config/sync. This is outside the webroot for security reasons. The command export a Drupal 8 website configuration to the configuration sync directory:

drush config-export

or use the short command:

drush cex

To export Drupal configuration and choose changes to commit to git run:

drush cex --add

To export Drupal configuration and commit all changes to current git branch:

drush cex --commit --message="This is the commit message."

To export Drupal configuration and commit all changes to current git branch and push to remote git repository:

drush cex --push --message="This is the commit message."

Import configuration changes into your developer environment

Short version

First go to the webroot folder (or use your sites drush site alias from anywhere) and first check that you do not have any configuration changes that will be overwritten with:

drush cex

If you do have changes that should not be overwritten answer yes to have them exported. Then in the project root do:

git pull origin dev
composer install

Then in the Drupal webroot do:

drush updatedb
drush cim
drush cr

Long version

First go to the webroot folder (or use your sites drush site alias from anywhere) and first check that you do not have any configuration changes that will be overwritten with:

drush cex

or using your sites drush alias from anywhere on the dev01 server:

drush @meos.d8 cex

If you do have changes that should not be overwritten answer yes to have them exported.

More information about why it is important to export your configuration before pulling configuration changes can be found in the Important notes section of Git workflow for managing Drupal 8 configuration


Next get the changes from the remote dev branch of the Git repository. On your Git Flow feature branch that you want to get changes from:

git pull origin dev

NOTE: If modules have been installed, updated or removed you will also need to run:

composer install

This will run all the changes in the composer.json file for the project. This will make all need changes to modules, themes and libraries.

NOTE: If modules have changed versions from the webroot you will also need to run the database update to make changes to the database that newer version of modules require.

drush updatedb

or with a Drush site alias

drush @meos.d8 updatedb

In the webroot import changes into the development environment with:

 drush config-import

Or use the short form:

drush cim

or from anywhere using a drush site alias:

drush @meos.d8 cim

If you would like to see a diff of the changes that will be applied:

drush cim --preview=diff

After importing Drupal configuration it is always good to rebuild a Drupal 8 site and clear all its caches with:

drush cache-rebuild

Or its short form:

drush cr

or with a Drush site alias

drush @meos.d8 cr

Safe Sequences

The following are from the Config Split developers presentation at Drupal Dev Days Seville in March 2017. A PDF of the presentation slides can be downloaded from the Nuvole. Information below is from slides 38-42.

Collaboration issues

A careless workflow may result in:

  • Losing all uncommitted work.
  • Accidentally overwrite work by others.
  • A configuration that looks OK at first sight but that is actually invalid for Drupal.

The safe sequence for sharing

  1. Export configuration: drush cex
  2. Commit
  3. Merge: git pull
  4. Update dependencies: composer install
  5. Run updates: drush updb
  6. Import configuration: drush cim
  7. Push: git push

If you do it wrong...

  • Import before Export: Deletes your work, no backup.
  • Merge before Export: Export deletes previous work, solved by git.
  • No updb or after cim (will be disallowed in the future): database might be broken.
  • No composer install: may not have all the updated code.
  • Merge before Commit: Manual labour on conflicts.
  • Forgotten Import: Next export will not contain merged config, more difficult to solve in git.

The safe sequence for updating

  1. Update code: composer update
  2. Run updates: drush updb
  3. Export updated config: drush cex
  4. Commit
  5. Push: git push

Breaking con guration with Git

  • Setup: Installed standard profile
  • Developer A on branch feature-a deletes Tags from 'Article'.
    • Resulting configuration change: 2 files are removed (field instance and field storage)
  • Developer B on branch feature-b adds Tags to 'Basic page'.
    • Resulting configuration change: 1 file is added (field instance)
  • Git will happily merge feature-a and feature-b into develop
  • The resulting configuration is invalid:
    • Tags has a field instance but no storage.

Takeaway: when merging check that the configuration is still valid by importing it.

Git Flow Setup / Configuration

Navigate to the folder where you want the git repository. To create a new git repository run: (you can also use a current git repository, just skip this step)

git init

To configure git flow run:

git flow init

For websites I prefer to use different names then the defaults for the production releases and the next releases. Production releases are on the live branch and the next release branch is on the dev branch.

No branches exist yet. Base branches must be created now.
Branch name for production releases: [master] live
Branch name for "next release" development: [develop] dev
How to name your supporting branch prefixes?
Feature branches? [feature/] 
Bugfix branches? [bugfix/] 
Release branches? [release/] 
Hotfix branches? [hotfix/] 
Support branches? [support/] 
Version tag prefix? [] 
Hooks and filters directory? [/tmp/aaa-tmp/.git/hooks]

This Git flow configuration only needs to be done once for a developer's local git repository. The is not saved to the remote repository. To see the current Git Flow configuration:

git flow config

Add Bonobo or other remote git repository to local git folder

git remote add 

Then you can pull the remote repository if it has commits or make a commit then you can do:

git push origin <BranchName

z.B for the dev branch

git push origin dev

Set tracking information for git pull for a remote repository

To be able to do just:

git pull

instead of:

git pull origin <BranchName>

You can set the remote branch to pull from each time like this:

git branch --set-upstream-to=<RemoteRepositoryName>/<RemoteBranchName> <LocalBranchName>

z.B to set the Remote repository with the name origin for the local branch dev to the remote branch dev

git branch --set-upstream-to=origin/dev dev

How to start a new git flow branch for development work

Normal development work should be done on "feature" branches not on dev.

git flow start feature <FeatureName>

z.B. new branch for paragraphs theming

git flow start feature paragraphstheming

How to finish your git flow feature branch

When you have finished your work and want to merge your branch into the dev branch and delete your feature branch:

git flow finish <FeatureName>

Or if you are currently on the git flow feature branch you can also use the short version

git flow finish

Add custom theme from separate repository to a developer environment

In the developer environment:

cd themes/custom

Clone repository.

git clone <UrlToRepository>

z.B. MEOS Adminimal Theme

git clone http://192.168.60.12/Bonobo.Git.Server/meostheme.git

Copy Drupal 8 Website to developer environments

  • cd in to the new developer environment:
cd /var/www/meos-fh
  • Initialize the folder for git:
git init
  • Add the Git Repository as a remote called origin:
git remote add origin <repository_URL>
  • Pull the repository to the directory:
git pull
  • Check out the development branch dev:
git checkout dev
  • Fix permissions on sites/default so Drupal can write to it:
chmod -R g+w docroot/sites/default/
  • Change to sites/default directory:
cd docroot/sites/default/
  • Copy as settings.local.php from another development site:
cp  /var/www/meos/docroot/sites/default/settings.local.php ./
  • Fix permissions of settings.local.php
chmod u+w settings.local.php
  • Edit the file:
nano settings.local.php

Change database settings to the new database for the website.

  • Change back to the project folder:
cd ../../../
  • Run Composer to install dependencies:
composer install
  • Pull the database from the main development site: Where @meos.fh is the Drush site alias you want to update.(NOTE:Drush shell alias needs to be created in the aliases file in /etc/drush/ for the pull-live.db command to work.)
drush @meos.fh pull-live-db
  • Import the configuration from the config/sync directory with Drush:
drush @meos.fh cim  #(cim is short for config-import)