# Pre-deploy and health checks

A build produces an image. Two settings decide what happens between a finished build and a version that takes traffic: the **pre-deploy command** runs first, and the **health check** decides when the new version counts as ready.

```text
build succeeds -> pre-deploy command -> new version starts -> health check passes -> it takes over
```

If either step fails, the version that was running keeps running.

## Pre-deploy command

The pre-deploy command runs **once per deploy, before the new version starts**. The usual use is a database migration.

```toml
[deploy]
pre_deploy = "node dist/migrate.js"
pre_deploy_timeout = "10m"
```

In the console it is **Pre-deploy command** in **Build settings**. See [Build settings](/git-builds/build-settings).

### What it runs in

Zaroz starts a one-off job from the **new image**. It gets:

- the same **environment variables and secrets** as the running service, so it can reach your database;
- the service's **private network**, so private addresses work;
- the same CPU and memory as the service's plan.

It does **not** get the service's volumes. A volume is attached to the version that is running, and that is the one being replaced. A command that needs files from a volume cannot run here.

The command runs in `/bin/sh`, so `npm run migrate && node seed.js` works. The image needs a shell, and an image without one fails the command. The build-time [build secrets](/git-builds/build-secrets) are not available: the command sees the runtime environment, not the build's.

### Limits and logs

- The default limit is **10 minutes**. You can raise it to **30 minutes** with `pre_deploy_timeout`.
- The command's output goes to the deploy's logs, where you can read it from the build and deployment pages.
- It is part of the plan: it adds **no build minutes** and no extra charge. It does run once for every deploy, so keep it short.
- For a service with several units, it still runs **once per deploy**, not once per unit.

### When it fails

If the command exits with an error, or is still running at the limit, the deploy stops with [`pre_deploy_failed`](/git-builds/troubleshooting#pre_deploy_failed). **The service keeps running the old version.** Zaroz does not offer to deploy anyway, because a half-applied migration can leave the new code on an old database.

Fix the command, or the migration it runs, and push again. Because a pre-deploy command may run again after a failure, **write it so that running it twice is safe**: a migration tool that records what it has applied is fine; a script that inserts rows without checking is not.

:::warning{title="Plan migrations for two versions"}
While the new version starts, the old version is still serving. A migration that removes or renames a column the old code uses breaks the old version before the new one is ready. Make schema changes in two steps: first add, deploy, then remove in a later deploy.
:::

## Health check

By default Zaroz considers the new version ready when it accepts a **TCP connection on the app's port**. That only shows that something listens. To check that the app really works, set an HTTP path:

```toml
[deploy]
health_check = { path = "/healthz", timeout = "5m" }
```

| Key | What it does | Default |
|---|---|---|
| `path` | The HTTP path Zaroz requests on the app's port. A success answer means ready. | none: a TCP check |
| `timeout` | How long Zaroz waits for the new version to become ready. | `"5m"` |

- Choose a path that is cheap and does not need a login. It should answer only when the app can serve: for example when it has connected to its database.
- The port is the service's **Port** (`$PORT`, 8080 by default). Your app has to listen on all addresses, not only on `localhost`. See [Languages](/git-builds/languages#the-port-your-app-must-listen-on-port).
- The wait covers everything from the start of the new version to its first ready answer. An app that needs a long time to warm up needs a longer `timeout`.

### When it fails

If the new version is not ready before the timeout, the deploy stops with [`health_check_failed`](/git-builds/troubleshooting#health_check_failed). The version that was running **keeps serving**, because the new one only takes over when it is ready. Read the service logs to see why it did not start.

## How a deploy rolls out

After the checks, the new version replaces the old according to the service's rollout rule:

| Rule | What happens |
|---|---|
| `rolling` | Units are replaced in batches, new first, so capacity does not dip. |
| `replace_idle` | Idle units are replaced now. Busy or allocated ones are replaced when they end. |
| `next_start` | Running units are left alone. The change applies on each unit's next start. |

The build and deployment pages say which rule applies and how many units still run the previous build. A `next_start` deploy shows as **deployed, applies as units start**, and never as done while some unit still runs the old image. The rules are explained in [Rollout rules](/units-code/rollouts).

## Related

- [Rollbacks and redeploys](/git-builds/rollbacks): put an older build back.
- [Troubleshooting](/git-builds/troubleshooting): one section for each failure.
