Skip to main content

4. Silver - manually integrate with Pact Broker

Now you have two different sets of tests in two different codebases. The artifacts that tie these tests together are the Pact file, and the verification results. The next step is to automate the exchange of these artifacts. At this level, you still run on your machine, but you have the consumer test publish its pact to the Pact Broker, and manually run provider verification where it gets the latest pact from the broker.

Silver diagram

Silver diagram

The Pact Broker is a service that allows your projects to exchange pacts and verification results in an automated way.

While you can use Pact without a Pact Broker, using one allows you to get the most out of Pact. Without the Broker, you will have to work out how to create your own feedback loop that lets you know the results of the pact verifications, and your pacts will not enable to you release your services independently and safely using the can-i-deploy tool (more on this later).

Sharing the contracts with the provider team via a broker

Sharing is caring

Now that you have created and run your consumer tests, producing a contract (the pact file) as an artefact. You've shared it with the team responsible for managing the Order API. They can confirm they meet all of the expectations set in it.

There are multiple ways to share pacts, but the recommended approach is to use a Pact Broker as it enables powerful automation workflows.

There are two flavours.

  • The Pact Broker is an open source tool that requires you to deploy, administer and host it yourself.
  • The Pactflow Broker is plug-and-play option, that we've created Pactflow, a fully managed Pact Broker with additional features to simplify teams getting started and scaling with Pact.

To get started for free, you can sign up to our Developer Plan here.

We will get started with the Open source broker, at this point in the demo, and once you have a full workflow, we will get you setup with a free hosted Pact Broker from pactflow.io

Quick Start with Pactflow

The Pact Broker is an open source tool that requires you to deploy, administer and host it yourself. If you would prefer a plug-and-play option, we've created Pactflow, a fully managed Pact Broker with additional features to simplify teams getting started and scaling with Pact.

Get started for free on our Developer Plan here.

Verifying pacts

There are two reasons that a verification task will need to be run:

  1. When the provider code changes
  • The verification task will run as part of the normal CI/CD pipeline for the provider
  • The verification task will fetch and verify all the relevant pacts from all consumers from the Pact Broker to ensure no regressions have occurred.
  1. When the pact changes
  • We recommend that a separate pipeline is set up to verify just the changed pact (more details on this later in the guide).

Consumer pipeline

The following diagram shows what a fully fledged Pact supported release pipeline might look like for a consumer. During this step of the set up guide, we are just going to cover publishing the pact. The other parts of this pipeline will be covered in later steps.

Consumer release pipeline

Consumer release pipeline

Provider pipeline

The following diagram shows what a fully fledged Pact supported release pipeline might look like for a provider. During this step of the set up guide, we are just going to cover the pact verification and results publication. The other parts of this pipeline will be covered in later steps.

Provider release pipeline

Provider release pipeline

Tasks

A. Set up a Pact Broker

  1. Read the Pact Broker home page, (taking note of the various deployment options available to you in the Usage section), or sign up to a hosted Developer plan at Pactflow.
  2. Deploy a Pact Broker to a network that has access to both consumer and provider CI systems so it can trigger builds.

Useful links:

For each of the next steps, you will need to set the following variables to your projects

  • PACT_BROKER_BASE_URL
  • PACT_BROKER_USERNAME # Pact Broker Only
  • PACT_BROKER_PASSWORD # Pact Broker Only
  • PACT_BROKER_TOKEN # Pactflow Broker Only

Copy this file and run docker-compose up -d, once it is running you can visit it at http://localhost:8000 and login with pact_workshop/pact_workshop

Step_03_GetAPactBroker/docker-compose.yml
loading...

B. Manually publish the pact to the Pact Broker

Now that things in are a fairly stable state, you can start taking advantage of the Pact Broker to manage the communication between your provider and consumer.

You can read more about publishing and retrieving pacts from a Pact Broker here

If you are using Pactflow - see https://docs.pactflow.io/#consumer for how to configure your consumer in each respective language implementation

Debug this until it works and looks good.

C. Manually verify using the Pact Broker

Now you can see if you can run your provider tests, this time pulling the pact file not from your local filesystem, but from the broker. Reconfigure your provider project to get the latest pact for each of its consumers from the broker.

If you are using Pactflow - see https://docs.pactflow.io/#provider for how to configure your provider in each respective language implementation

See folder ./Step_05_VerifyYourFirstPactFromABrokerLocally

Once your pact is uploaded, grab the pact file URL.

run you test with PACT_URL=your_url npm run test:provider

We will setup this task to run later via a webhook, so that it runs when the consumer pact file changes. We can publish our results back to the broker.

However we would normally do that via CI, you can trigger it by setting an env var

PACT_URL=your_url PACT_BROKER_PUBLISH_VERIFICATION_RESULTS=true npm run test:provider

Step_05_VerifyYourFirstPactFromABrokerLocally/provider/provider.spec.js
loading...

As verification results can affect things, lets run it from CI instead, so every time we make a change in our codebase, we can upload our pacts to the broker, and verify them