Skip to content

    Frontend System Design

    Preview Environments and Pipeline Design

    Give every pull request its own live preview, keep it safe from real data, secrets and search engines, and design a CI pipeline that stays fast as a team grows.

    By Published 8 min read

    • frontend-system-design
    • preview-environments
    • ci-cd
    • pipelines
    Share

    Focused lessonintermediateFrontend System Design

    Before this: CI/CD for Front Ends

    What you'll learn: By the end of this lesson you will be able to explain when a preview environment is created and removed, list what it must be kept away from (production data, secrets, search engines, strangers), reason about pipeline speed as the longest chain of dependent jobs, and avoid the path-filter trap that leaves a required check pending.

    Think of a dress rehearsal: the set is built for real and the lights are on, but there is no audience and nobody sells a ticket. A preview environment is that rehearsal for one change, a live copy of the site that a reviewer can try and that disappears when the change is merged or abandoned.

    The running example is the ShopStream storefront from CI/CD for Front Ends: a single-page app built into static files, calling a JSON API.

    The core idea

    1. A preview is the build of one change, served at its own URL, so a designer or tester can see it without checking out code.
    2. Previews are cheap to create and easy to leak. Each is a new place where your code runs, with a reachable backend. The risks are real data, real secrets and public exposure.
    3. A pipeline is a dependency graph, so its speed is its longest chain. Running independent checks side by side matters more than speeding up any one.

    What a preview is

    Vercel's documentation says it creates a preview deployment by default when you push to a branch that is not the production branch, open a pull request, or deploy from the CLI without --prod. Each gets a branch-specific URL (always the latest changes on that branch) and a commit-specific one (that exact deployment). Netlify builds a Deploy Preview when you open a pull or merge request, at a URL prefixed deploy-preview and the request's number, such as deploy-preview-42--mysitename.netlify.app.

    If you build previews in CI, GitHub documents that a pull_request workflow runs on the merge of the branch into its base: GITHUB_REF is refs/pull/<number>/merge, so the preview shows the merged result a reviewer should approve. By default the workflow runs on the activity types opened, synchronize and reopened, so every push refreshes the preview. closed is a separate type, and merging a pull request closes it: the hook for removing the preview.

    Figure 1. The life of a preview

    sequenceDiagram accTitle: Life cycle of a preview environment accDescr: A developer opens a pull request. The pipeline builds the merge result and deploys it to a preview URL, which it posts on the pull request. Each new push rebuilds and redeploys it. A reviewer tests the preview. When the pull request is merged or closed, the pipeline removes the preview. participant D as Developer participant P as Pipeline participant V as Preview host participant R as Reviewer D->>P: open pull request P->>V: build merge result, deploy P-->>D: preview URL on the pull request D->>P: push more commits P->>V: rebuild, redeploy same URL R->>V: try the change D->>P: merge or close P->>V: remove the preview

    How to read this: the preview follows the pull request. Opening creates it, each push refreshes it, closing removes it.

    Illustration 1: the minimal example

    Removal is a second workflow that fires on closed. Not run: CI is not available here, so this sketch is checked against the documentation only; ./scripts/remove-preview.sh is your own script.

    name: remove-preview
    on:
      pull_request:
        types: [closed]
    
    permissions:
      contents: read
    
    jobs:
      cleanup:
        runs-on: ubuntu-latest
        steps:
          - run: ./scripts/remove-preview.sh "${{ github.event.pull_request.number }}"

    Netlify's documentation says a Deploy Preview stays available until it is deleted, automatically or manually, so know the retention setting rather than assuming it.

    What a preview must be kept away from

    A preview is a second copy of the site that anyone with the URL might open. Four things need a decision.

    • Backend and data. If a preview calls the production API, a reviewer clicking "place order" places a real order. Point previews at a staging backend with fake data. Vercel's documentation says previews use the variables scoped to the Preview environment, not production ones (the first deployment of a new project is always a production deployment).
    • Secrets. A change from a fork must not run with your secrets. GitHub documents that, with the exception of GITHUB_TOKEN, secrets are not passed to the runner when a workflow is triggered from a forked repository, and that the token is read-only there. Netlify says Deploy Previews from forks by authors outside your team are handled by the project's untrusted deploys policy.
    • Search engines. Vercel adds an X-Robots-Tag: noindex header to previews, but not on a custom domain assigned to a Git branch. Netlify adds it too and recommends a password or private project.
    • Strangers. New Vercel projects start with Vercel Authentication, so visitors to a preview must log in with an account that has access. Decide who may open a preview before putting its URL in a public pull request.

    Configuration that bakes in an address is another trap: a sign-in provider or API that allows only fixed origins rejects every preview with a new URL (see Runtime Configuration and Environments).

    Pipeline design: the longest chain

    GitHub documents that the jobs in a workflow run in parallel by default, and that you create dependencies with needs. Total time is the longest chain of dependent jobs, not the sum of all work.

    Illustration 2: the realistic example

    The storefront pipeline has seven jobs. As one chain, each waits for the previous. Fanned out, lint, types, unit tests and build need only the install. The minutes are illustrative. Run in Node:

    function wallClock(jobs) {
      const done = {};
      const finish = (name) => {
        if (done[name] === undefined) {
          const { minutes, needs = [] } = jobs[name];
          done[name] = minutes + Math.max(0, ...needs.map(finish));
        }
        return done[name];
      };
      return Math.max(...Object.keys(jobs).map(finish));
    }
    
    const serial = {
      install: { minutes: 2 },
      lint: { minutes: 1, needs: ["install"] },
      types: { minutes: 2, needs: ["lint"] },
      unit: { minutes: 3, needs: ["types"] },
      build: { minutes: 3, needs: ["unit"] },
      preview: { minutes: 2, needs: ["build"] },
      e2e: { minutes: 6, needs: ["preview"] },
    };
    const fanned = {
      install: { minutes: 2 },
      lint: { minutes: 1, needs: ["install"] },
      types: { minutes: 2, needs: ["install"] },
      unit: { minutes: 3, needs: ["install"] },
      build: { minutes: 3, needs: ["install"] },
      preview: { minutes: 2, needs: ["build"] },
      e2e: { minutes: 6, needs: ["preview"] },
    };
    console.log("one chain:", wallClock(serial), "min");
    console.log("fanned out:", wallClock(fanned), "min");

    Output, from running it in Node 26.13.1:

    one chain: 19 min
    fanned out: 13 min

    The same work, six minutes sooner, with nothing made faster. Effort now belongs on the remaining chain: a dependency cache, a smaller end-to-end suite (see End-to-End Testing and Flakiness). Parallel jobs cost runner minutes: money for waiting time.

    Figure 2. A fanned-out pipeline

    Fan out after install; the preview and end-to-end test are the long chainInstall to Lint; Install to Types and unit tests; Install to Build; Build to Preview; Preview to E2E test (preview URL)Install: lockfile exactInstalllockfile exactLintLintTypes and unit testsTypes and unittestsBuild: artifactBuildartifactPreview: per pull requestPreviewper pull requestE2E test: on the previewE2E teston the previewpreview URL

    How to read this: lint and tests run beside the build. The accent boxes are the longest chain, so shortening it shortens the pipeline.

    Skipping work, and the trap

    A workflow can use paths or paths-ignore filters, which accept glob patterns and decide whether to run by evaluating the changed files; you cannot use both for the same event.

    Illustration 3: a failure, a required check that never reports

    Suppose the branch rule requires a check named verify, and a documentation-only pull request is skipped by a path filter. GitHub documents that when a workflow is skipped by path, branch or commit-message filtering, its associated checks stay Pending and block merging, whereas a job skipped by a conditional reports Success. So always run the workflow and skip inside it.

    Figure 3. Skipping safely

    flowchart TD accTitle: Skipping work without blocking a required check accDescr: If a workflow is skipped by a path filter, its required check stays pending and blocks the merge. If the workflow always starts and a job is skipped by a conditional, the check reports success and the merge is allowed. A{"How was the
    work skipped?"} A -->|"path filter on the workflow"| B["Required check stays Pending
    merge blocked"] A -->|"job-level condition
    inside a workflow that ran"| C["Check reports Success
    merge allowed"]

    How to read this: both skips look alike in the log but not in the merge rule. Skip at the job, not the workflow, when a check is required.

    With a merge queue, GitHub documents that workflows must use the merge_group event to run when a pull request is added to the queue, so a check that listens only to pull_request never runs there.

    Trade-offs

    Choice Gain Cost
    Preview per pull request Reviewers see a real build Hosting cost, a backend to point at, cleanup
    Wide fan-out Short wait More runner minutes, more caches
    Path filters Less wasted work Pending required checks, missed cross-package breakage

    Build it

    There is no lab. The pipeline-time model ran as shown; the YAML and host behaviour come from documentation and were not run.

    The other side of this topic

    On the back end, a preview means a per-change copy of a service and its data, covered by the Backend lessons on Docker and CI/CD pipelines.

    In practice: the slow pipeline and the real order

    NOTE

    This is a realistic scenario, not a case history. It is a composite built from common production patterns. It describes no specific company or product, and every number in it is an illustrative assumption.

    The situation

    The storefront pipeline takes about 19 minutes per pull request, so developers merge without waiting for a preview. A reviewer has also placed a test order from a preview that pointed at the production API.

    Starting architecture

    Figure 4. One chain, one shared backend

    Starting architecture: serial jobs, previews on the production APIPipeline to Preview (deploy); Preview to Production API (calls)Pipeline: one serial chainPipelineone serial chainPreview: per pull requestPreviewper pull requestProduction API: real ordersProduction APIreal ordersdeploycalls

    How to read this: the warning boxes are the two problems, a slow chain and a preview that can change real data.

    Options considered

    Option What it fixes What it costs Risk
    Fan out jobs Waiting time Runner minutes Flaky jobs surface more
    Staging API for previews Real orders A seeded staging backend Drift from production

    The decision

    Field Decision record
    Context A 19-minute serial pipeline, and previews that reach the production API.
    Decision Fan out after install, point the Preview environment at a staging API with fake data, remove previews on close, and skip inside jobs rather than at the workflow.
    Consequences, good Faster reviews; no accidental real orders.
    Consequences, bad A staging backend to maintain.
    Revisit when Per-pull-request backends become affordable.

    Rollout and verification

    1. Set the Preview variables first, deploy a preview and read the API host it calls.
    2. Fan out the jobs, then compare wall-clock time on two pull requests.
    3. Open a documentation-only pull request and confirm the required check reports; close one and confirm its preview is gone.

    The 2 a.m. failure

    Overnight, a bot opens dozens of dependency-update pull requests; each builds a preview, runner minutes run out, and the morning's urgent fix waits.

    First three checks: (1) whether stale runs are cancelled; (2) the count of open previews; (3) whether the bot is rate-limited. Mitigation: cancel stale runs and pause the bot. Follow-up: cap concurrent previews.

    Architect's notes

    • Time is the longest chain. Draw the dependency graph before adding a runner.
    • Decide the preview's backend before its URL.

    Common mistakes

    • Previews that call the production API with production credentials.
    • A path filter on a workflow whose check is required.
    • Serial jobs that do not depend on one another.

    Key takeaways

    • A preview is one change served at its own URL, created and removed with the pull request.
    • Keep previews away from production data, secrets, search engines and strangers.
    • Pipeline time is the longest chain of dependent jobs.
    • A path-filtered workflow leaves its required check pending; a conditionally skipped job reports success.

    Practice exercises

    1. Add a job a11y (2 minutes) that needs the preview, and recompute the fanned-out time.
    2. Write the branch rule and workflow so a documentation-only change still gets a green required check.

    In this course:

    Official documentation

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments