Using command-line workflow processes

Drupal 8's configuration systems solve many problems encountered when exporting and deploying configurations in Drupal 7. However, the task of synchronizing the configuration is still a user interface task and requires the manipulation of archive files that contain the configuration exports for a Drupal 8 site.

Configuration management can be done on the command line through Drush without requiring it to be installed. This mitigates any requirement to log in to the production website to import changes. It also opens the ability for more advanced workflows that place the configuration in version control.

In this recipe, we will use Drush to export the development site's configuration to the filesystem. The exported configuration files will then be copied to the production site's configuration directory. Using Drush, the configuration will be imported into production to complete the deployment.

Getting ready…

You will need a base Drupal site to act as the development site. Another Drupal site, which is a clone of the development site, must be available to act as the production Drupal site.

This recipe uses Drush. If you do not have Drush installed, instructions can be found at http://docs.drush.org/en/master/install/. Drush needs to be installed at both the locations where your Drupal sites are located.

How to do it…

  1. For demonstration purposes, change your development site's name to Drush Config Sync Demo!. This way, there is at least one configuration change to be imported to the production Drupal site.
  2. Open a command-line terminal and change your directory to the working directory of your development Drupal site.
  3. Use the drush config-export command to export the configuration to a directory. The command will default to the sync configuration directory defined in your Drupal 8 site.

    Note

    If you have not explicitly defined a sync directory, Drupal automatically creates a protected folder in the current site's uploaded files' directory, with a unique hash suffix on the directory name.

  4. You will receive a message that the configuration has been exported to the directory.
  5. Using a method of your choice, copy the contents of the configuration sync folder to your other Drupal sites that match the configuration sync folder. For example, a default folder generated by Drupal can be sites/default/files/config_XYZ/sync.
  6. Open a command-line terminal and change your directory to your production Drupal site's working directory.
  7. Use the drush config-import command to begin the process of importing your configuration.
  8. Review the changes made to the configuration entity keys and enter y to confirm the changes:
    How to do it…
  9. Check whether your configuration changes have been imported.

How it works…

The Drush command-line tool is able to utilize the code found in Drupal to interact with it. The config-export command replicates the functionality provided by the Configuration management module's full site export. However, you do not need to have the Configuration management module enabled for the command to work. The command will extract the available site configuration and write it to a directory, which is unarchived.

The config-import command parses the files in a directory. It will make an attempt to run a difference check against the YAML files like the Configuration management module's synchronize overview form does. It will then import all the changes.

There's more…

Drush config-pull

Drush provides a way of simplifying the transportation of configuration between sites. The config-pull command allows you to specify two Drupal sites and move the export configuration between them. You can either specify a name of a subdirectory under the /sites directory or a Drush alias.

The following command will copy a development site's configuration and import it into the staging server's site:

drush config-pull @mysite.local @mysite.staging

Additionally, you can specify the --label option. This represents a folder key in the $config_directories setting. The option defaults to sync automatically. Alternatively, you can use the --destination parameter to specify an arbitrary folder that is not specified in the setting of $config_directories.

Using the Drupal Console

Drush has been part of the Drupal community since Drupal 4.7 and is a custom built command-line tool. The Drupal Console is a Symfony Console-based application used to interact with Drupal. The Drupal Console project provides a means for configuration management over the command line.

Note

You can learn more about the Drupal Console in Chapter 13, Drupal CLI or at http://www.drupalconsole.com/.

The workflow is the same, except the naming of the command. The configuration export command is config:export, and it is automatically exported to your system's temporary folder until a directory is passed. You can then import the configuration using the config:import command.

Editing the configuration from the command line

Both Drush and Drupal Console support the ability to edit the configuration through the command line in YAML format. Both the tools operate in the same fashion and have similar command names:

  • Drush: config-edit [name]
  • Console: config:edit [name]

The difference is that Drush will list all the available options to be edited if you do not pass a name, while Console allows you to search.

When you edit a configuration item, your default terminal-based text editor will open. You will be presented with a YAML file that can be edited. Once you save your changes, the configuration is then saved on your Drupal site:

Editing the configuration from the command line

Exporting a single configuration item

Both Drush and Console provide their own mechanisms for exporting a single configuration entity:

  • Drush: config-get [name]
  • Console: config:debug [name]

Drush will print the configuration's output to the terminal, while Console's default behavior is to write the output to the file disk. For example, the following commands will output the values from system.site in YAML format:

$ drush config-get system.site
$ drupal config:debug system.site
Exporting a single configuration item

Using version control and command-line workflow

A benefit of having the configuration exportable to YAML files is the fact that the configuration can be kept in version control. The Drupal site's CONFIG_SYNC_DIRECTORY directory can be committed to version control to ensure that it is transported across environments and properly updated. Deployment tools can then use Drush or Console to automatically import changes.

The config-export command provided by Drush provides the Git integration:

drush config-export --add 

Appending the --add option will run git add -p for an interactive staging of the changed configuration files:

drush config-export --commit --message="Updating configuration "

The --commit and optional --message options will stage all configuration file changes and commit them with your message:

drush config-export --push --message="Updating configuration " 

Finally, you can also specify --push to make a commit and push it to the remote repository.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset