Skip to content

Check and apply configuration from CI

Before you begin

Plan
Included in Free, Pro and Team. How plans and limits work
Role
Viewer or higher to check for drift, Editor or higher to apply mocks and scenarios and Admin or higher to apply settings, chaos profiles or webhooks.
You need
  • A repository with a mockhub.yaml, as in [Keep a project's configuration in a repository](/guides/cli/install-and-connect/)

A pipeline runs mockhub diff on every change, so a pull request whose mockhub.yaml no longer matches the project fails, and mockhub push on the main branch, so what is merged is what the project serves.

  1. Create a machine token with Sync a project from a repository under What is it for?, for the project the file targets. Select Also apply settings, chaos profiles and webhooks only if the file changes those.

  2. Store the token as a secret of the repository’s CI, named MOCKHUB_TOKEN. Keep a credential for a project’s upstream there too, and write it in mockhub.yaml as ${env:SANDBOX_KEY} under settings.upstreamHeaders, so the file never holds it.

  3. Add a job that compares the file with the project, and applies it from the main branch. For GitHub Actions:

    .github/workflows/mockhub.yml
    on: [push, pull_request]
    jobs:
    mockhub:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v7
    - uses: actions/setup-node@v7
    with: { node-version: 22 }
    - name: Fail when mockhub.yaml drifts from staging
    if: github.ref != 'refs/heads/main'
    run: npx --yes @mockhub/cli@0.3.0 diff --env staging
    env:
    MOCKHUB_TOKEN: ${{ secrets.MOCKHUB_TOKEN }}
    - name: Apply on main
    if: github.ref == 'refs/heads/main'
    run: npx --yes @mockhub/cli@0.3.0 push --env staging --yes
    env:
    MOCKHUB_TOKEN: ${{ secrets.MOCKHUB_TOKEN }}

    For GitLab CI:

    mockhub:diff:
    image: node:22
    rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
    script:
    - npx --yes @mockhub/cli@0.3.0 diff --env staging
    mockhub:push:
    image: node:22
    rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    script:
    - npx --yes @mockhub/cli@0.3.0 push --env staging --yes

    GitLab passes the MOCKHUB_TOKEN CI/CD variable to the job as an environment variable.

  4. Push a branch that changes mockhub.yaml.

On the branch, diff prints the plan and exits 1, which fails the job: the file and the project differ. Once merged, push applies the file and the next diff prints No changes. The project matches the file. and exits 0.

Situation What happens
diff finds changes Exit 1. Use it to fail a check, or ignore the exit code to only report
push without --yes would delete something, off a terminal Nothing is applied, exit 2: The plan deletes 1 resource(s): mocks "GET /orders". Non-interactive sessions must pass --yes to confirm deletions.
The project changes between the preview and the apply Nothing is applied, exit 2, and the CLI says to run mockhub diff again
The document is invalid Nothing is applied, exit 2, with each field the project refused
Applied, but the mock server has not caught up Exit 0 with a warning. The change is saved, and requests may get the old answers for a moment

diff and push take --report-json <path> to write the project’s full report, such as each created, changed and deleted resource, for a later step to read. The exit codes of every command are in CLI commands.

A pipeline can also check an application against a fresh dependency with mockhub verify run, as in Verify an application against a mock dependency. The step uses a machine token that can create preview environments, and it fails unless the verdict is passed: exit 1 is a failed assertion, and anything MockHub could not decide exits 2.

- name: Verify the application
if: github.event.pull_request.head.repo.fork != true
run: >-
npx --yes @mockhub/cli@0.3.0 verify run --file mockhub.verification.yaml
--before-case ./point-app-at-dependency.sh --report-json verification-report.json
env:
MOCKHUB_TOKEN: ${{ secrets.MOCKHUB_TOKEN }}

--report-json writes the sealed report for a later step to upload. A pull request from a fork gets no secrets on GitHub, so the condition skips the step there; a skipped step is not a pass, so do not count it as one. If the job is cancelled, the CLI cancels the run and deletes its environments.

  • --yes on push confirms deletions without showing them. Keep it to the branch that owns the project, and review deletions in the diff of the pull request.
  • Pull requests from forks get no secrets on GitHub, so the diff step fails there for want of a token. Skip it with if: github.event.pull_request.head.repo.fork == false when the repository takes outside contributions.
  • One token reaches one project, or every project in the workspace. Give each pipeline the narrowest it needs.
  • Pin the CLI version, as above, and update it on purpose: a newer CLI can write a file that an older one refuses.

Report a problem with this page