Skip to main content

pact-action (GitHub Action)

This page is pact-action/README.md, copied by yarn workspace apiome-docs sync:action-pages — edit the README, not this page.

GitHub Action that runs the Pact steps of a pipeline against your Apiome workspace's Pact Broker (PFX-4.3 / #6940; record: PFX-9.4 / #7322), with the apiome pact commands:

  • publish — publishes the consumer's pacts for the commit and its branch, with a link to the run
  • can-i-deploy — gates a pull request or a deploy on the broker's verdict, and keeps one sticky PR comment with the answer up to date
  • record-deployment — records what was deployed, after the deploy, on merge
  • record — runs the consumer's tests against the hosted mock and turns the calls they made into a pact, published when the tests pass — no Pact library needed

Every step exits like pact-broker: 0 when it worked (or the version can be deployed), 1 otherwise; record exits with the tests' own code when they fail. Wrong or missing inputs are refused with the reason before anything is sent. The runner needs bash, curl and jq; GitHub-hosted runners have all three.

Usage​

name: pact
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
pull-requests: write
jobs:
pact:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run test:pact # writes ./pacts

- uses: apiome/apiome/pact-action@main
with:
command: publish
broker-base-url: ${{ secrets.PACT_BROKER_BASE_URL }}
broker-token: ${{ secrets.PACT_BROKER_TOKEN }}

- uses: apiome/apiome/pact-action@main
with:
command: can-i-deploy
pacticipant: Billing Service
environment: production
retry-while-unknown: "12"
broker-base-url: ${{ secrets.PACT_BROKER_BASE_URL }}
broker-token: ${{ secrets.PACT_BROKER_TOKEN }}

- if: github.event_name == 'push'
run: ./deploy.sh production

- if: github.event_name == 'push'
uses: apiome/apiome/pact-action@main
with:
command: record-deployment
pacticipant: Billing Service
environment: production
broker-base-url: ${{ secrets.PACT_BROKER_BASE_URL }}
broker-token: ${{ secrets.PACT_BROKER_TOKEN }}

A pact from the hosted mock​

record wraps the test command: the tests get APIOME_MOCK_URL (where the mock answers), APIOME_MOCK_CAPTURE_HEADER (X-Mock-Capture) and APIOME_MOCK_CAPTURE (<consumer>@<version>) in their environment. Point the HTTP client at the first and send the header on every request; the action then waits for the mock to store the calls, writes pacts/<consumer>-<provider>.json, and publishes it when the tests passed (publish: false only writes it).

- uses: apiome/apiome/pact-action@main
with:
command: record
mock: billing-api/2.7.0
pacticipant: Orders UI
run: npm test # or: pytest, go test ./...
broker-base-url: ${{ secrets.PACT_BROKER_BASE_URL }}
broker-token: ${{ secrets.PACT_BROKER_TOKEN }}

The key needs pacts:view, versions:view, pacts:delete (an earlier capture of the same commit is deleted first) and pacts:create to publish.

PACT_BROKER_BASE_URL is your workspace's broker base URL, such as https://api.apiome.dev/v1/tenants/acme/pact-broker. PACT_BROKER_TOKEN is an API key with the Pact: consumer + deploy preset; a job that only asks can-i-deploy needs Pact: read-only. When the job already sets PACT_BROKER_BASE_URL and PACT_BROKER_TOKEN in env:, leave the two inputs out.

Full guide: Run Pact in CI, which also has the GitLab CI, Bitbucket Pipelines, Jenkins and Azure Pipelines recipes (Jenkinsfile, azure-pipelines.yml).

Inputs​

