Drupal 9
Wondering how to configure Tugboat for a typical Drupal 9 repository? Every Drupal site tends to have slightly different requirements, so you may need to do more customizing, but this should get you started.
The following documentation assumes you are using Composer to manage your Drupal 9 project (typically with either the
drupal/recommended-project or the drupal-composer/drupal-project projects).
Configure Drupal
A common practice for managing Drupal’s settings.php is to leave sensitive information, such as database credentials,
out of it and commit it to git. Then, the sensitive information is loaded from a settings.local.php file that exists
only on the Drupal installation location.
This pattern works very well with Tugboat. It lets you keep a Tugboat-specific set of configurations in your repository, where you can copy it into place with a configuration file command.
Add or uncomment the following at the end of settings.php
1if (file_exists($app_root . '/' . $site_path . '/settings.local.php')) {
2 include $app_root . '/' . $site_path . '/settings.local.php';
3}Add a file to the git repository at .tugboat/settings.local.php with the following content:
1<?php
2$databases['default']['default'] = array (
3 'database' => 'tugboat',
4 'username' => 'tugboat',
5 'password' => 'tugboat',
6 'prefix' => '',
7 'host' => 'mysql',
8 'port' => '3306',
9 'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
10 'driver' => 'mysql',
11);
12
13// Use the TUGBOAT_REPO_ID to generate a hash salt for Tugboat sites.
14$settings['hash_salt'] = hash('sha256', getenv('TUGBOAT_REPO_ID'));
15
16// If your Drupal config directory is outside of the Drupal web root, it's
17// recommended to uncomment and adapt the following. Note: the TUGBOAT_ROOT
18// environment variable is equivalent to the git repo root.
19# $settings['config_sync_directory'] = getenv('TUGBOAT_ROOT') . '/config';
20
21// If you are using private files, and that directory is outside of the Drupal
22// web root, it's recommended to uncomment and adapt the following. Note: the
23// TUGBOAT_ROOT environment variable is equivalent to the git repo root.
24# $settings['file_private_path'] = getenv('TUGBOAT_ROOT') . '/files-private';
25
26/**
27 * Trusted host configuration for Tugboat preview environments.
28 *
29 * Drupal requires you to specify which hostnames are allowed to access your
30 * site. Since Tugboat preview URLs use the tugboatqa.com domain, we add this
31 * pattern to allow Drupal to accept requests from any Tugboat preview URL.
32 *
33 * @see https://www.drupal.org/docs/installing-drupal/trusted-host-settings
34 */
35$settings['trusted_host_patterns'] = [
36 '\.tugboatqa\.com$',
37];
38
39/**
40 * Set the memory limit for the CLI.
41 */
42if (PHP_SAPI === 'cli') {
43 ini_set('memory_limit', '-1');
44}Configure Tugboat
The Tugboat configuration is managed by a YAML file at
.tugboat/config.yml in the git repository. Here’s a basic Drupal 9 configuration you can use as a starting point, with
comments to explain what’s going on:
1services:
2 # What to call the service hosting the site.
3 php:
4 # This uses PHP 8.1.x with Apache: update to match your version of PHP
5 image: tugboatqa/php:8.1-apache
6
7 # Set this as the default service. This does a few things
8 # 1. Clones the git repository into the service container
9 # 2. Exposes port 80 to the Tugboat HTTP proxy
10 # 3. Routes requests to the preview URL to this service
11 default: true
12
13 # Wait until the mysql service is done building
14 depends: mysql
15
16 # A set of commands to run while building this service
17 commands:
18 # Commands that set up the basic preview infrastructure
19 init:
20 # Install opcache and mod-rewrite.
21 - docker-php-ext-install opcache
22 - a2enmod headers rewrite
23
24 # Link the document root to the expected path. This example links /web
25 # to the docroot.
26 - ln -snf "${TUGBOAT_ROOT}/web" "${DOCROOT}"
27
28 # Create the Drupal private and public files directories if they aren't
29 # already present.
30 - mkdir -p "${TUGBOAT_ROOT}/files-private" "${DOCROOT}/sites/default/files"
31
32 # Commands that import files, databases, or other assets. When an
33 # existing preview is refreshed, the build workflow starts here,
34 # skipping the init step, because the results of that step will
35 # already be present.
36 update:
37 # Use the tugboat-specific Drupal settings.
38 - cp "${TUGBOAT_ROOT}/.tugboat/settings.local.php" "${DOCROOT}/sites/default/"
39
40 # Install/update packages managed by composer.
41 - composer install --optimize-autoloader
42
43 # Copy Drupal's public files directory from an external server. The
44 # public SSH key found in the Tugboat Repository configuration must be
45 # copied to the external server in order to use rsync over SSH.
46 - rsync -av --delete user@example.com:/path/to/files/ "${DOCROOT}/sites/default/files/"
47
48 # Alternatively, another common practice is to use the
49 # stage_file_proxy Drupal module. This module lets Drupal serve
50 # files from another publicly-accessible Drupal site instead of
51 # syncing the entire files directory into the Tugboat Preview.
52 # This results in smaller previews and reduces the build time.
53 # You will need stage_file_proxy in your composer dependencies.
54 - vendor/bin/drush pm:enable --yes stage_file_proxy
55 - vendor/bin/drush config:set --yes stage_file_proxy.settings origin "http://www.example.com"
56
57 # Set file permissions such that Drupal will not complain.
58 - chgrp -R www-data "${DOCROOT}/sites/default/files"
59 - find "${DOCROOT}/sites/default/files" -type d -exec chmod 2775 {} \;
60 - find "${DOCROOT}/sites/default/files" -type f -exec chmod 0664 {} \;
61
62 # Commands that build the site. This is where you would add things
63 # like feature reverts or any other drush commands required to
64 # set up or configure the site. When a preview is built from a
65 # base preview, the build workflow starts here, skipping the init
66 # and update steps, because the results of those are inherited
67 # from the base preview.
68 build:
69 - composer install --optimize-autoloader
70 - vendor/bin/drush cache:rebuild
71 - vendor/bin/drush config:import -y
72 - vendor/bin/drush updatedb -y
73 - vendor/bin/drush cache:rebuild
74
75 # This 'mysql' key acts as the hostname to access the service by from the php service.
76 mysql:
77 # Use the latest available 5.x version of MySQL
78 image: tugboatqa/mariadb:10.5
79
80 # A set of commands to run while building this service
81 commands:
82 # Commands that import files, databases, or other assets. When an
83 # existing preview is refreshed, the build workflow starts here,
84 # skipping the init step, because the results of that step will
85 # already be present.
86 update:
87 # Copy a database dump from an external server. The public
88 # SSH key found in the Tugboat Repository configuration must be
89 # copied to the external server in order to use scp.
90 - scp user@example.com:database.sql.gz /tmp/database.sql.gz
91 - zcat /tmp/database.sql.gz | mysql tugboat
92 - rm /tmp/database.sql.gzWant to know more about something mentioned in the comments of this config file? Check out these topics:
- Name your Service
- Specify a Service image
- Leverage Service commands
- Define a default Service
- Set the document root path
- Set up remote SSH access
- Preview build process phases (
init,update,build) - How Base Previews work
- Multisite with Tugboat
Start Building Previews!
Once the Tugboat configuration file is committed to your git repository, you can start building previews!