# Buildstack previews: the previews.yml reference You are helping a developer write `previews.yml`, the file that tells Buildstack how to run a preview of their project. Everything you need is in this document. Write only the keys listed here; an unknown key is an error. ## What a preview is Every task in Buildstack gets its own running copy of the project, built from the task's branch, at its own address behind a sign-in. `previews.yml` says how to install, build, set up and start each part of the app so that copy comes up on its own. ## Where the file goes - Name it `previews.yml` and put it at the root of the repository. - The copy on the default branch is the project's config. While it is there, it wins: Settings shows it read-only. The two are never merged. - With no file in the repository, the same format is edited as text in the project's Settings (Preview environment). - A task's branch may add or change `previews.yml`. That version is used for that task's preview only, until it reaches the default branch. - On a task's branch, four things stay as the project's saved config has them until the file reaches the default branch: which secrets a service is given, `public`, `seed` and `replace_urls`/`media_from`. A branch can't hand a service a new secret or open a preview without sign-in. - If the repository's file has an error, the last config that worked stays in use and Settings shows the error. - A project made of several repositories reads the file from the first one; every `dir` is relative to the folder holding all of them. ## A minimal file ```yaml services: app: install: npm ci dev: npm run dev -- --host 127.0.0.1 --port $PORT health: / ``` ## Top-level keys | Key | Type | Default | Meaning | |---|---|---|---| | `services` | mapping of name → service | required | The parts of the app. At least one must be enabled. | | `primary` | service name | the first enabled service | The service that owns the task's plain address. Must be an enabled service. | Service names: a lowercase letter, then lowercase letters, digits or hyphens, at most 20 characters, never `--`. The primary is served at the task's address; every other service at `--` beside it. ## Service keys | Key | Type | Default | Meaning | |---|---|---|---| | `dir` | path | the repository root | The service's folder, relative to the repository root. Every command runs in it. | | `runtime` | `process` \| `php` \| `static` | `process` | `process`: `dev` starts a server listening on `$PORT`. `php`: the box serves `root` through PHP-FPM, no server of your own. `static`: the box serves the files in `root`. | | `install` | command | none | Installs dependencies. Runs once per preview, and again when the command or a lockfile in the service changes. With no `install`, a `process` service that has a `package.json` and no `node_modules` gets `npm install`. | | `build` | command | none | The asset build. Runs after `install`, and again only when its inputs change. Never given secrets. | | `build_output` | list of paths | none | What `build` writes, relative to `dir` (never `dir` itself). Lets an identical build from another preview be copied in instead of run again. Only with `build`. | | `build_inputs` | list of paths | the folder holding the `package.json` above each output, else the whole service folder | What `build` reads; a change to any of them builds again. Only with `build`. | | `dev` | command | none | The server, for `runtime: process` only. It must listen on `$PORT` on 127.0.0.1. Ignored for `php` and `static`. | | `setup` | command | none | One-time preparation, such as migrations and seed data. Runs once per preview, before `update`, and again when the command (or a secret's value it uses) changes. Keep it safe to run twice. | | `update` | command | none | What must follow the code, such as `php artisan migrate --force`. Runs after `setup` and again whenever the service's folder changed. Migrations, never seed data. | | `teardown` | command | none | Undoes what `setup` made outside the project's folder (another database, a queue). Runs once, when the preview is removed for good, with the same environment as `setup`; two minutes at most. | | `env` | mapping of NAME → value | none | Extra environment for the service. Names are letters, digits and `_`, not starting with a digit. Values may use the variables below. | | `health` | path | none | A path polled until it answers below 400. If it never does within 60 seconds, or answers 5xx three times in a row, the service fails with the reason. | | `database` | `mysql` \| `sqlite` | none | A fresh database for each preview, described by `$DB_*`. | | `root` | path | `.` | `php` and `static` only: the folder served, relative to `dir`. A `static` service with one `build_output` and no `root` is served from that output. | | `spa` | boolean | `false` | `static` only: a path that is not a file is answered with `/index.html`, so an app's own routes open directly. | | `php` | `"8.1"` \| `"8.2"` \| `"8.3"` \| `"8.4"` | the box's default | The PHP version on the PATH for this service's commands, and for `runtime: php` the version that serves it. Quote it. | | `node` | `"18"` \| `"20"` \| `"22"` \| `"24"` | the box's default | The Node version on the PATH for this service's commands. | | `needs` | list of service names | none | Services started first whenever this one starts. Enabled services of this file only, never itself, no loops. | | `on_demand` | boolean | `false` | Start this service only when a task needs it: the task's branch changed its folder, or it was started for that task. Otherwise every task starts it. A file with one enabled service always starts it. | | `disabled` | boolean | `false` | Keep the service in the file but never start it: no address, no variables. | | `public` | boolean | `false` | Serve this service without the sign-in. For an API that a front end calls from the browser, which sends no sign-in to another address. | | `seed` | `dump` | none | `php` with `database: mysql` only: import the project's uploaded, cleaned database copy (Settings → Preview environment → Preview data) before `setup`, while the preview's database is still empty. For WordPress. | | `replace_urls` | list of URLs | none | With `seed: dump`: after `setup`, each of these live addresses is replaced with the preview's `$URL` across the database. Up to 10, `http(s)://host[/path]`, no query. | | `media_from` | URL | none | `php` only: an origin (`https://example.com`, no path) that uploaded media missing from the preview is fetched from, once, and served from the preview's own address. | | `secrets` | list of secret names | none | Only for a script that reads a secret from its environment without the file naming it. Normally leave it out (see Secrets). | ## Variables filled in per preview Write them as `$NAME` in any command or `env` value. They are replaced in the text before the command runs, quotes included. | Variable | Value | |---|---| | `$PORT` | The port this service listens on. | | `$HOST` | This service's public host name. | | `$URL` | This service's public address, `https://$HOST`. | | `$PORT_`, `$HOST_`, `$URL_` | The same for another service in the file, by its name (`$URL_api`). Set even when that service isn't started on this task. | | `$DB_NAME`, `$DB_USER`, `$DB_PASS`, `$DB_HOST`, `$DB_PORT`, `$DB_URL` | With `database: mysql`. `$DB_URL` is `mysql://user:pass@host:port/name`. | | `$DB_PREFIX` | With `database: mysql`: the imported copy's table prefix, else `wp_`. | | `$DB_FILE`, `$DB_URL` | With `database: sqlite`: the database file, and `sqlite:`. | | `$REDIS_HOST`, `$REDIS_PORT`, `$REDIS_PASSWORD`, `$REDIS_DB`, `$REDIS_CACHE_DB`, `$REDIS_URL` | A pair of empty Redis databases of the preview's own, given to a service that names any of these. | | `$TASK` | The task's label as in its address, such as `cel-143`. | | `$PROJECT` | The project's short name, as in its addresses. | | `$PREVIEW_TOKEN` | A random value per preview, the same in every service of it: a shared secret between two of them. | ## The order steps run in For each service: `install` → `build` → `seed` → `setup` → `update` → the address is opened → `health`. Services named in `needs` go first. Each later turn on the task brings the preview up to date: install again if a lockfile changed, build again if the build's inputs did, `update` if the code did. Every step skips itself otherwise. A `build` that names `$URL_` of a service it `needs`, where that service has a `database`, is treated as reading its content (a static site built from a CMS): the box waits for that service to be up first, and builds again when its data or code changes. ## Secrets Values a preview needs that must not be in the repository (a test API key) are the project's preview secrets, entered once in Settings and never shown again. The file names one as `$NAME` or `${NAME}`: - in `setup`, `update`, `teardown` or `dev`. The secret is in that command's environment under its own name, so a script can also read it directly. - never in an `env` value: those are written where a secret must not be, so that is an error. Pass it in the command instead (`STRIPE_KEY=$STRIPE_TEST_KEY npm run start`). - never in `install` or `build`: build output is served, so that is an error. - a name that is neither a variable above nor a secret the project has yet is a warning, not an error, so the file can be committed before the value is entered. Use preview-only keys. A preview runs the branch's own code, so whatever it is given, that code can read. ## Rules for a valid file 1. YAML, at the repository root, named `previews.yml`, with `services` at the top. 2. Only the keys listed above. An unknown key is an error naming it and its line. 3. Service names: lowercase letter first, then lowercase letters, digits, hyphens; at most 20 characters; no `--`. 4. At least one service not `disabled`. `primary`, if given, names one of them. 5. `needs` names other enabled services in the same file, without a loop. 6. Paths (`dir`, `root`, `build_output`, `build_inputs`) are relative: no leading `/` or `~`, no `..`. 7. `dev` only on `runtime: process`; `root` only on `php` or `static`; `spa` only on `static`; `seed`, `replace_urls` and `media_from` only on `php`, and `seed` only with `database: mysql`. 8. `php` and `node` are values from the lists above, quoted (`"8.4"`). 9. Secrets never in `install`, `build` or an `env` value. 10. Everything a `process` service serves goes through `$PORT`; never hard-code a port or address, use the variables. ## Worked examples ### A Laravel API with a Vite front end ```yaml primary: web services: api: dir: api runtime: php php: "8.4" root: public database: mysql install: composer install setup: cp -n .env.example .env && php artisan key:generate && php artisan migrate --seed update: php artisan migrate --force health: /up public: true env: APP_URL: $URL DB_CONNECTION: mysql DB_HOST: $DB_HOST DB_PORT: $DB_PORT DB_DATABASE: $DB_NAME DB_USERNAME: $DB_USER DB_PASSWORD: $DB_PASS web: dir: front-end runtime: static node: "22" install: npm ci build: VITE_API_URL=$URL_api npm run build build_output: [dist] spa: true needs: [api] ``` The API is `public` because the front end calls it from the browser at another address. ### A Node app ```yaml services: app: node: "22" install: npm ci dev: STRIPE_KEY=$STRIPE_TEST_KEY npm run start -- --port $PORT health: / database: sqlite setup: npm run db:migrate update: npm run db:migrate env: DATABASE_URL: $DB_URL PUBLIC_URL: $URL ``` `STRIPE_TEST_KEY` is a preview secret entered in Settings, named in the `dev` command (never under `env`); `DATABASE_URL` and `PUBLIC_URL` are filled in per preview. ### A static site ```yaml services: site: runtime: static install: npm ci build: npm run build build_output: [public] ``` A site with no build step: `runtime: static` and `root` pointing at the folder with `index.html`. ### WordPress ```yaml services: wp: runtime: php php: "8.2" database: mysql seed: dump replace_urls: - https://www.example.com media_from: https://www.example.com setup: >- wp config create --dbname="$DB_NAME" --dbuser="$DB_USER" --dbpass="$DB_PASS" --dbhost="$DB_HOST:$DB_PORT" --dbprefix="$DB_PREFIX" --force health: / ``` Upload a copy of the live database in Settings → Preview environment → Preview data first; it is scrubbed on the box before any preview imports it. A WordPress preview never sends email. ## Testing it The Test preview button in Settings brings the config (the text in the editor, saved or not) up from the default branch at its own preview address, with each service's status and log. It is taken down when stopped, when another test starts, or after 30 minutes. ## Common failures - **`: install failed`** (or build, setup, update): the command exited with an error; the service's log shows its output. Run the same command in a fresh clone. - **Health never answered**: nothing answered on `health` within 60 seconds. A `process` server must listen on `$PORT` on 127.0.0.1. - **Three 5xx answers in a row**: the service started but errors; for `php` the PHP error is shown. - **A static site answers 404**: `root` is not the folder with `index.html`; set `root` or `build_output`. - **A front end can't reach its API** (a sign-in page instead of data): set `public: true` on the API. - **"its build reads , which failed to start"**: fix the service it needs first. - **Unknown key on line N**: a typo or a key this format doesn't have. - **A secret named in install or build**: move it to `setup`, `update` or `dev`. - **"No secret named X yet"**: add it in Settings, or fix the spelling. - **A loop in needs**: two services need each other; one must not. - **A service didn't start on this task**: it is `on_demand`; it starts when the task changes its folder.