Skip to Content

Preview environments

Every pull request gets its own temporary copy of your app, with its own address, rebuilt on every push and removed when the pull request is closed.

A preview environment is a temporary environment for one pull request. It's named pr-<number> and copied from the environment that deploys the pull request's target branch. You can try the change at its own address before you merge it.

This guide uses a project whose Dev environment deploys a repository's main branch. A pull request into main changes the page's heading.

Previews need the StackBlaze GitHub App, because it's how StackBlaze sees pull requests. Connect it from an app's Build settings.

Turn on previews for the project

Open Project settings → General. In Preview environments, previews start turned off.

Preview environments with Create previews for pull requests switched off
Preview environments with Create previews for pull requests switched off

Turn on Create previews for pull requests and click Save changes. The card shows Saved.

Preview environments switched on: Every pull request, Bot pull requests off, Delete after 7 days without a push, Data starts empty
Preview environments switched on: Every pull request, Bot pull requests off, Delete after 7 days without a push, Data starts empty

The defaults are:

  • Pull requests: Every pull request. Skip one with the no-preview label or [skip preview] in its title.
  • Bot pull requests: off.
  • Delete after: 7 days without a push.

Every option, including Only when asked for, seed commands and variable rules, is described in Project settings → Preview environments.

The project here has only Dev, Stage and Production: previews don't need an environment of their own. Each one is made when its pull request is opened.

Open a pull request

Push a branch and open a pull request into the branch your environment deploys, here main.

Within about half a minute, the preview is in the environment menu, under Previews. It shows the pull request number, its title, and how long ago it was made.

The environment menu: Dev, Stage, Production, and under Previews #2 docs: preview environment test, just now
The environment menu: Dev, Stage, Production, and under Previews #2 docs: preview environment test, just now

Open the preview

Click the preview in the menu. The header shows PR #2 with a Preview tag, and a bar at the bottom of the canvas reads Preview of PR #2 · from Dev · expires in 7 days · not backed up.

The canvas has a copy of the app from Dev, building the pull request's branch. The copy has its own address, different from the app in Dev.

The PR #2 preview: react-vite-starter running at its own address, with the preview bar at the bottom
The PR #2 preview: react-vite-starter running at its own address, with the preview bar at the bottom

In this example the build took about two minutes. Then the preview's address served the new heading, and the app in Dev still served the old one.

Check the pull request

StackBlaze posts one comment on the pull request, titled StackBlaze Preview, and keeps it up to date. It has a table with a row for each app: App, Status, Preview (a Visit link to the address) and Updated (UTC). Below it, a link opens the preview in StackBlaze.

The Status goes from 🔄 Building to ✅ Ready.

Each commit also gets a check named Stackblaze / and the build type, for example Stackblaze / railpack.

Push more commits

Push another commit to the pull request's branch. The preview rebuilds: the app shows Building, and then the new commit is live at the same address. Here it took just over a minute.

The PR #2 preview rebuilding after a second push: the app shows Building
The PR #2 preview rebuilding after a second push: the app shows Building

Every push restarts the expiry, so the bar shows expires in 7 days again. The comment's row goes back to ✅ Ready with a new time.

Close the pull request

When you merge or close the pull request, its preview is deleted. Here it was gone 17 seconds after the pull request was closed. The preview's address stopped answering.

The preview is no longer in the environment menu.

The environment menu after the pull request was closed: Dev, Stage and Production, no Previews
The environment menu after the pull request was closed: Dev, Stage and Production, no Previews

The comment's rows change to 🗑️ Removed, and the comment ends with "Preview removed."

If you reload a page of the deleted preview, it opens the Dev environment instead.

Loading the deleted preview's page opens Dev
Loading the deleted preview's page opens Dev

The preview bar

Inside a preview, the bar at the bottom of the canvas shows which pull request it's for, the environment it was copied from, when it expires, and that previews aren't backed up. It has three buttons.

