> ## Documentation Index
> Fetch the complete documentation index at: https://developer.upsun.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Use build and deploy hooks

> Add custom scripts at different stages in the build and deploy process.

As your app goes through the [build and deploy process](https://developer.upsun.com/docs/core-concepts/build-deploy.md),
you might want to run custom commands.
These might include compiling the app, setting the configuration for services based on variables, and rebuilding search indexes.
Do these tasks using one of [three hooks](https://developer.upsun.com/docs/configure-apps/hooks/hooks-comparison.md).

The following example goes through each of these hooks for a multi-app project

* Next.js acts as the frontend container, `client`
* Drupal serves data as the backend container, `api`

Configuration for [both applications](https://developer.upsun.com/docs/configure-apps/multi-app) resides in a single [`.upsun/config.yaml` configuration file](https://developer.upsun.com/docs/configure-apps).
Be sure to notice the `source.root` property for each.

## Build dependencies

The Next.js app uses Yarn for dependencies, which need to be installed.
Installing dependencies requires writing to disk and doesn't need any relationships with other services.
This makes it perfect for a `build` hook.

In this case, the app has two sets of dependencies:

* For the main app
* For a script to test connections between the apps

Create your `build` hook to install them all:

1. Create a `build` hook in your [app configuration](https://developer.upsun.com/docs/configure-apps/app-reference/single-runtime-image.md):


&#x20;   applications:
&#x20;     client:
&#x20;       type: 'nodejs:\{\{version:nodejs:latest}}'
&#x20;       source:
&#x20;         root: client
&#x20;       hooks:
&#x20;         build: |
&#x20;           set -e
&#x20;&#x20;

The hook has two parts so far:

* The `|` means the lines that follow can contain a series of commands.
  They aren't interpreted as new YAML properties.
* Adding `set -e` means that the hook fails if *any* of the commands in it fails.
  Without this setting, the hook fails only if its *final* command fails.

  If a `build` hook fails for any reason, the build is aborted and the deploy doesn't happen.
  Note that this only works for `build` hooks.
  If other hooks fail, the deploy still happens.

2. Install your top-level dependencies inside this `build` hook:


&#x20;   applications:
&#x20;     client:
&#x20;       type: 'nodejs:\{\{version:nodejs:latest}}'
&#x20;       source:
&#x20;         root: client
&#x20;       hooks:
&#x20;         build: |
&#x20;           set -e
&#x20;           yarn --frozen-lockfile
&#x20;&#x20;

This installs all the dependencies for the main app.

## Configure Drush and Drupal

The example uses [Drush](https://www.drush.org/latest/) to handle routine tasks.
For its configuration, Drush needs the URL of the site.
That means the configuration can't be done in the `build` hook.
During the `build` hook, the site isn't yet deployed and so there is no URL to use in the configuration.
(The [`PLATFORM_ROUTES` variable](https://developer.upsun.com/docs/development/variables/use-variables#use-provided-variables) isn't available.)

Add the configuration during the `deploy` hook.
This way you can access the URL before the site accepts requests (unlike in the `post_deploy` hook).

The script also prepares your environment to handle requests,
such as by [rebuilding the cache](https://www.drush.org/latest/commands/cache_rebuild/)
and [updating the database](https://www.drush.org/latest/commands/updatedb/).
Because these steps should be done before the site accepts request, they should be in the `deploy` hook.

All of this configuration and preparation can be handled in a bash script.

1. Copy the [preparation script from the Upsun Fixed template](https://github.com/platformsh-templates/nextjs-drupal/blob/master/api/platformsh-scripts/hooks.deploy.sh)
   into a file called `hooks.deploy.sh` in a `api/platformsh-scripts` directory.
   Note that hooks are executed using the dash shell, not the bash shell used by SSH logins.
2. Copy the [Drush configuration script from the template](https://github.com/platformsh-templates/nextjs-drupal/blob/master/api/drush/platformsh_generate_drush_yml.php)
   into a `drush/platformsh_generate_drush_yml.php` file.
3. Set a [mount](https://developer.upsun.com/docs/configure-apps/image-properties/mounts.md).
   Unlike in the `build` hook, in the `deploy` hook the system is generally read-only.
   So create a mount where you can write the Drush configuration:


&#x20;   applications:
&#x20;     api:
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       source:
&#x20;         root: api

&#x20;       mounts:
&#x20;         /.drush:
&#x20;           source: storage
&#x20;           source\_path: 'drush'
&#x20;&#x20;

4. Add a `deploy` hook that runs the preparation script:


&#x20;   applications:
&#x20;     api:
&#x20;       type: 'php:\{\{version:php:latest}}'
&#x20;       source:
&#x20;         root: api

&#x20;       mounts:
&#x20;         /.drush:
&#x20;           source: storage
&#x20;           source\_path: 'drush'

&#x20;       hooks:
&#x20;         deploy: !include
&#x20;           type: string
&#x20;           path: platformsh-scripts/hooks.deploy.sh
&#x20;&#x20;

This `!include` syntax tells the hook to process the script as if it were included in the YAML file directly.
This helps with longer and more complicated scripts.

## Get data from Drupal to Next.js

This Next.js app generates a static site.
Often, you would generate the site for Next.js in a `build` hook.
In this case, you first need to get data from Drupal to Next.js.

This means you need to wait until Drupal is accepting requests
and there is a relationship between the two apps.
So the `post_deploy` hook is the perfect place to build your Next.js site.

You can also redeploy the site every time content changes in Drupal.
On redeploys, only the `post_deploy` hook runs,
meaning the Drupal build is reused and Next.js is built again.
So you don't have to rebuild Drupal but you still get fresh content.

1. Set a relationship for Next.js with Drupal.
   This allows the Next.js app to make requests and receive data from the Drupal app.


&#x20;   applications:
&#x20;     client:
&#x20;       type: 'nodejs:\{\{version:nodejs:latest}}'
&#x20;       source:
&#x20;         root: client

&#x20;       relationships:
&#x20;         api:
&#x20;           service: 'api'
&#x20;           endpoint: 'http'
&#x20;&#x20;

2. Set [mounts](https://developer.upsun.com/docs/configure-apps/image-properties/workers.md).
   Like the [`deploy` hook](#configure-drush-and-drupal), the `post_deploy` hook has a read-only file system.
   Create mounts for your Next.js files:


&#x20;   applications:
&#x20;     client:
&#x20;       type: 'nodejs:\{\{version:nodejs:latest}}'
&#x20;       source:
&#x20;         root: client

&#x20;       mounts:
&#x20;         /.cache:
&#x20;           source: tmp
&#x20;           source\_path: 'cache'
&#x20;         /.next:
&#x20;           source: storage
&#x20;           source\_path: 'next'
&#x20;         /.pm2:
&#x20;           source: storage
&#x20;           source\_path: 'pm2'
&#x20;         deploy:
&#x20;           source: storage
&#x20;           service: files
&#x20;           source\_path: deploy
&#x20;&#x20;

3. Add a `post_deploy` hook that first tests the connection between the apps:


&#x20;   applications:
&#x20;     client:
&#x20;       type: 'nodejs:\{\{version:nodejs:latest}}'
&#x20;       source:
&#x20;         root: client
&#x20;       hooks:
&#x20;         post\_deploy: |
&#x20;           . deploy/platformsh.environment
&#x20;           cd platformsh-scripts/test && yarn debug
&#x20;&#x20;

Note that you could add `set -e` here, but even if the job fails, the build/deployment itself can still be counted as successful.

4. Then build the Next.js site:


&#x20;   applications:
&#x20;     client:
&#x20;       type: 'nodejs:\{\{version:nodejs:latest}}'
&#x20;       source:
&#x20;         root: client
&#x20;       hooks:
&#x20;         post\_deploy: |
&#x20;           . deploy/platformsh.environment
&#x20;           cd platformsh-scripts/test && yarn debug
&#x20;           cd \$PLATFORM\_APP\_DIR && yarn build
&#x20;&#x20;

The `$PLATFORM_APP_DIR` variable represents the app root and can always get you back there.

## Final hooks


&#x20;   applications:
&#x20;     api:
&#x20;       \# The runtime the app uses.
&#x20;       type: 'php:\{\{version:php:latest}}'

&#x20;       \# The relationships of the app with services or other apps.
&#x20;       relationships:
&#x20;         database:
&#x20;           service: 'db'
&#x20;           endpoint: 'mysql'
&#x20;         redis:
&#x20;           service: 'cache'
&#x20;           endpoint: 'redis'

&#x20;       \# The hooks executed at various points in the lifecycle of the app.
&#x20;       hooks:
&#x20;         deploy: !include
&#x20;         type: string
&#x20;         path: platformsh-scripts/hooks.deploy.sh

&#x20;       \# The 'mounts' describe writable, persistent filesystem mounts in the app.
&#x20;       mounts:
&#x20;         /.drush:
&#x20;           source: storage
&#x20;           source\_path: 'drush'
&#x20;         /drush-backups:
&#x20;           source: storage
&#x20;           source\_path: 'drush-backups'
&#x20;         deploy:
&#x20;           source: service
&#x20;           service: files
&#x20;           source\_path: deploy
&#x20;     client:
&#x20;       \# The type key specifies the language and version for your app.
&#x20;       type: 'nodejs:\{\{version:nodejs:latest}}'

&#x20;       dependencies:
&#x20;         nodejs:
&#x20;           yarn: "1.22.17"
&#x20;           pm2: "5.2.0"

&#x20;       build:
&#x20;         flavor: none

&#x20;       relationships:
&#x20;         api:
&#x20;           service: 'api'
&#x20;           endpoint: 'http'

&#x20;       \# The hooks that are triggered when the package is deployed.
&#x20;       hooks:
&#x20;         build: |
&#x20;           set -e
&#x20;           yarn --frozen-lockfile # Install dependencies for the main app
&#x20;           cd platformsh-scripts/test
&#x20;           yarn --frozen-lockfile # Install dependencies for the testing script
&#x20;         \# Next.js's build is delayed to the post\_deploy hook, when Drupal is available for requests.
&#x20;         post\_deploy: |
&#x20;           . deploy/platformsh.environment
&#x20;           cd platformsh-scripts/test && yarn debug
&#x20;           cd \$PLATFORM\_APP\_DIR && yarn build

&#x20;       mounts:
&#x20;           /.cache:
&#x20;               source: tmp
&#x20;               source\_path: 'cache'
&#x20;           /.next:
&#x20;               source: storage
&#x20;               source\_path: 'next'
&#x20;           /.pm2:
&#x20;               source: storage
&#x20;               source\_path: 'pm2'
&#x20;           deploy:
&#x20;               source: storage
&#x20;               service: files
&#x20;               source\_path: deploy


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.