---
title: "Preview environments: a live VM for every pull request"
description: "Give every GitHub pull request its own exe.dev VM: your app and a seeded Postgres, the link posted on the PR, updated on push, deleted on close."
url: https://exe.dev/deploy/preview-environments
category: "Developer tools"
published: 2026-11-25
updated: 2026-11-25
---

# Preview environments: a live VM for every pull request

A private VM per pull request, updated on every push, deleted on close.

- **Software:** Preview environments (https://pypi.org/project/exedev/)
- **Source:** https://github.com/boldsoftware/exe-sdk-python
- **License:** MIT (the exedev SDK)
- **Time:** 15 minutes
- **Machine-readable:** https://exe.dev/deploy/preview-environments/manifest.json
- **File:** https://exe.dev/deploy/preview-environments/preview-deploy.sh
- **File:** https://exe.dev/deploy/preview-environments/preview.py
- **File:** https://exe.dev/deploy/preview-environments/preview.yml
- **File:** https://exe.dev/deploy/preview-environments/smoke.py

## What you get

- A VM per pull request, copied from a warm base in seconds, with the PR's commit running on port 8000
- One comment on the pull request with the preview's link, edited on every push
- A database per preview, seeded from a fixture, migrated on every push
- Previews private behind exe.dev login, shared with your team or reviewers
- Previews deleted when the pull request closes, and a nightly sweep for the rest

Running on the VM:

- **Workflow**: .github/workflows/preview.yml on GitHub's runners, with EXE_TOKEN as its only secret
- **preview.py**: a short Python script on the exedev SDK that creates, updates, comments, and deletes
- **Base VM**: acme-notes-base, your app, PostgreSQL, and seeded data, refreshed on every push to main, port 8000, data in `~/app, PostgreSQL's database app`
- **Preview VMs**: acme-notes-pr-7 for pull request 7, a copy of the base, the app as a systemd service, port 8000, data in `~/app, its own copy of the database`

How it is reached:

- https://acme-notes-pr-7.exe.xyz/ (private): pull request 7's preview, for you, whoever you share it with, and checks holding a VM token
- https://acme-notes-base.exe.xyz/ (private): the base VM previews are copied from

Every pull request gets its own exe.dev VM. A GitHub Actions workflow
copies a prepared **base VM**, uploads the pull request's commit, runs
your app with its own PostgreSQL database, and comments the link on the
pull request. Pushes update the same VM and comment; closing the pull
request deletes the VM.

A preview is a whole Linux machine, so it runs what production runs.
It's private behind exe.dev login, and you can `ssh` into it when
something's wrong.

The parts: a workflow, [preview.py](https://exe.dev/deploy/preview-environments/preview.py)
(150 lines on the [exedev SDK](/deploy/python-sdk)), and a deploy hook
for your app. The only secret is an exe.dev token limited to preview VMs.

## How it works

- **acme-notes-base** has your app, PostgreSQL, seeded data, and warm
  caches. Every push to `main` refreshes it.
- **acme-notes-pr-7** is pull request 7's preview. `preview.py` copies the
  base (about 2 seconds), uploads the commit to `~/app`, runs the deploy
  hook, waits for port 8000, and comments the link.
- A nightly job deletes previews whose pull request is closed or idle
  for a week.

## Make the token

```bash
ssh exe.dev ssh-key generate-api-key --label=previews-acme-notes --tag=preview --exp=90d \
  '--cmds="new,cp,ls,ssh,rm,share add,share set-public,ssh-key generate-api-key,ssh-key remove"'
gh secret set EXE_TOKEN   # paste the exe1.… token
```

`--tag=preview` tags every VM the token makes and hides all others from
it. The `ssh-key` commands are only for the
[smoke test](#smoke-test-with-a-vm-token).

## Add the files

Add [preview.py](https://exe.dev/deploy/preview-environments/preview.py),
[preview-deploy.sh](https://exe.dev/deploy/preview-environments/preview-deploy.sh),
and [preview.yml](https://exe.dev/deploy/preview-environments/preview.yml)
to your repository:

```bash
mkdir -p .github/workflows && cd .github
curl -fsSLO https://exe.dev/deploy/preview-environments/preview.py
curl -fsSLO https://exe.dev/deploy/preview-environments/preview-deploy.sh
curl -fsSL -o workflows/preview.yml https://exe.dev/deploy/preview-environments/preview.yml
```

Set `PREVIEW_PREFIX` in `preview.yml`. VM names are global across
exe.dev, so pick something unique, like `acme-notes`.

```yaml
# .github/workflows/preview.yml: a preview VM on exe.dev for every pull request
name: preview
on:
  pull_request:
    types: [opened, reopened, synchronize, closed]
  push:
    branches: [main]          # refresh the base VM that previews are copied from
  schedule:
    - cron: "17 4 * * *"      # delete previews of closed or idle pull requests
  workflow_dispatch:
permissions:
  contents: read
  pull-requests: write
concurrency:
  group: preview-${{ github.event.pull_request.number || github.event_name }}
  cancel-in-progress: ${{ github.event.action == 'synchronize' }}
env:
  PREVIEW_PREFIX: acme-notes  # VMs are acme-notes-pr-<n>; VM names are global, so pick your own
  PREVIEW_SHARE: ""           # "team", or reviewers' email addresses, space-separated
  EXE_TOKEN: ${{ secrets.EXE_TOKEN }}
  GITHUB_TOKEN: ${{ github.token }}
jobs:
  preview:
    # A fork's pull request gets no secrets, and its code shouldn't run on your account.
    if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          ref: ${{ github.event.pull_request.head.sha }}
      - uses: astral-sh/setup-uv@v10
      - id: preview
        run: uv run .github/preview.py
      - name: Smoke test through exe.dev's proxy
        if: steps.preview.outputs.url
        run: |
          uvx --from playwright==1.65.2 playwright install --with-deps chromium-headless-shell
          uv run tests/smoke.py
        env:
          PREVIEW_URL: ${{ steps.preview.outputs.url }}
          PREVIEW_VM: ${{ steps.preview.outputs.vm }}
          PREVIEW_SHA: ${{ github.event.pull_request.head.sha }}
  base:
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: astral-sh/setup-uv@v10
      - run: uv run .github/preview.py base
  reap:
    if: github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: astral-sh/setup-uv@v10
      - run: uv run .github/preview.py reap
```

Delete the smoke test step if you don't have `tests/smoke.py`.

## Write the deploy hook

`preview.py` runs `.github/preview-deploy.sh` in `~/app` on the VM after
every upload. It must migrate, install, and (re)start the app on port 8000,
and be safe to run again. The example, for Python and PostgreSQL:

```bash
#!/usr/bin/env bash
# Runs on the preview VM, in ~/app, after every upload. Safe to run again.
# This one is for a Python app with PostgreSQL; change it to fit yours.
set -euo pipefail
export PGOPTIONS=--client-min-messages=warning

# PostgreSQL, the first time. A copy of the base VM has it already.
if ! command -v psql >/dev/null; then
  sudo apt-get update -q
  sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -q postgresql
  sudo -u postgres createuser --superuser "$USER"
fi

# The database: created and seeded once, migrated on every push.
fresh=0
if createdb app 2>/dev/null; then fresh=1; fi
psql -q -v ON_ERROR_STOP=1 -d app -f db/schema.sql
if [ "$fresh" = 1 ]; then
  psql -q -v ON_ERROR_STOP=1 -d app -f db/seed.sql
fi

# Dependencies
uv sync --frozen --quiet

# The app, as a systemd service, so it comes back after a restart
sudo tee /etc/systemd/system/app.service >/dev/null <<EOF
[Unit]
Description=pull request preview
After=postgresql.service

[Service]
User=$USER
WorkingDirectory=$HOME/app
EnvironmentFile=$HOME/preview.env
ExecStart=$HOME/app/.venv/bin/python app.py
Restart=always

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --quiet app
sudo systemctl restart app
```

Swap in your migrations (`alembic upgrade head`, `rails db:prepare`, …),
build, and start command. `~/preview.env` gives the app `PREVIEW_PR`,
`PREVIEW_SHA`, and `PREVIEW_URL`.

## Make the base VM

Once, from a checkout of `main`, with the same token:

```bash
EXE_TOKEN=exe1.… PREVIEW_PREFIX=acme-notes uv run .github/preview.py base
```

Keep the base private: copies inherit its sharing.

## Open a pull request

A comment appears within seconds and becomes the link once the app
answers. Pushes redeploy in about 4 seconds. A failed deploy edits the
comment and links the log. Closing the pull request deletes the VM.

`preview.py` also reads `PREVIEW_SHARE`, `PREVIEW_HEALTH` (the path it
waits for, default `/`), and `PREVIEW_MAX_DAYS` (default 7).

## Sharing

Previews are private to you. Set `PREVIEW_SHARE` in the workflow:

- `team`: your whole [team](/docs/teams/overview). Best for teams.
- `ana@example.com sam@example.com`: these reviewers, by email invite.
- `public`: anyone with the link. Only for public code and data.

Your app sees who is looking in the `X-ExeDev-Email` header.

## Smoke test with a VM token

Tests reach a private preview with a [VM token](/docs/https-tokens-for-vms)
in the `X-Exedev-Authorization` header.
[smoke.py](https://exe.dev/deploy/preview-environments/smoke.py) mints a
one-hour token and opens the preview with
[Playwright](https://playwright.dev/python/):

```python
token = exe.ssh_key.generate_api_key(vm=vm, label=label, exp="1h")["token"]
page = browser.new_page(extra_http_headers={"X-Exedev-Authorization": f"Bearer {token}"})
page.goto(url)
expect(page.get_by_text("Welcome")).to_be_visible()  # a row from db/seed.sql
```

## Data and secrets

- **Database:** each preview's is a copy of the base's, migrated on every
  push. For realistic data, load a scrubbed dump into the base. Never
  give previews production data or credentials.
- **API keys:** attach test keys to the tag as an
  [HTTP proxy integration](/docs/integrations-http-proxy), so they never
  sit on a VM:
  `ssh exe.dev integrations add http-proxy --name stripe-test --target https://api.stripe.com --bearer sk_test_… --attach tag:preview`

## Forks and security

- Anyone who can push a branch can change `preview.py` in their pull
  request and use `EXE_TOKEN`. The tag limits the damage to preview VMs.
- Fork pull requests get no secrets, so the workflow skips them. Don't
  switch to `pull_request_target` to run fork code: that hands out your
  token.
- Dependabot needs `EXE_TOKEN` under Dependabot secrets, or add
  `&& github.actor != 'dependabot[bot]'` to the job's `if:`.

## What we tested

November 25, 2026, against exe.dev with exedev 0.7.0, PostgreSQL 16, and
Playwright 1.65.2: create, push, failed deploy, restart, close, sweep,
sharing, and the VM-token smoke test. GitHub's API was a local mock; not
yet run on GitHub's runners.

## Troubleshooting

- **Deploy failed:** the comment links the log. On the VM:
  `ssh acme-notes-pr-7.exe.xyz journalctl -u app -n 50`.
- **Name taken:** VM names are global; change `PREVIEW_PREFIX`.
- **`NotFoundError`:** the VM isn't tagged `preview`, or the token lacks
  a command.
- **Not port 8000:** `ssh exe.dev share port acme-notes-base 3000`;
  copies inherit it.

## Frequently asked questions

### How is this different from Vercel or Render previews?

Those build your app from their presets. Here each preview is a whole Linux VM running whatever your deploy hook says: any language, PostgreSQL, workers, Docker Compose. It's private by default, and you can `ssh` in to debug it.

### Does every preview get its own database?

Yes. Each preview is a copy of the base VM's disk, database included, so pull requests can't touch each other's data.

### Does it work with Docker Compose?

Yes: have the deploy hook run `docker compose up -d --build --wait`, and pull or build the images on the base VM so previews start with them cached.

### What about pull requests from forks?

They get no preview: GitHub gives fork pull requests no secrets, and the workflow skips them. See [Forks and security](#forks-and-security).


## Related

- [exe.dev Python SDK](https://exe.dev/deploy/python-sdk.md): Everything ssh exe.dev does, from Python code, sync or asyncio.
- [exe.dev Rust SDK](https://exe.dev/deploy/rust-sdk.md): Everything ssh exe.dev does, from Rust, with Tokio or blocking calls.
- [Run GitHub Actions jobs on exe.dev, a fresh VM per job](https://exe.dev/deploy/github-actions.md): Every job on a fresh copy of a warm VM, billed by the second.
- [Run CI on exe.dev, a fresh VM for every job](https://exe.dev/deploy/ci.md): A warm base VM, a fresh copy per job, deleted when the job ends.
- [Sandboxes on exe.dev](https://exe.dev/deploy/sandbox.md): A VM with its own CPU and memory, billed by the second while it runs.
- [Self-host PostgreSQL](https://exe.dev/deploy/postgres.md): Your own Postgres on a real disk, with backups you've actually restored.