Preview of PR #2 · from Dev · expires in 7 days · not backed up, with Sync from Dev, Open PR and Delete
Preview of PR #2 · from Dev · expires in 7 days · not backed up, with Sync from Dev, Open PR and Delete
  • Sync from Dev (named after the environment the preview came from) brings the preview up to date with it. See below.
  • Open PR ↗ opens the pull request.
  • Delete deletes the preview.

Sync from the base environment

Click Sync from Dev. The dialog reads "Bring this preview up to date with Dev." Choose what to copy:

  • Configuration (ticked): "Variables, settings and apps; the preview's data stays." The dialog compares the preview with Dev; here it says "The configuration already matches Dev." The button is Sync configuration.
  • Data: databases and volumes.
Sync from Dev with Configuration ticked: the configuration already matches Dev, Cancel and Sync configuration
Sync from Dev with Configuration ticked: the configuration already matches Dev, Cancel and Sync configuration

With Data ticked too, the dialog checks what can be copied; here it says "Dev has no databases or volumes to copy." It warns: "This preview's data will be replaced with a copy of Dev. Apps in the preview stop while it copies." The button becomes Sync and replace data.

Sync from Dev with Configuration and Data ticked: the replace-data warning and Sync and replace data
Sync from Dev with Configuration and Data ticked: the replace-data warning and Sync and replace data

Cancel closes the dialog without syncing.

Delete a preview

Click Delete. The dialog asks "Delete the preview of PR #2?" and warns: "Deletes this preview's apps, databases and volumes. The pull request is not changed; a new push to it creates the preview again." Type the pull request number (Type 2 to confirm) and click Delete preview. Cancel keeps the preview. In this guide the preview was removed by closing the pull request instead.

Delete the preview of PR #2? with Type 2 to confirm, Cancel and Delete preview
Delete the preview of PR #2? with Type 2 to confirm, Cancel and Delete preview

Notifications

The bell in the header gets an entry when a preview is ready, when it's updated by a push, and when it's removed. For example: Preview pr-2 ready · 1 app, Preview pr-2 updated: react-vite-starter-4l47 ready and Preview environment pr-2 removed (PR closed).

Notifications: Preview environment pr-2 removed (PR closed), Preview pr-2 updated, Preview pr-2 ready · 1 app
Notifications: Preview environment pr-2 removed (PR closed), Preview pr-2 updated, Preview pr-2 ready · 1 app

How a preview is made

Which environment it copies

The preview copies the environment whose services build from the pull request's repository and deploy its target branch. If several environments match, Production wins. If none deploys that branch, but some environment has a service on the repository, Production is copied when there is one.

What gets built

  • Services that build from the pull request's repository follow its branch: they're built from the pull request and rebuilt on every push to it.
  • A service whose watched files the pull request doesn't touch (for example a monorepo service whose Build path the pull request leaves alone) starts on the image it already runs, instead of building. Later pushes that do touch it rebuild it.
  • Services that run an image, and services from other repositories, are plain copies.

Variables a preview adds

Each service copied into a preview gets these variables, so your code can tell it's running in one (for example to skip sending real email):

VariableValue
STACKBLAZE_PREVIEWtrue
STACKBLAZE_PR_NUMBERThe pull request number, such as 2
STACKBLAZE_PR_BRANCHThe pull request's branch
STACKBLAZE_PREVIEW_SOURCEThe service it was copied from, as project/environment/service

Every other variable is copied as it is, unless a variable rule in Project settings says otherwise.

Pull requests that get no preview

  • Pull requests from a fork never get one: their code would run with your project's variables and databases.
  • Pull requests opened by a bot, unless Bot pull requests is on.
  • Pull requests with the no-preview label or [skip preview] in the title (or, with Only when asked for, without the preview label or [preview]).

Plans

Preview environments are part of the Pro and Business plans. See Billing.

Promote a preview

When the change looks right, you can put it live from the preview itself: on the service's Deployment tab, the release's ⋯ menu has Promote. It rebuilds the service the preview was copied from, in its own environment, from the preview's commit. See Deployments.

Good to know

  • Previews are never backed up. Back up the environment they're copied from instead.
  • A preview's data starts empty. Use Sync from Dev with Data to copy it.

On this page