Services in Action
Now that we’ve gone through all the components you’ll need to set up Services for your Tugboat, let’s take a look at an example config file so you can see Services in action. This config file is for a Drupal 10 site, but you can check out our starter configuration files to see if we’ve got a code example to kick-start your setup.
1# Default Drupal 10 Tugboat starter config.
2# https://docs.tugboatqa.com/starter-configs/tutorials/drupal-10/
3services:
4 # Define the database service.
5 database:
6 # Use the latest available 5.x version of MySQL
7 image: tugboatqa/mariadb:10.5
8
9 # A set of commands to run while building this service
10 commands:
11 # Configure the server for the site to run on.
12 init:
13 # Increase the allowed packet size to 512MB.
14 - mysql -e "SET GLOBAL max_allowed_packet=536870912;"
15 # Ensure this packet size persists even if MySQL restarts.
16 - echo "max_allowed_packet=536870912" >> /etc/mysql/conf.d/tugboat.cnf
17
18 # Commands that import files, databases, or other assets. When an
19 # existing preview is refreshed, the build workflow starts here,
20 # skipping the init step, because the results of that step will
21 # already be present.
22 update:
23 # TODO: Copy a database dump from an external server. The public
24 # SSH key found in the Tugboat Repository configuration must be
25 # copied to the external server in order to use scp.
26 - scp user@example.com:database.sql.gz /tmp/database.sql.gz
27 - zcat /tmp/database.sql.gz | mysql tugboat
28 - rm /tmp/database.sql.gz
29
30 # Run any commands needed to prepare the site. This is generally not needed
31 # for database services.
32 build: []
33
34 # Define the webserver service.
35 webserver:
36 # This uses PHP 8.1.x with Apache: update to match your version of PHP.
37 image: tugboatqa/php:8.1-apache
38
39 # Set this as the default service. This does a few things
40 # 1. Clones the git repository into the service container
41 # 2. Exposes port 80 to the Tugboat HTTP proxy
42 # 3. Routes requests to the preview URL to this service
43 default: true
44
45 # Wait until the mysql service is done building.
46 depends: database
47
48 # A set of commands to run while building this service
49 commands:
50 # The INIT command configures the webserver.
51 init:
52 # Install opcache and mod-rewrite.
53 - docker-php-ext-install opcache
54 - a2enmod headers rewrite
55
56 # Link the document root to the expected path. This example links /web
57 # to the docroot.
58 - ln -snf "${TUGBOAT_ROOT}/web" "${DOCROOT}"
59
60 # Create any required directories that don't exist.
61 # - mkdir -p "${TUGBOAT_ROOT}/files-private"
62
63 # Commands that import files, databases, or other assets. When an
64 # existing preview is refreshed, the build workflow starts here,
65 # skipping the init step, because the results of that step will
66 # already be present.
67 update:
68 # Install/update packages managed by composer, including drush and Stage File Proxy.
69 - composer install --optimize-autoloader
70 - composer require --dev drupal/stage_file_proxy
71
72 # Set the tugboat-specific Drupal settings.
73 - cp "${TUGBOAT_ROOT}/.tugboat/settings.local.php" "${DOCROOT}/sites/default/settings.local.php"
74
75 # Map your custom modules and themes into the Drupal structure.
76 #- ln -snf "${TUGBOAT_ROOT}/custom/themes" "${DOCROOT}/themes/custom"
77 #- ln -snf "${TUGBOAT_ROOT}/custom/modules" "${DOCROOT}/modules/custom"
78
79 # Make sure our files and translations folders exists and are writable.
80 - mkdir -p "${DOCROOT}/sites/default/files/translations"
81 - chgrp -R www-data "${DOCROOT}/sites/default/files"
82 - find "${DOCROOT}/sites/default/files" -type d -exec chmod 2775 {} \;
83 - find "${DOCROOT}/sites/default/files" -type f -exec chmod 0664 {} \;
84
85 # Optional: Copy Drupal's public files directory from an external server. The
86 # public SSH key found in the Tugboat Repository configuration must be
87 # copied to the external server in order to use rsync over SSH. More commonly
88 # we use Stage File Proxy, which we enable in the `build` step below.
89 - rsync -av --delete user@example.com:/path/to/files/ "${DOCROOT}/sites/default/files/"
90
91 # Commands that build the site. This is where you would add things
92 # like feature reverts or any other drush commands required to
93 # set up or configure the site. When a preview is built from a
94 # base preview, the build workflow starts here, skipping the init
95 # and update steps, because the results of those are inherited
96 # from the base preview.
97 build:
98 # Install new configuration and database updates.
99 - vendor/bin/drush cache:rebuild
100 - vendor/bin/drush config:import --yes
101 - vendor/bin/drush updatedb --yes
102
103 # If you are downloading your files from a remove server, you won't need
104 # to enable Stage File Proxy.
105 - vendor/bin/drush pm:enable --yes stage_file_proxy
106 - vendor/bin/drush config:set --yes stage_file_proxy.settings origin "http://www.example.com"
107 - vendor/bin/drush config:set --yes stage_file_proxy.settings origin_dir "sites/default/files"
108
109 # One last cache rebuild.
110 - vendor/bin/drush cache:rebuild