Pre-deploy script

Run a bash script over SSH on the preview instance before each deployment — for migrations, first-run seeding, or target-specific setup.

Use the pre_script input to run a local bash script inline over SSH on the preview instance, just before PullPreview runs your selected deployment target. It works for both targets: the script runs before docker compose up (compose) and before the Helm release install or upgrade (helm). Reach for it when you need migrations, first-run seeding, or any one-off preparation step that has to happen before your app comes up.

How it works

pre_script is a path resolved relative to app_path. PullPreview copies your repository to the instance, then executes the script over SSH.

Before your script runs, PullPreview sources /etc/pullpreview/env, so the script can read every PullPreview environment variable, including:

  • PULLPREVIEW_URL
  • PULLPREVIEW_PUBLIC_DNS
  • PULLPREVIEW_PUBLIC_IP
  • PULLPREVIEW_DEPLOYMENT_TARGET
  • PULLPREVIEW_NAMESPACE
  • PULLPREVIEW_RELEASE_NAME
  • PULLPREVIEW_FIRST_RUN

If you set the registries input (compose), PullPreview generates a wrapper script that runs docker login for each registry first, and then runs your pre_script. See private registries for details.

Configuration

Point pre_script at a script in your repository:

- uses: pullpreview/action@v6
with:
pre_script: ./bin/preview-setup.sh

Example: run a migration on first deploy

PULLPREVIEW_FIRST_RUN is set to true the first time an environment is created, and empty on subsequent deployments. Use it to run one-off steps such as schema migrations or seeding only once, instead of on every push to the PR.

#!/usr/bin/env bash
set -euo pipefail
# /etc/pullpreview/env has already been sourced by PullPreview,
# so PULLPREVIEW_* variables are available here.
if [ "${PULLPREVIEW_FIRST_RUN:-}" = "true" ]; then
echo "First run for ${PULLPREVIEW_URL} — running migrations"
docker compose run --rm web bin/rails db:prepare
else
echo "Subsequent run — running migrations only"
docker compose run --rm web bin/rails db:migrate
fi

For loading sample data into a fresh environment, see seeding data, which combines pre_script with PULLPREVIEW_FIRST_RUN to seed each preview once.