Skip to Content
LogiStackDocs
Deploy

Preview environments for pull requests

Give every pull request its own live LogiStack environment with its own URL, cloned from staging or production and removed when the pull request closes.

A preview environment is a temporary copy of a project for one pull request. When someone opens a pull request, LogiStack clones an environment you choose into pr-<number>, builds the pull request's head commit, gives each exposed service its own URL and posts the URLs on the pull request. Docker Compose stacks come along too: a stack deployed from Git can read its compose file from the pull request's branch. Closing or merging the pull request removes the copy. Use previews so reviewers can click through a change before it merges.

Before you start

  • Connect the repository through a Git provider that sends pull request events: GitHub, GitLab, Gitea or Bitbucket. See Deploy from Git.
  • Have an environment to clone, usually Staging, with at least one service or Compose stack that comes from that repository. A stack must be deployed from Git to follow pull requests; see Compose stacks.

Turn on previews

Open Preview deployments

Open the project, open the actions menu in the project header and select Preview deployments….

The Preview deployments wizard on its What a preview runs step, with the Clone from field

Choose what a preview runs

On What a preview runs:

  • Clone from: the environment each preview copies. Every preview starts with its services, stacks and variables. Volumes and databases are never shared: each preview gets empty ones.
  • What follows the pull request: tick the Git services that should build the pull request's head commit, for example web and api, and the Git stacks that should read their compose file from the pull request's branch. Everything else runs what the source runs. Pull requests on these repositories open previews.

Every stack of the source environment is copied into the preview as a stack of its own, with new services, new volumes and Docker names derived from their new IDs. A followed stack reads docker-compose.yml from the pull request's branch, so a pull request that adds a service to the compose file previews that service too, and its build: services build the branch. Other stacks run the compose file the source runs.

Choose where it is reached

On Where it is reached, every service the source exposes on a domain gets one in the preview. Pick Preview hostnames:

  • Generated: each service gets an sslip.io or wildcard hostname of its own. Nothing to configure.
  • A pattern: readable hosts on a domain you point at this server. The Pattern must include {number}, and can use {service} and {wildcard}.
Pattern examples
pr-{number}-{service}.{wildcard}
pr-{number}-{service}.preview.example.com

Use {service} when the preview has more than one exposed service, such as a stack with web and api: without it, the first service takes the host and the others get generated ones. A stack service is exposed in the preview when its counterpart in the source has a domain, on the same port.

For pr-{number}-{service}.preview.example.com, create a wildcard DNS record *.preview.example.com pointing at the server. See Wildcard DNS.

Override variables

On Variables, add the variables that must differ in a preview, such as a test payment key or a public URL. They win over every definition in the source environment. Values are stored as secrets; when you edit later, a stored value shows Stored; type to replace.

Typical overrides
STRIPE_SECRET_KEY   sk_test_51H...
PUBLIC_URL          https://pr-42.preview.example.com

Set limits and safety

On Limits and safety:

  • At most: how many previews run at once, 1 to 50 (default 5). A pull request opened past the limit gets no preview until one closes.
  • Remove after: hours without a commit, up to 90 days (default 72). Closing or merging removes the preview at once.
  • Preview pull requests from forks: off by default. Leave it off for public repositories.
  • Previews on: turning previews off stops new ones; running ones stay until their pull request closes.

With forks allowed, anyone who can fork the repository can run code on your server. A fork's pull request is built and started with the preview's variables, secrets included, so its author can read every one of them and use the server's network. Only allow this when every fork author is trusted.

Review and turn on

Check the summary and select Turn on previews. Later edits use Save changes.

Reach one service from another

Every container of a preview, stack services included, gets the preview's URLs as variables:

VariableValue
LOGISTACK_PREVIEW_URLThe preview's main URL: that of the first followed service with a domain
LOGISTACK_PREVIEW_URL_<SERVICE>The URL of each exposed service, by its name in upper case with dashes as underscores

For a stack with web and api and the pattern pr-{number}-{service}.preview.example.com, pull request 42 sets LOGISTACK_PREVIEW_URL_API=https://pr-42-api.preview.example.com in both containers. Point the front end at its API with it:

docker-compose.yml
services:
  web:
    build: .
    environment:
      API_URL: ${LOGISTACK_PREVIEW_URL_API:-https://api.example.com}

Compose fills ${...} when LogiStack reads the file, before the variable exists, so the default applies there. Read LOGISTACK_PREVIEW_URL_API at runtime in your app instead, or add a preview variable override on Variables. A container reads the variables when it starts: when a new pull request commit adds an exposed service, LogiStack redeploys the preview's other services so they see its URL.

Check that it worked

Open a pull request on the repository. Then:

  • A new environment pr-<number> appears among the project's environments.
  • A comment on the pull request shows the preview URLs. LogiStack keeps it up to date as you push.
  • Each push to the pull request's branch redeploys the followed services. A followed stack re-reads its compose file and redeploys what changed and every service it builds.

Select the preview's environment tab to see its pull request, commit and each service's URL. From the environment's menu, Open preview opens the main URL and Pull request #<number> opens the pull request.

How a preview is removed

A preview is deleted, with its volumes and databases, when:

  • The pull request is closed or merged.
  • No pull request event touched it for the Remove after hours.

Common problems

  • No preview appears: the pull request isn't on the repository of a followed service, the limit in At most is reached, or the pull request comes from a fork and forks are off.
  • No comment on the pull request: for GitHub, the app needs read and write access to Pull requests; for GitLab, Gitea and Bitbucket, the token needs write access.
  • The preview can't reach its database: each preview gets an empty database. Run migrations with a pre-deploy command, and seed data in it if your app needs some. See Deployments.
  • "Include {number}, so each pull request gets its own host": add {number} to the pattern.
  • "A stack did not follow the last commit" on the preview's tab: the stack's compose file on the pull request's branch can't run, or can't be read. The message says why; the rest of the preview runs, and the stack keeps what it ran. Push a fix to the pull request. The stack's own page shows the same reason.
  • A stack service has no URL: only services whose counterpart in the source has a domain get one. Add a domain to the service in the source environment.

On this page