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.
-
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.
-
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 inmockhub.yamlas${env:SANDBOX_KEY}undersettings.upstreamHeaders, so the file never holds it. -
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-lateststeps:- uses: actions/checkout@v7- uses: actions/setup-node@v7with: { node-version: 22 }- name: Fail when mockhub.yaml drifts from stagingif: github.ref != 'refs/heads/main'run: npx --yes @mockhub/cli@0.3.0 diff --env stagingenv:MOCKHUB_TOKEN: ${{ secrets.MOCKHUB_TOKEN }}- name: Apply on mainif: github.ref == 'refs/heads/main'run: npx --yes @mockhub/cli@0.3.0 push --env staging --yesenv:MOCKHUB_TOKEN: ${{ secrets.MOCKHUB_TOKEN }}For GitLab CI:
mockhub:diff:image: node:22rules:- if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCHscript:- npx --yes @mockhub/cli@0.3.0 diff --env stagingmockhub:push:image: node:22rules:- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCHscript:- npx --yes @mockhub/cli@0.3.0 push --env staging --yesGitLab passes the
MOCKHUB_TOKENCI/CD variable to the job as an environment variable. -
Push a branch that changes
mockhub.yaml.
Result
Section titled “Result”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.
What the pipeline can rely on
Section titled “What the pipeline can rely on”| 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.
Verify the application in the pipeline
Section titled “Verify the application in the pipeline”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.
Limits
Section titled “Limits”--yesonpushconfirms deletions without showing them. Keep it to the branch that owns the project, and review deletions in thediffof the pull request.- Pull requests from forks get no secrets on GitHub, so the
diffstep fails there for want of a token. Skip it withif: github.event.pull_request.head.repo.fork == falsewhen 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.