Skip to content

Run MockHub from a GitHub Actions workflow

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 or run a preview environment 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/)

The MockHub Action, mockhub-io/action, runs the CLI in a GitHub Actions job: it installs Node and the CLI, maps the CLI’s exit codes to the step’s result, and writes outputs a later step can read. It does what the CLI in a pipeline does, with less to write.

  1. Create a machine token with Sync a project from a repository under What is it for?, for the project the file targets. For preview environments, choose Preview environments for pull requests instead.

  2. Store the token as a repository secret named MOCKHUB_TOKEN.

  3. Add a workflow that checks for drift on pull requests and applies the file from the main branch:

    .github/workflows/mockhub.yml
    on: [push, pull_request]
    jobs:
    mockhub:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v7
    - name: Detect drift from staging
    id: drift
    if: github.ref != 'refs/heads/main'
    uses: mockhub-io/action@871cef41a8666dbbf227d8ac353cff3e6a892132 # v0.3.0
    with:
    token: ${{ secrets.MOCKHUB_TOKEN }}
    environment: staging
    - name: Fail on drift
    if: steps.drift.outputs.changes == 'true'
    run: |
    cat "${{ steps.drift.outputs.report }}"
    exit 1
    - name: Apply on main
    if: github.ref == 'refs/heads/main'
    uses: mockhub-io/action@871cef41a8666dbbf227d8ac353cff3e6a892132 # v0.3.0
    with:
    token: ${{ secrets.MOCKHUB_TOKEN }}
    environment: staging
    command: push
  4. Push a branch that changes mockhub.yaml.

On the branch, the drift step succeeds with changes set to true, and the next step fails the job with the plan printed. Once merged, the apply step applies the file.

Each release of the Action is tagged with the CLI version it installs: v0.3.0 runs @mockhub/cli@0.3.0. Pin the commit the tag points to, as above, so a moved tag cannot change what runs with your token. This prints it:

Terminal window
git ls-remote https://github.com/mockhub-io/action 'v0.3.0^{}'

The Action is published only as mockhub-io/action. The GitHub account mockhub is not MockHub’s: never write uses: mockhub/….

command chooses what the step runs; diff is the default.

command Runs The step fails when
validate mockhub validate --remote The file is invalid
diff mockhub diff, setting changes and report Only on an error: drift is reported, not failed
push mockhub push The plan deletes something and allow-deletions is unset
env-up mockhub env up, named pr-<number> after the pull request The environment cannot be created or updated
env-down mockhub env down; an environment that is already gone is not an error The environment cannot be deleted
openapi-validate mockhub openapi validate on a HAR, responses, requests or logs A result reaches fail-on, invalid by default
verify mockhub verify run on the suite file The verdict is anything but passed

Every input and output is listed in the Action’s README.

With a token for preview environments, env-up creates or updates an environment per pull request and env-down deletes it when the pull request closes. Set github-token to keep one comment on the pull request with the environment’s address and expiry.

on:
pull_request:
types: [opened, synchronize, reopened, closed]
jobs:
preview:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write # only for the comment
steps:
- uses: actions/checkout@v7
- name: Update the preview environment
if: github.event.action != 'closed'
uses: mockhub-io/action@871cef41a8666dbbf227d8ac353cff3e6a892132 # v0.3.0
with:
token: ${{ secrets.MOCKHUB_ENV_TOKEN }}
command: env-up
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Delete it when the pull request closes
if: github.event.action == 'closed'
uses: mockhub-io/action@871cef41a8666dbbf227d8ac353cff3e6a892132 # v0.3.0
with:
token: ${{ secrets.MOCKHUB_ENV_TOKEN }}
command: env-down

env-up sets gateway-url, expires-at and project-id for later steps. What an environment is and how long it lives are in Create a preview environment for each pull request.

  • A pull request from a fork gets no secrets on GitHub. For env-up, env-down and verify, the Action skips it, sets skipped to true and succeeds; a skipped step is not a pass. The other commands fail there for want of a token, so skip them with if: github.event.pull_request.head.repo.fork == false when the repository takes outside contributions. There is no pull_request_target mode.
  • The Action never creates an access key for a private environment, because the key would reach outputs, logs or the comment. Create one in a step of your own, as the Action’s README shows.
  • The step runs in bash and uses the runner’s Node, installing Node 22 only when the runner has none.

Report a problem with this page