Preview Commands
Helm can run three commands of your own in each preview: one before the stack starts, one that starts it, and one after it has started. This page explains when each runs, what it runs when you leave it empty, and how a failure shows up.
What you're about to do, in plain English
Most backends need a little work done around each deploy. The common case is a migration: a script, usually part of your framework, that creates or changes the tables in the database so they match the code. Every preview starts with an empty database, so the migrations must run before the app can serve requests.
Each preview is a virtual machine that runs your Docker Compose file (see
Write the Compose File).
The three commands are shell commands that Helm runs in that virtual machine, in the folder of
your Compose file, with docker compose available. They are on your project's
Settings → Preview Environments tab. Changing them needs the
Project Admin role or higher.
The three commands
| Setting | When it runs | When it is empty |
|---|---|---|
| Pre-deploy Command | First, before the stack starts, on every new preview and every rebuild. | Nothing runs. |
| Start Command | Second, on every new preview and every rebuild. It brings the stack up and must return once the services are running. | docker compose up --build --detach --wait runs. |
| First-deploy Command | Last, after the web service answers, on every new preview and every rebuild. | Nothing runs. |
After the start command, Helm waits for the web service to answer on the App Port before it runs the first-deploy command and marks the preview ready.
How Helm runs a command
- As written, as shell text, in the preview's virtual machine, not inside one of your containers. To run something inside a container, use
docker compose runordocker compose exec, as in the examples below. - In the folder of your Compose file, so
docker composefinds the file without-f. - With your variables and secrets in its environment, and with
PREVIEW_DOMAINandPREVIEW_URL. - One line each. To run several steps, join them with
&&, or put them in a script in your repository and run the script, for examplesh scripts/predeploy.sh. - At most 4096 bytes long (a character such as
écounts as two bytes). Helm removes spaces at the start and end.
Example: migrations and seed data before the app starts
This pre-deploy command, for the Django example in
Write the Compose File,
builds the app image, starts the database it depends on, creates the tables, and loads seed
data, all in a one-off app container that is removed afterwards:
docker compose run --rm --build app sh -c "python manage.py migrate --noinput && python manage.py loaddata seed.json"
--rmremoves the one-off container when the command ends.--buildbuilds the image from your repository first, so the migrations are the ones in the branch being previewed.- Docker Compose starts the services
appdepends on first. Withcondition: service_healthyon the database, as in the example, the migrations wait until the database accepts connections.
A rebuild destroys the preview's virtual machine and its volumes, so the database starts empty and every command runs again. Seed data can go in the pre-deploy command, as here, or in the first-deploy command; either way it comes back after every rebuild.
Example: a start command of your own
Leave the start command empty unless the default does not fit. For example, to start only the
services in a Compose profile named preview (a profile is a named group of
services):
docker compose --profile preview up --build --detach --wait
Keep --detach so the command returns, and --wait so it returns only
once the services are running (and healthy, for those with a health check). A start command
that never returns fails the preview when the time limit runs out.
Example: setup once the stack is running
The first-deploy command runs in the running stack, so it can use docker compose exec
against the web service. This loads demo data after the app has started, on every new preview
and every rebuild:
docker compose exec -T app python manage.py loaddata demo.json
-T tells Docker Compose that no keyboard is attached, which is the case in a preview.
The order
On a new preview
- Helm starts the preview's virtual machine and copies your branch from GitHub into it.
- Helm checks your Compose file and refuses settings that previews never allow; see "Settings Helm refuses, and why" in Write the Compose File.
- Helm runs the pre-deploy command.
- Helm runs the start command.
- Helm waits for the web service to answer
GET /on the App Port. - Helm runs the first-deploy command.
- Helm publishes the preview's address.
On a rebuild
A rebuild destroys the preview's virtual machine and its volumes and starts a new one, with the project's current settings and an empty database. It runs every step above again, including the first-deploy command. The address answers with an error until the new version is ready.
Time limits and failures
- The pre-deploy command has no time limit of its own: it counts toward the start timeout below. The first-deploy command may run for 15 minutes after the web service answers, on top of the start timeout.
- Building the images, the pre-deploy command and the start command must together finish, and the web service must answer, within the project's Start timeout (minutes): 20 minutes unless you change it. Set it from 5 to 60 minutes on the Previews settings tab, or for one preview in the Create preview card. A preview that runs out of time fails with a reason such as
build_failed: stopped after 20 minutes at the start stage, the project's start timeout; raise it under Settings, Previews. - A command that exits with a code other than
0, or runs too long, fails the preview. The reason on the PR Previews page and in the status request'serror_messagenames the command, for examplepredeploy_failed: exit code 1, and ends with the last lines the command printed. - A rebuild that fails leaves nothing serving, because the old virtual machine has already been replaced. Fix the reason and click Rebuild on the failed preview to start it again as a new preview.
- Helm keeps each command's output, and the output of
docker compose logs, when a preview fails. Contact Helm support with the preview's address if you need them.
Troubleshooting
The preview fails with predeploy_failed
The pre-deploy command exited with an error. Read the last lines in the reason; a failed migration is the most common cause. Run the same command on your computer, in the folder of your Compose file, against an empty database:
docker compose down --volumes
docker compose run --rm --build app sh -c "python manage.py migrate --noinput && python manage.py loaddata seed.json"
The pre-deploy command cannot reach the database
The one-off container started before the database was ready. Give the database a
healthcheck: and give the app depends_on: with
condition: service_healthy, as in the Compose file example.
The preview fails with first_deploy_failed
The first-deploy command exited with an error or ran longer than 15 minutes. Use
docker compose exec -T rather than docker compose exec, and read the
last lines in the reason.
The preview does not start in time
The start command did not return, or the services were not running within the start timeout.
Keep --detach and --wait in a start command of your own, and check
that every health check in your Compose file can pass. If your images simply take a long time
to build, raise Start timeout (minutes) in Settings, Previews.
My seed data is gone after a rebuild
A rebuild starts with an empty database. Load the seed data in the pre-deploy command, as in the first example, or in the first-deploy command; both run on every rebuild. Data testers created by hand is not kept.
"Pre-deploy Command is N bytes long; the limit is 4096 bytes."
Put the steps in a script in your repository and set the command to run it, for example
sh scripts/predeploy.sh.