Drupal 8 website development, git, drush and configuration management
The advent of Drupal 8 has brought great news also for those involved in the development of Drupal websites. One of these concerns the simplicity with which you can now manage the entire life cycle of a site or a web application in continuous integration contexts.
It is good practice to avoid making changes directly on a live site without them having been previously tested on a separate environment. For this reason, there are usually several instances of the same site present at the same time in almost always different locations, and in different stages of development:
-
The PC instances of the developers involved in the project, where developments of new features take place
-
The "test" site, on which changes are made and tested before production
-
The "live" site, on the production server, with the latest content, registered users etc..
For those who know about Drupal website development, will know that migrating changes to source files has never been a problem since there are systems of source code versioning, but any website or web application that relies on CMS has another key component heavily involved in most changes or implementations of new features on a website: the database.
The database of the "live" site cannot be simply replaced with that of a development or test site: all the data entered on the site by users in the last hours, days or months (articles, comments, orders in the case of an eCommerce site, etc.) would be lost. By running the "git pull" command on any environment, all new files replace the previous ones, but changes to the database must be transferred appropriately between environments.
Data structures known to those in charge of content building/ development websites Drupal 8 such as content type, block type, all settings under Admin->Configuration and Admin->Appearance, views are stored in the database. How can they be transferred easily and automatically from one instance to another?
In Drupal 7 this result could be achieved through the Features module and a series of modules on which it depends, but the benefits of using this functionality were often overcome by some inherent limitations of the solution.
While if you opt for website development in Drupal 8 there is an elegant solution that makes the migration of configurations between environments a quick and efficient operation: it is called Configuration Management.
To fully exploit the potential of Configuration Management it is necessary to correctly set the environments from the beginning of a project. The follow-up to the article outlines the procedure we have followed in setting up a new Drupal 8 project.
Here are the tools that should be present on each machine for site development in Drupal 8:
It also assumes that you have a git repository to load your new project into.
After having checked out Drupal 8 on the work machine, proceed to launch the command "composer install" to install the software.
It could be useful to immediately create different development "branches" on which you can intervene individually.
Before pushing the code on the repository it is recommended to intervene to avoid that some files end up on the repository. Generally the /sites/default/files folder should not be transferred to Git as the files in it are dependent on each site. The same applies to the settings.php file. This is achieved by creating the file .gitignore in the root of the site, containing the list of files and folders that should not be transferred to the repository.
Now is the time to use the potential of Configuration Management. Drush, the trusted partner of anyone involved in the development of Drupal websites, comes to our aid. The commands to use for our purpose are: drush cex and drush cim.
These two commands are used to manage the export and import of application configurations in file format yaml.
Before going into the specific it is useful to remember that, in the development of Drupal 8 websites, to make this system work it is necessary to reset the folder "Sync", in which the above files are deleted, at the root level of the site and then out of the Drupal web folder. To do this in the settings.php file add the following lines of code (commenting/deleting the existing correspondents):
# Changing The Storage Location of the Sync Directory
$config_directories{{CONFIG_SYNC_DIRECTORY}} = '../config';
With the first command (drush cex) we have the possibility to insert inside of the package the configuration files, during the phase of creation of the commit.
Done this and configured the settings.php file is all set to make the first push of all files.
Now supposing you want to recreate a new site installation on another machine, you just need to clone the repository and run the drush cim command: this makes it possible to import the settings that have been previously committed.
A new view is created on one environment and you want to transfer it to another environment? Same operation:
Environment origin:
drush cex
git push
Target environment:
git pull
drush cim
Through this approach, the continuous integration of a Drupal 8 site becomes a linear, controlled and extremely effective process in following all the phases of web application creation or development of Drupal websites during their natural evolution.
Do you have a new Drupal 8 project starting and do you need advice from our experts in Drupal website development to set it up optimally right away? Contact us now, we are at your disposal to listen to your needs.