# What happens when you deploy

Source: https://docs.apphaven.eu/deployments

You click **Deploy** (or push, with auto-deploy on) and a few minutes later
your app is live. This page walks through what happens in between, what you
see at each step, and, just as important, what happens when a step goes
wrong.

## The steps

### 1. Your manifest is read and validated

AppHaven reads `apphaven.yaml` at the branch's **latest commit** and
[validates it strictly](https://docs.apphaven.eu/manifest.md#validation). A manifest problem (a typo'd
field, an undeclared volume, a dependency cycle) stops the deploy right
here, with a message naming the problem, before anything about your running
app changes.

### 2. Your image is built once per commit

If a service has a [`build` block](https://docs.apphaven.eu/services/container.md#building-from-source-build),
AppHaven checks whether this exact commit has already been built. If it has,
the existing image is reused and the deploy skips straight ahead. Deploying
the same commit again, or promoting a previewed commit to production, doesn't
rebuild it.

If not, a fresh, isolated build environment is created for this one build: it
clones your repository at that commit, runs your Dockerfile, and is destroyed
when it finishes. The build's full output appears live in the **Builds** tab,
success or failure, and stays there afterwards: every build is kept in the
tab's history with its branch, commit, and duration.

The result is one image per commit. The deployment that goes live runs
exactly the image built from the commit it records; nothing can substitute
a different image under the same deploy.

Each build also records what went into the image, and that inventory is checked
against published vulnerabilities as soon as the build finishes. The result
lands in the app's **Security** section; see
[Vulnerability management](https://docs.apphaven.eu/vulnerability-management.md).

### 3. Your environment is provisioned

For a first deploy, AppHaven creates the deployment's environment: its own
[virtual machine](https://docs.apphaven.eu/how-it-works.md) at the
[size you've chosen](https://docs.apphaven.eu/resources.md), its persistent storage, and any
[managed database](https://docs.apphaven.eu/services/postgres.md), initialized with
generated credentials that exist only where the database runs.

### 4. Your services start

Containers start in [dependency order](https://docs.apphaven.eu/services.md#start-order-depends_on):
anything with `depends_on` waits for its target to be started, or healthy if
you asked for that. `${...}` connection references in your `env` resolve
to real values as each service starts, and your console **Variables &
Secrets** are applied on top of the manifest's `env`.

### 5. Your app is live

Each port you declared is served on its own address over HTTPS (one per
[entry point](https://docs.apphaven.eu/manifest.md#ports-and-addresses)), with certificates handled for
you. The deployment card lists each container and
its state, and each container has a **Show logs** action: your app's console
output, no SSH required.

Throughout, the deployment shows where it is:

- **Provisioning**: building or pulling images, starting containers.
- **Running**: your app is up, with its containers and addresses listed.
- **Failed**: something went wrong; the card shows what, with logs.

## Deploying an update

When you deploy a new commit to an already-running deployment, the steps run
in an order designed to protect the running version:

1. **The build happens first.** While your new image builds, the current
   version keeps serving traffic, untouched.
2. **Then changed services are replaced.** Only services whose image or
   configuration actually changed are restarted onto the new version;
   unchanged services keep running as they are.
3. **Your data carries over.** Persistent volumes and databases belong to the
   deployment, not to a release: the new version starts against the same
   data, on the same URL.

## When something goes wrong

### The build fails

The deploy stops at step 2. **Your running version keeps serving traffic**:
a failed build never touches it. The build appears as failed in the Builds
tab with its full log (the error is at the end), and the activity timeline
records it. Fix the code or Dockerfile, push, and deploy again.

### Services fail to start

If your containers can't come up (a crashing process, a bad migration, a
missing variable), the deployment is marked **Failed** and the card shows the
error. AppHaven captures the final console output of the failed containers at
that moment, so you can read exactly what your app printed even after the
containers are gone. Your data is untouched: fix the problem and deploy
again, or deploy a previous commit, and the deployment starts against the
same volumes and database.

A Failed deployment that started on the platform still holds its reserved CPU,
memory and storage, and they count toward your usage until you stop it, deploy
again, or delete it. If the fix will take a while, **Stop** it from its
deployment page: that ends the cost and keeps its data. The one exception is
production waiting for support after a
[restore that did not finish](https://docs.apphaven.eu/restore.md#replacing-production): it is not billed
while it waits.

A first deploy can also be turned away before anything starts, for example
when it does not fit in your [project's quota](https://docs.apphaven.eu/resources.md#project-quotas). Its
deployment page then says it did not start. Nothing runs, nothing is reserved
and nothing is billed for it, so there is nothing to stop. Change what kept it
from starting, such as the app's size, and deploy again, or delete it.

### Your app crashes while running

Crashed containers are **restarted automatically**. A restart loop is visible
on the deployment card and in the container logs, so a recurring crash is
something you find out about, not something that silently takes the app down
for good.

If a service keeps crashing, AppHaven stops restarting it and marks the
deployment **Failed**, with what happened and the service's last output. It
still holds its reserved CPU, memory and storage, and they count toward your
usage until you stop it, deploy again, or delete it.

## Stopping, deleting, and rolling back

**Stop** a deployment and its containers shut down while every byte of
persistent data (volumes and databases) is kept. A stopped deployment also
releases its CPU and memory back to your
[project's quota](https://docs.apphaven.eu/resources.md#project-quotas). Deploy again and it picks up
right where it left off, same data, same URL.

**Deleting** a deployment is the operation that removes its data. Stop is
reversible; delete is not.

**Rolling back** starts from what you know: every deployment records exactly
which commit it shipped, so you always know what is running. To put a
production version that worked back in place, use
[Run an older version](https://docs.apphaven.eu/restore.md#run-an-older-version) in the console: it
deploys the exact build that ran then, with no rebuild, and your data stays as
it is. To make the rollback stick, revert the bad commit on your branch too, so
the next push does not bring it back. For the cases where bad *data* shipped
along with bad code, [go back to before the deploy](https://docs.apphaven.eu/restore.md#go-back-to-before-a-deploy)
instead.

## Where to go next

- [How AppHaven runs your app](https://docs.apphaven.eu/how-it-works.md): the platform in one picture.
- [Preview deployments](https://docs.apphaven.eu/previews.md): a running copy of every branch.
- [Backups & recovery](https://docs.apphaven.eu/backups.md): the restore points kept for your data.
- [Restoring your app](https://docs.apphaven.eu/restore.md): going back to before a deploy, or to an
  older version.
- [Building with AI agents](https://docs.apphaven.eu/ai-agents.md): the preview-first deploy rhythm.
