Publishing Contracts
Publishing the Consumer Contract to Swagger Contract Testing
Publishing a consumer contract is the same as publishing a regular Pact. For details on publishing pact files to Swagger Contract Testing, refer to Sharing Pacts with the Pact Broker.
Publishing the Provider Contract + Results to Swagger Contract Testing
The provider contract can be an OpenAPI definition (for HTTP APIs) or an AsyncAPI definition (for event-driven APIs). There are several ways Contract Testing can be used to publish contracts.
The links in the list will take you to the respective documentation in each repo.
Otherwise, see the installation and usage instructions.
Installation
Docker
The Pact Broker CLI is packaged with the other Ruby command line tools in the pactfoundation/pact-cli Docker image.
docker pull pactfoundation/pact-cli
Pact Standalone CLI
Download the latest pact-ruby-standalone package.
Available from version
v1.89.00and upwardsInstallation instructions are provided in the above link, for
windows/macos/linuxYou do not need Ruby to run the CLI, as the Ruby runtime is packaged with the executable using Travelling Ruby.
windows (bash shell)
curl -LO https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v1.89.00/pact-1.89.00-win32.zip && \
unzip pact-1.89.00-win32.zip && \
./pact/bin/pactflow.bat helpMacOS
curl -LO https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v1.89.00/pact-1.89.00-osx.tar.gz && \
tar xzf pact-1.89.00-osx.tar.gz && \
./pact/bin/pactflow helpLinux
curl -LO https://github.com/pact-foundation/pact-ruby-standalone/releases/download/v1.89.00/pact-1.89.00-linux-x86_64.tar.gz && \
tar xzf pact-1.89.00-linux-x86_64.tar.gz && \
./pact/bin/pactflow helpPact Broker Client (Ruby)
Either
Add
gem 'pact_broker-client'to your Gemfile and runbundle installInstall the gem directly by running
gem install pact_broker-client
GitHub Actions
Copy and paste the following snippet into your .yml file.
- name: publish-provider-contract action uses: pactflow/actions/publish-provider-contract@v0.0.2
Publishing
Usage
Usage:
pactflow publish-provider-contract CONTRACT_FILE ... --provider=PROVIDER -a,
--provider-app-version=PROVIDER_APP_VERSION -b, --broker-base-url=BROKER_BASE_URL
Options:
--provider=PROVIDER
# The provider name
-a, --provider-app-version=PROVIDER_APP_VERSION
# The provider application version
-h, [--branch=BRANCH]
# Repository branch of the provider version
-t, [--tag=TAG]
# Tag name for provider version. Can be specified multiple
times.
[--specification=SPECIFICATION]
# The contract specification
# Default: oas
[--content-type=CONTENT_TYPE]
# The content type. eg. application/yml
[--verification-success], [--no-verification-success]
# Whether or not the self verification passed successfully.
[--verification-exit-code=N]
# The exit code of the verification process. Can be used instead
of --verification-success|--no-verification-success for a
simpler build script.
[--verification-results=VERIFICATION_RESULTS]
# The path to the file containing the output from the
verification process
[--verification-results-content-type=VERIFICATION_RESULTS_CONTENT_TYPE]
# The content type of the verification output eg. text/plain,
application/yaml
[--verification-results-format=VERIFICATION_RESULTS_FORMAT]
# The format of the verification output eg. junit, text
[--verifier=VERIFIER]
# The tool used to verify the provider contract
[--verifier-version=VERIFIER_VERSION]
# The version of the tool used to verify the provider contract
[--build-url=BUILD_URL]
# The build URL that created the provider contract
-o, [--output=OUTPUT]
# json or text
# Default: text
-b, --broker-base-url=BROKER_BASE_URL
# The base URL of the Pact Broker
-u, [--broker-username=BROKER_USERNAME]
# Pact Broker basic auth username
-p, [--broker-password=BROKER_PASSWORD]
# Pact Broker basic auth password
-k, [--broker-token=BROKER_TOKEN]
# Pact Broker bearer token
-v, [--verbose], [--no-verbose]
# Verbose output. Default: falseDocker
docker run --rm -v /${PWD}:/${PWD} -w ${PWD} \
-e PACT_BROKER_BASE_URL \
-e PACT_BROKER_TOKEN \
pactfoundation/pact-cli:0.50.0.28 \
pactflow publish-provider-contract \
oas/swagger.yml \
--provider "pactflow-example-bi-directional-provider-postman" \
--provider-app-version 3a0994c \
--branch test-pactflow-command \
--content-type application/yaml \
--verification-exit-code=0 \
--verification-results newman/newman-run-report-2022-06-09-14-18-33-406-0.json \
--verification-results-content-type text/plain \
--verifier postmanTo publish an AsyncAPI definition, add --specification asyncapi.
Pact Standalone CLI
./pact/bin/pactflow publish-provider-contract \
oas/swagger.yml \
--provider "pactflow-example-bi-directional-provider-postman" \
--provider-app-version 3a0994c \
--branch test-pactflow-command \
--content-type application/yaml \
--verification-exit-code=0 \
--verification-results newman/newman-run-report-2022-06-09-14-03-30-715-0.json \
--verification-results-content-type text/plain \
--verifier postmanTo publish an AsyncAPI definition, add --specification asyncapi.
Pact Broker Client (Ruby)
GitHub Actions
# (This just saves defining these multiple times for different pact jobs)
env:
version: "1.2.3"
application_name: "my-api-provider"
pact_broker: ${{ secrets.PACT_BROKER_BASE_URL }}
pact_broker_token: ${{ secrets.PACT_BROKER_TOKEN }}
jobs:
pact-publish-oas-action:
steps:
- uses: actions/checkout@v3 # MANDATORY: Must use 'checkout' first
- uses: pactflow/actions/publish-provider-contract@v0.0.2
env:
oas_file: oas/swagger.yml
results_file: ${{ env.results_file }}To publish an AsyncAPI definition, set the contract file to your AsyncAPI definition and set the specification: asyncapi.
AsyncAPI Publishing Flags
When publishing an AsyncAPI definition, use the following flags:
--specification asyncapi- tells Contract Testing this is an AsyncAPI definition--content-type application/yaml- use for YAML-formatted definitions--content-type application/json- use for JSON-formatted definitions--verifier "tool-name"- the name of your verification tool (for example, "my-event-verifier")
The --specification asyncapi flag is required for AsyncAPI definitions; when omitted, the specification defaults to oas (OpenAPI).