Create a preview environment for each pull request
Before you begin
- Plan
- Included in Pro and Team. How plans and limits work
- Role
- Editor or higher.
- You need
- A repository with a mockhub.yaml, as in [Keep a project's configuration in a repository](/guides/cli/install-and-connect/)
An ephemeral environment is a project the pipeline creates from the branch’s mockhub.yaml, with its own address, so tests for a pull request run against that branch’s mocks without touching a shared project. It deletes itself when its time runs out.
-
Create a machine token with Preview environments for pull requests under What is it for?. Settings → Environments links to the same form with New CI token.
-
Store the token as a secret of the repository’s CI, named
MOCKHUB_ENV_TOKEN. -
Add a job that creates or updates the environment on every push to the pull request, and deletes it when the pull request closes. For GitHub Actions:
.github/workflows/mockhub-preview.yml on:pull_request:types: [opened, synchronize, reopened, closed]jobs:preview:runs-on: ubuntu-latest# Pull requests from forks get no secrets: skip them instead of failing.if: github.event.pull_request.head.repo.fork == falsesteps:- uses: actions/checkout@v7- uses: actions/setup-node@v7with: { node-version: 22 }- name: Update the preview environmentif: github.event.action != 'closed'run: npx --yes @mockhub/cli@0.3.0 env up "pr-${{ github.event.number }}"env:MOCKHUB_TOKEN: ${{ secrets.MOCKHUB_ENV_TOKEN }}- name: Delete it when the pull request closesif: github.event.action == 'closed'run: npx --yes @mockhub/cli@0.3.0 env down "pr-${{ github.event.number }}"env:MOCKHUB_TOKEN: ${{ secrets.MOCKHUB_ENV_TOKEN }}For GitLab CI, with the token in a
MOCKHUB_TOKENCI/CD variable:mockhub:preview:image: node:22rules:- if: $CI_PIPELINE_SOURCE == "merge_request_event"script:- npx --yes @mockhub/cli@0.3.0 env up "mr-$CI_MERGE_REQUEST_IID"mockhub:preview:stop:image: node:22rules:- if: $CI_PIPELINE_SOURCE == "merge_request_event"when: manualscript:- npx --yes @mockhub/cli@0.3.0 env down "mr-$CI_MERGE_REQUEST_IID" -
Open a pull request.
Result
Section titled “Result”The job prints where the environment answers and when it expires:
Created environment "pr-482". Gateway: https://pr-482-0198f6a2.mockhub.dev Expires: 2026-10-18T14:00:00+00:00Applied: 12 created, 0 updated, 0 deleted.Point the pull request’s tests at the Gateway address. The next push to the pull request prints Environment "pr-482" already exists — expiry extended., applies the branch’s file again, and starts the countdown afresh. Settings → Environments lists every environment with its URL and when it Expires, and has buttons to extend or delete each one.
How an environment behaves
Section titled “How an environment behaves”- It is a real project. It has mocks, request logs and version history like any other, and counts against every limit of the plan, such as projects and active mocks. The plan also sets how many environments a workspace can have at once and how long one can live; see the limits page.
- Its lifetime is a countdown. Without
--ttl, an environment lives 4 hours, or less if the plan’s ceiling is lower.--ttl 24asks for 24 hours. When the time runs out, the project is deleted with its request logs, versions and webhook deliveries, whether or not the pipeline ranenv down. env downis safe to repeat. For a name the workspace no longer has, because it expired or another job deleted it,env downprintsEnvironment "pr-482" does not exist, so there was nothing to delete., lists the environments that do exist, and exits0. A wrong workspace, address or token still fails.env upnever asks. It applies the branch’s file without confirming deletions: removing what the branch removed is the point. Use it only with environments, never to update a project people rely on.- Its address follows from its name. The subdomain is the name followed by the first eight characters of the workspace’s id, on MockHub’s own domain, so the same name always reaches the same environment. The name is 1 to 32 lowercase letters, digits or hyphens, and cannot start or end with a hyphen.
- Seeds load after the file. A
seedin the file fills the data store once the configuration is applied;--reset-storeempties the store first. See CLI commands.
Private environments
Section titled “Private environments”mockhub env up pr-482 --private, or accessMode: private under settings in the file, creates the environment as a private project from the start, so its address never answers publicly. It needs a token with the Admin role, and a plan with private projects (Pro and Team). No access key is created: until a step of your own makes one with mockhub access-keys create <name> --project <id> --output <file>, the environment refuses every request. See Make a project private.
If the pipeline fails
Section titled “If the pipeline fails”| The CLI prints | What to do |
|---|---|
Ephemeral projects are not available on your plan. Upgrade to use them. |
Environments are part of Pro and Team. |
Your plan allows … ephemeral project(s) per workspace. |
Delete an environment in Settings → Environments, or with mockhub env down, or wait for one to expire. |
TTL exceeds your plan ceiling of … hours. |
Ask for less with --ttl, or leave it out. |
An environment name must be 1-32 lowercase letters, digits or hyphens, and cannot start or end with a hyphen. |
Change the name the job passes to env up and env down. |
The derived host is taken by an existing project. |
A project that is not an environment already has the address. Use another name. |
MockHub has no room for new projects right now. Existing projects are not affected. Try again later. |
MockHub has no room for new projects, so no environment was created. Run the job again later; a push to an existing environment is not refused. See No room for new projects. |
| A message about the token’s scope | The token cannot manage environments. Create one with Preview environments for pull requests; see Token scope. |
With a personal token, the env commands need a top-level "workspace" key in mockhub.yaml. … |
Use a machine token, or add workspace: with the workspace’s id to the file. |
Limits
Section titled “Limits”- An environment’s address cannot be changed, and it cannot use a custom domain.
- A token that manages environments reaches every project in the workspace; it cannot be limited to one.
env downcannot tell an environment that expired from a name that never existed: both exit0. Check the job’s log for a misspelled name.- Settings → Environments needs the Editor role, even to see the list.