InputUsed byDefaultDescription
commandall(required)publish, can-i-deploy, record-deployment or record. Anything else is refused; run other apiome pact commands in a run: step
broker-base-urlallPACT_BROKER_BASE_URLThe workspace's broker base URL (http or https)
broker-tokenallPACT_BROKER_TOKENAPI key with the Pact scopes the command needs. Passed to the CLI in the environment, never on its command line. PACT_BROKER_PASSWORD also works
pactspublishpactsPact files or folders of pact files, one per line. A path that does not exist is refused
pacticipantcan-i-deploy, record-deployment, record—The application being deployed; for record, the consumer whose tests run (required)
mockrecord—The mocked API, <project>/<version> (required)
runrecord—The test command, run with bash -c, such as npm test (required)
matchingrecordtypeexact (the recorded values), type (any value of the same type) or spec (type plus the spec's formats)
fieldsrecordallall (every response field) or read (only the fields the tests reported reading with the SDK's fieldUsage hook or the mock's browser recorder; refused without a report)
publishrecordtrueHand the pact to the mock's recording policy (by default it publishes when the tests pass, and publishes a looser pact with its owners told or holds it for review); false only writes it
providerrecordthe pacticipant bound to the mocked projectThe provider's name
versionallthe PR's head commit, or the commit the workflow runs onThe application version
branchpublish, recordthe PR's head branch, or the branch the workflow runs onThe branch the version came from
tagspublish, record—Tags for the consumer version, one per line
environmentcan-i-deploy, record-deployment—The environment's name, such as production (required)
application-instancerecord-deployment—The instance deployed to (a region, a customer)
retry-while-unknowncan-i-deploy0Retry this many times while a verification result is missing
retry-intervalcan-i-deploy10Seconds between retries
dry-runcan-i-deployfalsetrue reports the verdict but never fails the step
commentpublish, can-i-deploy, recordtruefalse skips the sticky PR comment
comment-idpublish, can-i-deploy, record—Tells comments apart when one PR runs the same command more than once
github-tokenpublish, can-i-deploy, record${{ github.token }}Token for the sticky comment (needs pull-requests: write)
installalltrueInstall the Apiome CLI unless apiome is already on PATH; false when an earlier step installs it
cli-versionallmainGit tag, branch or commit of the CLI to install. Pin a tag in real pipelines

Outputs​

OutputDescription
exit-codeThe command's exit code: 0 it worked (or deployable), 1 anything else; record — the tests' own code when they failed
report-pathapiome-pact-<command>.md in the workspace: the command's markdown output
deployablecan-i-deploy only: true or false; empty when the check could not run

What it does​

  1. Installs the CLI with the installer from https://get.apiome.dev for the runner user (~/.local/bin, no sudo), unless apiome is already on PATH.
  2. Runs one command with --output markdown, writes the output to apiome-pact-<command>.md and prints it in the log.
  3. Writes the job summary: a heading with the verdict, the command's output and any messages it printed (for record, the last 40 lines — the tests' output streams to the log as they run).
  4. Upserts one sticky PR comment for publish, can-i-deploy and record on a pull request. The comment is found again by a hidden marker made of the command, the pacticipant and the environment (or comment-id), so each gate keeps one comment however many times the PR is pushed. A comment that cannot be posted is a warning; it never changes the step's result. Outside a pull request no comment is posted.

What it refuses​

Each refusal is an error annotation with the reason, exits 1, and sends nothing:

RefusedWhy
A command other than the fourThe action covers the pipeline's three steps; the rest of apiome pact runs in a run: step
No broker URL or token, or a URL that is not http(s)The step cannot reach your workspace
can-i-deploy or record-deployment without pacticipant or environmentThe broker needs both to answer
retry-while-unknown, retry-interval, dry-run or comment with a value of the wrong kindNo guessing what was meant
An empty pacts, or a path that does not existUsually the Pact tests did not run before the step
record without mock (or not <project>/<version>), pacticipant or run; a matching, fields or publish of the wrong kindThe step would record nothing, or the wrong thing
record-deployment in a pull_request or pull_request_target runRecording it would tell can-i-deploy the PR's commit is live; run it on push to your main branch

Tests​

bash pact-action/tests/run.sh # every test
bash pact-action/tests/test_ci_recipes.sh --recipe gitlab # or bitbucket, jenkins, azure

test_ci_recipes.sh runs each job of the GitLab, Bitbucket, Jenkins and Azure recipes against a stub CLI and checks that the guide embeds each recipe byte for byte.