> Index of every page on this site: [llms.txt](https://docs.a-line.cloud/llms.txt)

# Preview pull requests

> Give each GitHub pull request its own running copy of a project, with its own URLs, access rules, lifetime and build budget.

**Goal**: every pull request of a project's repository gets a preview: a
copy of the project built from the pull request, on its own URLs, posted
back to the pull request as one comment that A-Line.cloud keeps up to date.

## Before you start

- A project [linked to a GitHub repository](/guides/iac-project-link) (Git
  sync). Previews follow the pull requests of that repository.
- The **Admin** role on the project's environment to change preview
  settings. Organization admins choose where preview names live and approve
  pull requests from forks.

A linked project nobody configured previews **every** pull request with the
defaults below. Turn previews off or narrow them in the settings.

## Turn previews on and pick the pull requests

<InterfaceTabs items={['Console', 'IaC']}>
<InterfaceTab value="Console">

<Steps>
<Step>

Open the project on the canvas, open **Project settings** and go to the
**Previews** tab.

</Step>
<Step>

Under **Pull request previews**, switch **Preview pull requests** on.

</Step>
<Step>

Under **Which pull requests**, pick one:

- **Every PR**: every pull request that is not a draft.
- **With a label**: only pull requests carrying the **Label** you set
  (default `preview`).
- **Into branches**: only pull requests into a matching base branch. Enter
  the **Base branches** as comma-separated globs, such as `main, release/*`.

</Step>
<Step>

Set who can open a preview, what happens with forks, and the limits (see
the sections below), then select **Save**.

</Step>
</Steps>

</InterfaceTab>
<InterfaceTab value="IaC">

<Steps>
<Step>

Add a `preview:` block to the project file:

```yaml title="projects/shop/project.yaml" iac
# yaml-language-server: $schema=https://a-line.cloud/schemas/iac/v1.json
version: 1
name: shop
cluster: hetzner-fsn1
preview:
  trigger: label
  label: preview
  access: link
  forks: approve
  on_draft: sleep
  ttl: 3d
  sleep_after: 30m
  max_active: 5
  quota:
    cpu: "2"
    memory: 4Gi
  build_budget: 20
```

</Step>
<Step>

Commit it. The next sync applies it; the console then shows the settings
as **Set by preview: in project.yaml** and edits go through the file.

</Step>
</Steps>

`trigger` is `every_pr`, `label`, `branches` or `off`. A `preview:` block
without a `trigger` means `off`. Every key is listed under
[`PreviewSpec`](/iac/reference/types#previewspec).

</InterfaceTab>
</InterfaceTabs>

Two labels are reserved on every trigger: `no-preview` keeps a pull request
out, and `preview-approve` approves a fork's commit (see
[forks](#forks)).

## What a pull request does to its preview

| Event | Preview |
| --- | --- |
| Pull request opened or pushed | Builds and deploys the new head commit. The URLs keep serving the previous build until the new one is ready. |
| Draft pull request | Nothing builds until it is ready for review. |
| Turned back into a draft | Sleeps (keeps its URLs), or is destroyed with `on_draft: destroy`. |
| Closed or merged | Destroyed. |
| No push for the **Delete after** time | Destroyed. |
| Idle for the **Sleep after idle** time | Scaled to zero. A new push wakes it. |

The pull request comment shows the state (**Building**, **Ready**,
**Asleep**, **Queued**, **Failed** or **Destroyed**), a table with each
app's URL and the apps left out. A preview also shows on GitHub as a
deployment named `preview/pr-<number>`.

## Which apps are previewed

A preview is a copy of the project named `<project>--pr-<number>`. Its
apps, settings and secrets come from the pull request's **base** branch;
from the pull request itself it takes only the Dockerfile and build context,
and plain environment values. The copy is kept small and safe:

- Sandboxed isolation on the cluster's default pool: never GPU or dedicated
  pools.
- At most one replica, no autoscaling.
- Generated hostnames only: no custom domains.
- No network policies to other projects.
- Only the environment's secret group named `preview`. If the project's apps
  use secret groups and the environment has no `preview` group, the preview
  is refused.

Databases and other data services from templates (Postgres, ClickHouse,
Valkey, NATS) get a fresh, empty instance of their own in each preview
(`preview: {mode: empty}`, the default for them; `clone` means the same). It
runs the same template and version on the smallest size, with a small volume,
no backups, no cron jobs, no public port and no custom domains, and it is
created and deleted with the preview. It has its own credentials: apps of the
preview that use the database are pointed at it, and production data,
credentials and secrets are never copied. It starts empty: fill it with
the [preview steps](#steps) of the apps that use it. The preview lists it as
`<name> (empty database)` (or `(empty Valkey)`, ...), without a URL. Preview
secrets are not billed as secret storage.

### Use production in a preview

Two settings let a preview reach production instead of a copy. Both are
read from the base branch's files only, and both are off for pull requests
from forks (the app is skipped and listed as `` `<name>`: share skipped ``).

- `preview: {mode: share}` on an app with a port: the preview runs no copy
  of it. The preview's apps that read it (`from: app/<name>`) get the
  production app's address, so **previews call the production service**.
  The production app must be deployed on the same cluster as the preview.
- `preview: {data: share}` on a Postgres template: the preview gets no
  database of its own. Its apps that lease that database get a login of
  their own on the **production** database instead: read-only
  (`SELECT` on existing tables, `default_transaction_read_only` on, at most
  5 connections, statements and idle transactions cut off after 60
  seconds), created with the preview and dropped with it. Its
  password is never stored and never reaches production's apps. Previewed
  apps with `migrate` or `seed` steps can't share a database (they would
  write to it); Valkey and other data services can't share.

  A read-only login can still write through a `SECURITY DEFINER` function
  it may execute, so a database where `PUBLIC` can execute one (outside
  `pg_catalog`, `information_schema` and extensions) is not shared: the app
  is skipped and the preview lists the functions (up to 5). Revoke it for
  each, then push again:

  ```sql
  REVOKE EXECUTE ON FUNCTION <schema>.<name> FROM PUBLIC;
  ```

```yaml
# apps/db.yaml
source:
  template: postgres
preview:
  data: share
```

A preview that uses production keeps the project's `access`: `team`, `link`
or `public` is your choice. Its page and pull request comment show
`uses production: <apps>`, and while the access is `link` or `public` they
(and the console's access setting) warn that the preview uses production data
and who can reach it.

Owners and admins can turn this off for the whole organization in its
security policies, **Previews may use production data**: Allowed (default) or
Blocked (2FA confirmation required, audited). While Blocked, a sync with a
`share` fails with "blocked by your organization's preview data policy", and
existing previews are synced again right away and skip their shared apps.

### Migrations and seed data

An app previewed with `clone` can run two commands in each preview, set in
its file's `preview:` block:

```yaml
preview:
  migrate: ["./manage.py", "migrate", "--noinput"]
  seed: ["./manage.py", "loaddata", "fixtures/preview.json"]
```

- Each is a command in exec form: a list of 1 to 32 strings, each at most
  4 KiB. There is no shell: write `["sh", "-c", "..."]` if you need one.
- Only apps with `clone` (the default) take them. A database or data
  service template gets an empty instance, so run its migrations from the
  app that uses it. `skip` and `empty` apps refuse them.
- They are read from the base branch's files only: a pull request can't
  change what runs.
- Each runs as a one-shot job with the preview's build image, environment
  and secrets, on the app's pool and size, without a port. It gets one try
  and at most 15 minutes. Its output is in the app's logs.

The order is: the preview's databases are ready, then every app's `migrate`,
then every app's `seed`, then the apps roll out. While a step runs, the
preview shows as deploying.

- `migrate` runs again for every new build of the pull request's head, so
  your migrations must be **idempotent**: running them twice must be safe.
- `seed` runs once per preview, and again only when the preview's database
  instances are recreated.

When a step fails, the preview fails: its comment names the app and the step
and links the deploy's log. The next push (or a redeploy) tries it again. The
jobs are deleted with the preview, and production never runs either command.

The other stateful apps are not previewed yet. Each app below is listed on the
preview as `<name>: not previewed yet` with the reason:

- Templates that are not a database or data service, and templates seeded
  from elsewhere.
- Apps with a volume or a bucket, or that lease credentials of an app outside
  the preview.
- Workers (apps without a port).
- Apps that read from any of the apps above.
- Cron jobs, redirects, buckets, storages and backups.
- Apps the pull request adds, and apps built from another repository.

### Static sites

Each static site of the project (`sites/<name>.yaml`) is previewed too. It is
built from the pull request's head commit with the pull request's build
settings and build variables, and served on its own preview hostname, such as
`docs--pr-7.<handle>.site.<domain>`, with the same access rules as the
preview's apps. A preview site never gets the site's custom domains, its
scheduled rebuild or more than one server pod. Its files are deleted with the
preview.

These sites are listed under **Not in this preview** instead:

- Sites with `preview: {mode: skip}` in their file.
- Sites whose build reads build secrets: preview builds get none yet.
- Sites built from another repository, uploaded sites, and sites the pull
  request adds.

## Who can open a preview

Set **Who can open a preview** on the **Previews** tab (`access:` in IaC):

| Option | Who gets in |
| --- | --- |
| **Team only** (`team`, default) | Signed-in members of the organization with at least Viewer on the project's environment. Organization admins always get in. |
| **Team and share links** (`link`) | Members, plus anyone holding a signed, expiring share link. |
| **Anyone with the URL** (`public`) | Anyone. Previews are still never indexed by search engines. |

### Share a preview

With **Team and share links**:

<Steps>
<Step>

Open the preview's page from the **What is left out and who can open it**
link in the pull request comment.

</Step>
<Step>

Under **Share links**, give the link a label such as `Design review`, pick
**Expires after** (1 hour, 1 day, 7 days or 30 days) and select **Create
share link**.

</Step>
<Step>

Copy the link right away: it is not shown again. Anyone with it can open the
preview until it expires or you revoke it.

</Step>
</Steps>

## Pull requests from forks

A pull request from a fork comes from outside the repository, so anyone may
have written its code. **Pull requests from forks** decides what happens to
them; pull requests from branches of the repository itself are never
affected:

- **Approve each commit** (`approve`, default): nothing builds until an
  organization admin selects **Approve** next to the pull request in the
  list of previews on the project's **Previews** settings tab (shown as
  **Waiting for approval**), or someone with triage rights on the
  repository adds the `preview-approve` label. A new push needs a new
  approval.
- **No previews** (`off`): pull requests from forks get no preview.

Fork previews get no secrets and no outbound network.

## Where preview names live

Preview hosts are the app's host label with `--pr-<number>` added, under
the A-Line content domain by default (the picker shows your organization's
base, such as `*.t0123456789abcdef.site.example.com`).
An organization admin can move them under one
of the organization's [hostname bases](/guides/hostname-bases) with **Where
pull request preview names live** (`domain:` in IaC).

To put previews on your own domain, add a hostname base under **Domains**
first; it then shows in the picker.

The A-Line content domain keeps preview cookies away from your own domains. A base
that shares its registrable domain with your production names (such as
`previews.example.com` next to `www.example.com`) lets pull request code
read and set production cookies, so picking it needs **I understand
previews can read and set these cookies** (`domain_shared_ok: true`). Open
previews move with a change; the old names keep serving for 7 days.

## Lifetime, size and budget

| Setting | IaC | Range | Default |
| --- | --- | --- | --- |
| **Delete after** | `ttl` | 1h to 30d | 7d |
| **Sleep after idle** | `sleep_after` | 5m to 24h | 30m |
| **Most previews at once** | `max_active` | 1 to 500 | 5; more are queued |
| **CPU per preview** | `quota.cpu` | | 2 |
| **Memory per preview** | `quota.memory` | | 4Gi |
| **Build budget (EUR per month)** | `build_budget` | 1 to 10000, for all previews of the project | 20 |

Previews run on spare room in the project's cluster and never add nodes,
so pick **Most previews at once** to fit your team and cluster.
Their builds are billed like any build. When they reach the monthly budget,
new pushes don't build until the 1st; running previews keep their last
build.

## Verify

Open a pull request. Its comment appears with the state **Building**, then
**Ready** and the URL table. The **Previews** tab
lists the open previews with their pull request number, commit and state.

## Troubleshooting

- **No comment on the pull request**: check the trigger and labels, and
  that the pull request is not a draft. If the **Previews** tab says the
  GitHub App can't write PR comments or Deployments, previews still run;
  re-approve the GitHub App to get them back.
- **Settings are locked**: they are set by `preview:` in `project.yaml`;
  change them there. Without the environment Admin role they are read-only.
- **Waiting for approval**: the pull request comes from a fork; approve it as
  described under [forks](#forks).
- **Queued**: **Most previews at once** are running; the newest queued
  pull request starts when one is removed.
- **An app is missing**: it is listed under **Not in this preview** with the
  reason; see [which apps are previewed](#apps).

## Next steps

- [Use your own domain for generated names](/guides/hostname-bases).
- [Store secrets](/guides/secrets) and add a `preview` secret group.
- [Manage a project from Git](/guides/iac-project-link).
