# Use preview environments

Give every pull request its own copy of the app, at its own address, torn down when it closes.

With preview environments on, a pull request into the app's branch gets a copy of the app built
from the pull request's branch, on one small machine of its own, at
the app's default address with the branch in front, `https://<branch>.<app>.<organization>.apps.…`. Every push to the branch redeploys it. It is
deleted, with its machine, when the pull request merges or closes, or when nobody has pushed to the
branch for the idle days you set. Replace app-id with the app's id.

What a preview gets:

| Part        | From                                                                                                                      |
| ----------- | ------------------------------------------------------------------------------------------------------------------------- |
| Code        | the pull request's branch, built like the app                                                                             |
| Machine     | one machine of the region's default type (the cheapest with 2 GiB), or the type set for previews                          |
| Environment | the app's plain variables, then your overrides; of its secrets only the ones you name                                     |
| Database    | with **A database branch for each preview**: a copy of the app's bound Postgres database, made in seconds; otherwise none |
| Address     | `<branch>.<app>.<organization>.apps.…`, its own certificate                                                               |

A branch name becomes one label: `feature/Login` is `feature-login`; a name longer than 40
characters is cut and ends in a short hash.

On the pull request's commit, the status `ews/preview` says what is happening: building, waiting
for the database branch, ready (with the address) or failed (with the reason). When the preview
first serves, the pull request gets one comment with the address, the machine and what it costs per
hour and per day.

**Important:**
Every preview is a machine, billed as one from the moment it exists until it is deleted. The
**Previews** tab shows each preview's hourly price and what it has cost so far, and the total of all
open previews.

## Prerequisites

- An app built from a repository in Deutsche Cloud Git, on machines of its own.
- Permission for `deploy:UpdatePreviewSettings` on the app to turn previews on.
- Whoever opens the pull request or pushes needs `deploy:CreatePreview` on the app, not
  `deploy:DeployApp`: a contributor can have previews without deploying production. Without it, the
  commit status says "Not allowed to have previews of this app" and nothing is bought.
- For a database branch: the app bound to a Postgres database with at least one completed backup.
- In the console, also `deploy:DescribeApp` to open the app's page (without it the page is not
  found).

## Turn previews on

**Console**
1. Open the app's **Previews** tab.
2. Under **Make a preview for**, choose **Every pull request** or **Every pushed branch**.
3. Set **Delete when idle for (days)** and **Previews at once**. Under the second, the console says
   what that many open previews cost at most.
4. Under **Environment overrides**, add `KEY=value` lines the previews need instead of the app's
   values.
5. Under **Secrets previews may use**, tick the app's secret variables a preview may have. Every
   other secret is withheld.
6. Select **A database branch for each preview** if the previews need data.
7. Choose **Turn on**.

**CLI**
```sh
dcloud deploy previews configure app-id --enabled \
  --env-overrides '{"NODE_ENV":"preview"}' --database-branch --idle-days 3
dcloud deploy previews list app-id
```

**API**
```http
PUT /deploy/api/apps/3f1c2b4a-5d6e-4f70-8a91-b2c3d4e5f601/previews
{
  "enabled": true,
  "trigger": "pull_request",
  "envOverrides": { "NODE_ENV": "preview" },
  "sharedSecretKeys": [],
  "databaseBranch": true,
  "idleDays": 3,
  "maxPreviews": 10
}
```

Turning previews on for a pooled app, an app without a repository, or a preview itself is refused
with `409`. Turned off, the open previews are deleted within the hour.

## Work with a preview

- **Open it**: the address is in the pull request's comment, the commit status and the **Previews**
  tab.
- **Keep it**: in the preview's row menu on the **Previews** tab, choose **Keep while idle**, or
  `dcloud deploy previews keep app-id <preview-id> --keep`. A kept preview is not deleted
  for being idle; it still goes with its pull request.
- **Delete it now**: choose **Delete**, or `dcloud deploy previews delete app-id
<preview-id>`.
- **Make one by hand**: `dcloud deploy previews create app-id --branch <branch>`, for a branch
  that has no pull request yet.

When the app already has as many previews as **Previews at once**, the next pull request's commit
status is "Preview limit reached" and nothing is bought: close a pull request or delete a preview.

## Result

Each preview is an app of its own: its page shows its deployments, build log, logs and metrics,
and its machine appears under **Compute** with the app's tags. Its activity and the app's say every
preview made, deployed, refused or deleted, and why.

## Related

- [Turn on deploy on push](turn-on-deploy-on-push.md)
- [Bind a database](bind-a-database.md)
- `deploy.previews.configure` · `dcloud deploy previews configure`
