Run Pact in CI
This page puts the apiome pact commands together into the three jobs of a Pact
pipeline: the consumer publishes its pacts, the provider is verified against them, and the
deploy job asks can-i-deploy and records what it deployed. On GitHub, the
apiome/pact-action runs the steps for you and keeps the can-i-deploy
answer in one pull-request comment; for GitLab CI and Bitbucket Pipelines there are
ready-made pipelines, and Jenkins and Azure Pipelines have
theirs. Every step is also one shell command, so the same lines
work in any other runner.
Copy a pipeline from the app. In Contracts, click Settings, then CI and commit
statuses. Pipeline recipes shows the GitHub Actions, GitLab CI, Jenkins and Azure Pipelines
pipelines of this page — exactly the files below — and Broker address shows your workspace's
PACT_BROKER_BASE_URL, with Create a key with Pact scopes for the token. See
Choose which statuses are written for the
switches on the same tab.
Before you start
Starting from an empty workspace? Set up contracts creates the key, writes the two secrets into your GitHub repositories and opens a pull request with the can-i-deploy and record-deployment steps for you. The rest of this page is the same pipeline by hand.
-
Two CI secrets.
PACT_BROKER_BASE_URL— your workspace's broker base URL, such ashttps://api.apiome.dev/v1/tenants/acme/pact-broker— andPACT_BROKER_TOKEN, an API key from a member whose role holds Pacts → Create. Create the key on API keys with the narrowest Pact preset each job needs:Job Preset Scopes Consumer (section 1) and deploy (section 3) Pact: consumer + deploy pact:readpact:publishProvider (section 2) Pact: CI verifier pact:readpact:verifyA job that only asks can-i-deployPact: read-only pact:readA pipeline already set up for basic auth can keep
PACT_BROKER_USERNAMEandPACT_BROKER_PASSWORDinstead: the password is the API key. -
The environments you deploy to, created once with a Full access key:
apiome pact create-environment --name testapiome pact create-environment --name production --production -
For hosted verification, a verification target for the provider, such as
ledger-staging.
Each job installs the CLI with the installer:
curl -fsSL https://get.apiome.dev -o get-apiome.sh && sh ./get-apiome.sh --user
Use the GitHub Action
apiome/pact-action installs the CLI and runs one of publish, can-i-deploy,
record-deployment or record per step. On a pull request, publish, can-i-deploy and record
keep one comment
on the pull request up to date with their result, and every run writes the result to the job
summary:
# .github/workflows/pact.yml
name: pact
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
pull-requests: write
jobs:
pact:
runs-on: ubuntu-latest
env:
PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run test:pact
- uses: apiome/apiome/pact-action@main
with:
command: publish
- uses: apiome/apiome/pact-action@main
with:
command: can-i-deploy
pacticipant: Billing Service
environment: production
retry-while-unknown: "12"
- 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
The version is the pull request's head commit (or the pushed commit), and the branch is the pull
request's branch. record-deployment refuses to run in a pull-request run, because recording it
would tell can-i-deploy that an unmerged commit is live. Every input, output and refusal is on
pact-action. The sections below are the same pipeline as plain
commands.
1. The consumer publishes its pacts
After the consumer's Pact tests write their files to ./pacts:
# .github/workflows/consumer.yml
name: consumer
on: push
jobs:
pact:
runs-on: ubuntu-latest
env:
PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run test:pact
- run: curl -fsSL https://get.apiome.dev -o get-apiome.sh && sh ./get-apiome.sh --user
- run: apiome pact publish ./pacts --consumer-app-version "$GITHUB_SHA" --auto-detect-version-properties
--auto-detect-version-properties records the branch and the link to this run. The job fails if
a file is not a pact, or if this commit already published a different pact.
Record a pact from the hosted mock
A consumer with no Pact tests can still publish a pact: run its ordinary tests against the
hosted mock and let record turn the calls into the pact. The tests get
APIOME_MOCK_URL, APIOME_MOCK_CAPTURE_HEADER and APIOME_MOCK_CAPTURE in their environment; point
the HTTP client at the first and send the header (X-Mock-Capture: <consumer>@<version>) on every
request.
- 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 ./...
run goes through bash -c, so npm ci && npm test works. The pact is written to
pacts/<consumer>-<provider>.json and handed to the mock's
recording policy, which by default publishes it when
the tests pass and publishes a looser pact with its owners told — or holds it for review
(publish: false only writes it); matching is type by default (exact or spec). The step exits with the tests' own code when
they fail, and 1 when they pass but nothing was recorded or the pact could not be published. The
tests' output streams to the log; the job summary and the pull-request comment show the report and the
last 40 lines. Outside GitHub, run the same thing as a command:
apiome pact record --mock billing-api/2.7.0 --consumer "Orders UI" --publish -- npm test
The key needs Pacts → View, Versions → View, Pacts → Delete (an earlier capture of the same commit is deleted first) and the publish permission.
2. The provider is verified
Either run your own Pact verifier and let it publish its results, or let Apiome replay the pacts for you:
# .github/workflows/provider.yml (a step of the provider's build)
- run: |
apiome pact verify \
--provider "Ledger API" --provider-app-version "$GITHUB_SHA" --branch "$GITHUB_REF_NAME" \
--target ledger-staging \
--consumer-version-selector '{"mainBranch": true}' \
--consumer-version-selector '{"deployedOrReleased": true}' \
--enable-pending -o markdown >> "$GITHUB_STEP_SUMMARY"
The step fails when a pact that is not pending fails. If your verifier ran elsewhere, send its
report instead with
apiome pact publish-verification-results.
To start this job whenever a consumer publishes a pact that needs verifying, create a webhook once, with a Full access key:
apiome pact create-webhook https://ci.example.com/hooks/ledger-verify \
--header "Content-Type: application/json" \
--data '{"pactUrl": "${pactbroker.pactUrl}", "providerVersion": "${pactbroker.providerVersionNumber}"}' \
--provider "Ledger API" --contract-requiring-verification-published
To start a GitHub Actions workflow directly, create the webhook with a GitHub target as
Contract webhooks shows; create-webhook sends plain HTTP requests only.
3. Deploy, gated on contracts
# a step of each service's deploy job
- run: |
apiome pact can-i-deploy --pacticipant "Billing Service" --version "$GITHUB_SHA" \
--to-environment production --retry-while-unknown 12 --retry-interval 10
- run: ./deploy.sh production
- run: |
apiome pact record-deployment --pacticipant "Billing Service" --version "$GITHUB_SHA" \
--environment production
can-i-deploy exits 1 and the deploy stops when the version is not compatible with what runs in
production, or when a verification result is still missing after the retries. Recording the
deployment afterwards is what lets the next can-i-deploy of its consumers and providers check
against it.
An AI agent can run step 3's checks too, for example a coding agent deciding whether a release is
safe. It uses the pact.can_i_deploy and pact.record_deployment MCP tools with an MCP key. It
gets the same verdict as the pipeline. See
Ask from an AI agent.
See the verdict on the pull request
Because every job above uses the commit SHA as the version, Apiome can post each pact's verdict
back to the commit as a GitHub or GitLab commit status named
apiome-pact/<consumer>/<provider>. The consumer's pull request shows pending until the
provider verifies the pact, then success or failure. The provider's pull request shows
the result of its own verification. To require it before merging, add the status to the branch
protection rule.
To turn this on, register both repositories under Repositories and set each pacticipant's repository URL. No CI step is needed. See Commit statuses.
GitLab CI and Bitbucket Pipelines
The two pipelines below run the same three steps in the image that carries the Apiome CLI. Merge
requests and pull requests publish the pacts and ask can-i-deploy; the main branch also records
the deployment. Add PACT_BROKER_BASE_URL and PACT_BROKER_TOKEN as masked (GitLab) or secured
(Bitbucket) variables, change PACTICIPANT and DEPLOY_ENVIRONMENT to yours, and put your deploy
command before record-deployment. can-i-deploy keeps its answer as the can-i-deploy.md
artifact. Both pipelines are run against a stub CLI on every change, so they stay correct.
GitLab CI
# Apiome Pact pipeline for GitLab CI (PFX-4.3 / #6940).
#
# Copy the jobs into your .gitlab-ci.yml. Merge requests publish the pacts and ask can-i-deploy;
# the default branch does the same, deploys, then records the deployment.
#
# Required CI/CD variables (Settings -> CI/CD -> Variables, "Masked" + "Protected"):
# PACT_BROKER_BASE_URL https://api.apiome.dev/v1/tenants/<workspace>/pact-broker
# PACT_BROKER_TOKEN Apiome API key with the "Pact: consumer + deploy" preset
#
# Every apiome pact command exits 0 when it worked and 1 otherwise, so each job fails on its own.
stages:
- test
- pact
- deploy
variables:
PACTICIPANT: Billing Service
PACT_DIR: pacts
DEPLOY_ENVIRONMENT: production
APIOME_LOAD_DOTENV: "0"
.apiome-pact:
image:
# The image carries the Apiome CLI. Pin a version tag or digest - `latest` drifts.
name: ghcr.io/apiome/diff-action:latest
# The image entrypoint is the GitHub Action wrapper; GitLab needs a shell.
entrypoint: [""]
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
pact-publish:
extends: .apiome-pact
stage: pact
script:
- 'test -n "$PACT_BROKER_BASE_URL" || { echo "PACT_BROKER_BASE_URL is not set - add it as a masked CI/CD variable" >&2; exit 1; }'
- 'test -n "$PACT_BROKER_TOKEN" || { echo "PACT_BROKER_TOKEN is not set - add it as a masked CI/CD variable" >&2; exit 1; }'
- 'apiome pact publish "$PACT_DIR" --consumer-app-version "$CI_COMMIT_SHA" --branch "${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:-$CI_COMMIT_BRANCH}" --build-url "$CI_JOB_URL"'
pact-can-i-deploy:
extends: .apiome-pact
stage: pact
needs: [pact-publish]
script:
- 'test -n "$PACT_BROKER_BASE_URL" || { echo "PACT_BROKER_BASE_URL is not set - add it as a masked CI/CD variable" >&2; exit 1; }'
- 'test -n "$PACT_BROKER_TOKEN" || { echo "PACT_BROKER_TOKEN is not set - add it as a masked CI/CD variable" >&2; exit 1; }'
- 'apiome pact can-i-deploy --pacticipant "$PACTICIPANT" --version "$CI_COMMIT_SHA" --to-environment "$DEPLOY_ENVIRONMENT" --retry-while-unknown 12 --retry-interval 10 --output markdown > can-i-deploy.md || gate=$?'
- 'cat can-i-deploy.md'
- 'exit ${gate:-0}'
artifacts:
when: always
expire_in: 1 week
paths:
- can-i-deploy.md
pact-record-deployment:
extends: .apiome-pact
stage: deploy
needs: [pact-can-i-deploy]
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- 'test -n "$PACT_BROKER_BASE_URL" || { echo "PACT_BROKER_BASE_URL is not set - add it as a masked CI/CD variable" >&2; exit 1; }'
- 'test -n "$PACT_BROKER_TOKEN" || { echo "PACT_BROKER_TOKEN is not set - add it as a masked CI/CD variable" >&2; exit 1; }'
- 'echo "deploy $CI_COMMIT_SHA to $DEPLOY_ENVIRONMENT here, or run this job after your deploy job"'
- 'apiome pact record-deployment --pacticipant "$PACTICIPANT" --version "$CI_COMMIT_SHA" --environment "$DEPLOY_ENVIRONMENT"'
Bitbucket Pipelines
# Apiome Pact pipeline for Bitbucket Pipelines (PFX-4.3 / #6940).
#
# Copy the steps into your bitbucket-pipelines.yml. Pull requests publish the pacts and ask
# can-i-deploy; main does the same, deploys, then records the deployment.
#
# Required repository variables (Repository settings -> Repository variables, check "Secured"):
# PACT_BROKER_BASE_URL https://api.apiome.dev/v1/tenants/<workspace>/pact-broker
# PACT_BROKER_TOKEN Apiome API key with the "Pact: consumer + deploy" preset
#
# Every apiome pact command exits 0 when it worked and 1 otherwise, so each step fails on its own.
# The image carries the Apiome CLI. Pin a version tag or digest - `latest` drifts.
image: ghcr.io/apiome/diff-action:latest
definitions:
steps:
- step: &pact-publish
name: Publish pacts
script:
- 'export PACT_DIR="${PACT_DIR:-pacts}"'
- 'export APIOME_LOAD_DOTENV=0'
- 'test -n "$PACT_BROKER_BASE_URL" || { echo "PACT_BROKER_BASE_URL is not set - add it as a secured repository variable" >&2; exit 1; }'
- 'test -n "$PACT_BROKER_TOKEN" || { echo "PACT_BROKER_TOKEN is not set - add it as a secured repository variable" >&2; exit 1; }'
- 'apiome pact publish "$PACT_DIR" --consumer-app-version "$BITBUCKET_COMMIT" --branch "$BITBUCKET_BRANCH" --build-url "$BITBUCKET_GIT_HTTP_ORIGIN/pipelines/results/$BITBUCKET_BUILD_NUMBER"'
- step: &pact-can-i-deploy
name: Pact can-i-deploy
script:
- 'export PACTICIPANT="${PACTICIPANT:-Billing Service}"'
- 'export DEPLOY_ENVIRONMENT="${DEPLOY_ENVIRONMENT:-production}"'
- 'export APIOME_LOAD_DOTENV=0'
- 'test -n "$PACT_BROKER_BASE_URL" || { echo "PACT_BROKER_BASE_URL is not set - add it as a secured repository variable" >&2; exit 1; }'
- 'test -n "$PACT_BROKER_TOKEN" || { echo "PACT_BROKER_TOKEN is not set - add it as a secured repository variable" >&2; exit 1; }'
- 'apiome pact can-i-deploy --pacticipant "$PACTICIPANT" --version "$BITBUCKET_COMMIT" --to-environment "$DEPLOY_ENVIRONMENT" --retry-while-unknown 12 --retry-interval 10 --output markdown > can-i-deploy.md || gate=$?'
- 'cat can-i-deploy.md'
- 'exit ${gate:-0}'
artifacts:
- can-i-deploy.md
- step: &pact-record-deployment
name: Record deployment
script:
- 'export PACTICIPANT="${PACTICIPANT:-Billing Service}"'
- 'export DEPLOY_ENVIRONMENT="${DEPLOY_ENVIRONMENT:-production}"'
- 'export APIOME_LOAD_DOTENV=0'
- 'test -n "$PACT_BROKER_BASE_URL" || { echo "PACT_BROKER_BASE_URL is not set - add it as a secured repository variable" >&2; exit 1; }'
- 'test -n "$PACT_BROKER_TOKEN" || { echo "PACT_BROKER_TOKEN is not set - add it as a secured repository variable" >&2; exit 1; }'
- 'echo "deploy $BITBUCKET_COMMIT to $DEPLOY_ENVIRONMENT here, or add this step after your deploy step"'
- 'apiome pact record-deployment --pacticipant "$PACTICIPANT" --version "$BITBUCKET_COMMIT" --environment "$DEPLOY_ENVIRONMENT"'
pipelines:
pull-requests:
'**':
- step: *pact-publish
- step: *pact-can-i-deploy
branches:
main:
- step: *pact-publish
- step: *pact-can-i-deploy
- step: *pact-record-deployment
Jenkins and Azure Pipelines
These two pipelines run the same three steps, one stage or job each. Pull requests publish the
pacts and ask can-i-deploy; the main branch also records the deployment, and never from a pull
request. Neither runs in an image: each installs the CLI with the installer unless apiome is
already on the agent. can-i-deploy keeps its answer as the can-i-deploy.md build artifact (a
build-summary section on Azure). Both pipelines are run against a stub CLI on every change, so
they stay correct.
Jenkins
Copy the stages into the Jenkinsfile of a Multibranch Pipeline job, which supplies the
branch and the pull-request variables the stages read.
- Add a credential. In Manage Jenkins → Credentials, add a Secret text credential
with the id
apiome-pact-broker-tokenholding the API key (the Pact: consumer + deploy preset). - Set your values. Change
PACT_BROKER_BASE_URL,PACTICIPANTandDEPLOY_ENVIRONMENTto yours, and put your deploy stage before Record deployment.
The key stays in the credential binding, and the commands are single-quoted so the shell, not
Groovy, reads it. The version is GIT_COMMIT; a multibranch job for a pull request builds the
pull request's head commit unless the branch source is set to build the merge with the target.
Keep it on the head commit so the version is one that was published.
// Apiome Pact pipeline for Jenkins (PFX-4.6 / #6943).
//
// Copy the stages into your Jenkinsfile (a Multibranch Pipeline job). Pull requests publish the
// pacts and ask can-i-deploy; the main branch does the same, deploys, then records the deployment.
//
// Set up once:
// * a "Secret text" credential with id apiome-pact-broker-token holding an Apiome API key with
// the "Pact: consumer + deploy" preset;
// * PACT_BROKER_BASE_URL below, changed to your workspace's broker base URL.
//
// The agent needs curl and sh. Every apiome pact command exits 0 when it worked and 1 otherwise,
// so each stage fails on its own. Commands sit in single-quoted strings, so the shell, not
// Groovy, reads the variables and the key never lands in the Groovy log.
pipeline {
agent any
options {
timestamps()
disableConcurrentBuilds(abortPrevious: true)
}
environment {
PACT_BROKER_BASE_URL = 'https://api.apiome.dev/v1/tenants/<workspace>/pact-broker'
PACT_BROKER_TOKEN = credentials('apiome-pact-broker-token')
PACTICIPANT = 'Billing Service'
PACT_DIR = 'pacts'
DEPLOY_ENVIRONMENT = 'production'
APIOME_LOAD_DOTENV = '0'
PATH = "${env.HOME}/.local/bin:${env.PATH}"
}
stages {
stage('Install Apiome CLI') {
steps {
sh 'command -v apiome >/dev/null 2>&1 || { curl -fsSL https://get.apiome.dev -o get-apiome.sh && sh ./get-apiome.sh --user --no-modify-path; }'
}
}
stage('Publish pacts') {
steps {
sh 'for v in PACT_BROKER_BASE_URL PACT_BROKER_TOKEN; do test -n "$(printenv "$v")" || { echo "$v is not set - see the setup notes at the top of this file" >&2; exit 1; }; done'
sh 'apiome pact publish "$PACT_DIR" --consumer-app-version "$GIT_COMMIT" --branch "${CHANGE_BRANCH:-$BRANCH_NAME}" --build-url "$BUILD_URL"'
}
}
stage('Can I deploy?') {
steps {
sh 'for v in PACT_BROKER_BASE_URL PACT_BROKER_TOKEN; do test -n "$(printenv "$v")" || { echo "$v is not set - see the setup notes at the top of this file" >&2; exit 1; }; done'
sh 'apiome pact can-i-deploy --pacticipant "$PACTICIPANT" --version "$GIT_COMMIT" --to-environment "$DEPLOY_ENVIRONMENT" --retry-while-unknown 12 --retry-interval 10 --output markdown > can-i-deploy.md || gate=$?; cat can-i-deploy.md; exit ${gate:-0}'
}
post {
always {
archiveArtifacts artifacts: 'can-i-deploy.md', allowEmptyArchive: true
}
}
}
stage('Record deployment') {
// Never in a pull request: recording it would tell can-i-deploy an unmerged commit is live.
when {
allOf {
branch 'main'
not { changeRequest() }
}
}
steps {
sh 'for v in PACT_BROKER_BASE_URL PACT_BROKER_TOKEN; do test -n "$(printenv "$v")" || { echo "$v is not set - see the setup notes at the top of this file" >&2; exit 1; }; done'
sh 'echo "deploy $GIT_COMMIT to $DEPLOY_ENVIRONMENT here, or add your deploy stage before this one"'
sh 'apiome pact record-deployment --pacticipant "$PACTICIPANT" --version "$GIT_COMMIT" --environment "$DEPLOY_ENVIRONMENT"'
}
}
}
}
Azure Pipelines
Copy the jobs into azure-pipelines.yml.
- Add two variables. In Pipelines → Edit → Variables, add
PACT_BROKER_BASE_URLandPACT_BROKER_TOKEN, and tick Keep this value secret for the token. - Set your values. Change
PACTICIPANTandDEPLOY_ENVIRONMENTto yours, and put your deploy job before Record deployment. - Start pull-request runs. The
pr:trigger does this for GitHub and Bitbucket repositories. For Azure Repos, add the pipeline as Build validation on the branch policy.
Azure does not pass secret variables to scripts on its own, so each step maps the broker values
into its env. A variable that is not defined reaches the script as its own $(NAME) text; the
step treats that as missing and stops with the reason. On a pull request the version is the pull
request's head commit, not the merge commit Azure checks out.
# Apiome Pact pipeline for Azure Pipelines (PFX-4.6 / #6943).
#
# Copy the jobs into your azure-pipelines.yml. Pull requests publish the pacts and ask
# can-i-deploy; main does the same, deploys, then records the deployment. For a GitHub or
# Bitbucket repository the `pr:` trigger below starts the pull-request run; for Azure Repos add
# this pipeline as a build validation on the branch policy.
#
# Required pipeline variables (Pipelines -> Edit -> Variables):
# PACT_BROKER_BASE_URL https://api.apiome.dev/v1/tenants/<workspace>/pact-broker
# PACT_BROKER_TOKEN Apiome API key with the "Pact: consumer + deploy" preset - keep it secret
#
# Secret variables are not environment variables until a step maps them, so each step does.
# Every apiome pact command exits 0 when it worked and 1 otherwise, so each job fails on its own.
trigger:
branches:
include: [main]
pr:
branches:
include: ['*']
pool:
vmImage: ubuntu-latest
variables:
PACTICIPANT: Billing Service
PACT_DIR: pacts
DEPLOY_ENVIRONMENT: production
APIOME_LOAD_DOTENV: "0"
jobs:
- job: Publish
displayName: Publish pacts
steps:
- script: |
set -e
command -v apiome >/dev/null 2>&1 || { curl -fsSL https://get.apiome.dev -o get-apiome.sh && sh ./get-apiome.sh --user --no-modify-path; }
echo "##vso[task.prependpath]$HOME/.local/bin"
displayName: Install Apiome CLI
- script: |
set -euo pipefail
for v in PACT_BROKER_BASE_URL PACT_BROKER_TOKEN; do
case "$(printenv "$v")" in ''|'$('*) echo "$v is not set - add it as a pipeline variable" >&2; exit 1 ;; esac
done
branch="${SYSTEM_PULLREQUEST_SOURCEBRANCH:-$BUILD_SOURCEBRANCH}"
apiome pact publish "$PACT_DIR" --consumer-app-version "${SYSTEM_PULLREQUEST_SOURCECOMMITID:-$BUILD_SOURCEVERSION}" --branch "${branch#refs/heads/}" --build-url "${SYSTEM_COLLECTIONURI}${SYSTEM_TEAMPROJECT}/_build/results?buildId=${BUILD_BUILDID}"
displayName: Publish pacts
env:
PACT_BROKER_BASE_URL: $(PACT_BROKER_BASE_URL)
PACT_BROKER_TOKEN: $(PACT_BROKER_TOKEN)
- job: CanIDeploy
displayName: Pact can-i-deploy
dependsOn: Publish
steps:
- script: |
set -e
command -v apiome >/dev/null 2>&1 || { curl -fsSL https://get.apiome.dev -o get-apiome.sh && sh ./get-apiome.sh --user --no-modify-path; }
echo "##vso[task.prependpath]$HOME/.local/bin"
displayName: Install Apiome CLI
- script: |
set -euo pipefail
for v in PACT_BROKER_BASE_URL PACT_BROKER_TOKEN; do
case "$(printenv "$v")" in ''|'$('*) echo "$v is not set - add it as a pipeline variable" >&2; exit 1 ;; esac
done
apiome pact can-i-deploy --pacticipant "$PACTICIPANT" --version "${SYSTEM_PULLREQUEST_SOURCECOMMITID:-$BUILD_SOURCEVERSION}" --to-environment "$DEPLOY_ENVIRONMENT" --retry-while-unknown 12 --retry-interval 10 --output markdown > can-i-deploy.md || gate=$?
cat can-i-deploy.md
echo "##vso[task.uploadsummary]$PWD/can-i-deploy.md"
exit ${gate:-0}
displayName: Ask can-i-deploy
env:
PACT_BROKER_BASE_URL: $(PACT_BROKER_BASE_URL)
PACT_BROKER_TOKEN: $(PACT_BROKER_TOKEN)
- job: RecordDeployment
displayName: Record deployment
dependsOn: CanIDeploy
# Never in a pull request: recording it would tell can-i-deploy an unmerged commit is live.
condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'), ne(variables['Build.Reason'], 'PullRequest'))
steps:
- script: |
set -e
command -v apiome >/dev/null 2>&1 || { curl -fsSL https://get.apiome.dev -o get-apiome.sh && sh ./get-apiome.sh --user --no-modify-path; }
echo "##vso[task.prependpath]$HOME/.local/bin"
displayName: Install Apiome CLI
- script: |
set -euo pipefail
for v in PACT_BROKER_BASE_URL PACT_BROKER_TOKEN; do
case "$(printenv "$v")" in ''|'$('*) echo "$v is not set - add it as a pipeline variable" >&2; exit 1 ;; esac
done
echo "deploy $BUILD_SOURCEVERSION to $DEPLOY_ENVIRONMENT here, or add your deploy job before this one"
apiome pact record-deployment --pacticipant "$PACTICIPANT" --version "$BUILD_SOURCEVERSION" --environment "$DEPLOY_ENVIRONMENT"
displayName: Record deployment
env:
PACT_BROKER_BASE_URL: $(PACT_BROKER_BASE_URL)
PACT_BROKER_TOKEN: $(PACT_BROKER_TOKEN)
Smoke-test a provider build with Newman
A provider team that does not run a Pact verifier yet can still replay the consumer's requests. Export the pact as a Postman collection in the provider's build, and run it with Newman against the build:
# a step of the provider's build, after the provider is up on port 8080
- run: |
apiome pact export-postman --provider "Ledger API" --consumer "Billing Service" --branch main \
--provider-base-url http://localhost:8080 -o ledger.postman_collection.json
npx newman run ledger.postman_collection.json
env:
PACT_BROKER_BASE_URL: ${{ secrets.PACT_BROKER_BASE_URL }}
PACT_BROKER_TOKEN: ${{ secrets.PACT_BROKER_TOKEN }}
The provider must be in each request's provider states first. A Newman run does not publish a
verification result, so it does not count for can-i-deploy. Use
apiome pact verify for that. See
Export a pact as a Postman collection.
What fails a job
Every apiome pact command exits 0 when it worked and 1 otherwise, as pact-broker does —
except record, which exits with the tests' own code when they fail:
| The job fails when | The message says |
|---|---|
| The key is missing, or belongs to another workspace | which setting to give, or the server's refusal |
| The key's role lacks Pacts → View or Create | the missing permission |
| The key's scopes do not allow the call — a verifier key publishing a pact, for example | the key's scopes and the one required |
| A file is not a pact, or a flag is wrong | the file or flag and the reason |
can-i-deploy says no, or cannot decide in time | the reason for each integration |
verify fails or cannot replay everything | each failed pact and each interaction it could not replay |
record's tests fail (the job exits with their code), no call reached the mock, or the pact cannot be generated or published | the tests' output, or what to check and the server's refusal |
To try can-i-deploy without blocking deploys yet, add --dry-run: it prints the verdict and
exits 0.
Related
- The apiome pact commands — and the same checks as MCP tools for AI agents
- Export a pact as a Postman collection — the consumer's requests, with test scripts, for Postman or Newman
- Commit statuses — the verdict on each commit and pull request
- pact-action (GitHub Action) — every input and output
- Can I deploy?
- Environments and deployments
- CI contract gate on GitLab and Bitbucket — the OpenAPI diff gate, which runs next to these jobs