Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
chore: use tags (legacy), prefix pacticipant names
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F authored Oct 5, 2022
1 parent 7ce8bc0 commit 7d93354
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 92 deletions.
33 changes: 0 additions & 33 deletions .github/workflows/contract_requiring_verification_published.yml

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/trigger_partner_docs_update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ on:

jobs:
run:
if: ${{ github.repository == 'pactflow/example-provider' }}
if: ${{ github.repository == 'pactflow/example-provider-legacy' }}
runs-on: ubuntu-latest
steps:
- name: Trigger docs.pactflow.io update workflow
run: |
curl -X POST https://api.github.com/repos/pactflow/docs.pactflow.io/dispatches \
-H 'Accept: application/vnd.github.everest-preview+json' \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-d '{"event_type": "pactflow-example-provider-js-updated"}'
-d '{"event_type": "pactflow-example-provider-legacy-js-updated"}'
env:
GITHUB_TOKEN: ${{ secrets.GHTOKENFORTRIGGERINGPACTDOCSUPDATE }}
- name: Trigger partners.pact.io update workflow
run: |
curl -X POST https://api.github.com/repos/pactflow/partners.pactflow.io/dispatches \
-H 'Accept: application/vnd.github.everest-preview+json' \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-d '{"event_type": "pactflow-example-provider-js-updated"}'
-d '{"event_type": "pactflow-example-provider-legacy-js-updated"}'
env:
GITHUB_TOKEN: ${{ secrets.GHTOKENFORTRIGGERINGPACTDOCSUPDATE }}
20 changes: 2 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
PACTICIPANT := "pactflow-example-provider"
GITHUB_REPO := "pactflow/example-provider"
PACTICIPANT := "pactflow-example-provider-legacy"
GITHUB_REPO := "pactflow/example-provider-legacy"
PACT_CHANGED_WEBHOOK_UUID := "c76b601e-d66a-4eb1-88a4-6ebc50c0df8b"
CONTRACT_REQUIRING_VERIFICATION_PUBLISHED_WEBHOOK_UUID := "8ce63439-6b70-4e9b-8891-703d5ea2953c"
PACT_CLI="docker run --rm -v ${PWD}:${PWD} -e PACT_BROKER_BASE_URL -e PACT_BROKER_TOKEN pactfoundation/pact-cli"

# Only deploy from master
Expand Down Expand Up @@ -93,21 +92,6 @@ create_or_update_pact_changed_webhook:
test_pact_changed_webhook:
@curl -v -X POST ${PACT_BROKER_BASE_URL}/webhooks/${PACT_CHANGED_WEBHOOK_UUID}/execute -H "Authorization: Bearer ${PACT_BROKER_TOKEN}"

create_or_update_contract_requiring_verification_published_webhook:
"${PACT_CLI}" \
broker create-or-update-webhook \
"https://api.github.com/repos/${GITHUB_REPO}/dispatches" \
--header 'Content-Type: application/json' 'Accept: application/vnd.github.everest-preview+json' 'Authorization: Bearer $${user.githubToken}' \
--request POST \
--data '{ "event_type": "contract_requiring_verification_published","client_payload": { "pact_url": "$${pactbroker.pactUrl}", "sha": "$${pactbroker.providerVersionNumber}", "branch":"$${pactbroker.providerVersionBranch}" , "message": "Verify changed pact for $${pactbroker.consumerName} version $${pactbroker.consumerVersionNumber} branch $${pactbroker.consumerVersionBranch} by $${pactbroker.providerVersionNumber} ($${pactbroker.providerVersionDescriptions})" } }' \
--uuid ${CONTRACT_REQUIRING_VERIFICATION_PUBLISHED_WEBHOOK_UUID} \
--provider ${PACTICIPANT} \
--contract-requiring-verification-published \
--description "contract_requiring_verification_published for ${PACTICIPANT}"

test_contract_requiring_verification_published_webhook:
@curl -v -X POST ${PACT_BROKER_BASE_URL}/webhooks/${CONTRACT_REQUIRING_VERIFICATION_PUBLISHED_WEBHOOK_UUID}/execute -H "Authorization: Bearer ${PACT_BROKER_TOKEN}"

## ======================
## Misc
## ======================
Expand Down
32 changes: 8 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Example Provider

[![Build Status](https://github.com/pactflow/example-provider/actions/workflows/build.yml/badge.svg)](https://github.com/pactflow/example-provider/actions)
[![Build Status](https://github.com/pactflow/example-provider-legacy/actions/workflows/build.yml/badge.svg)](https://github.com/pactflow/example-provider-legacy/actions)

[![Can I deploy Status](https://test.pactflow.io/pacticipants/pactflow-example-provider/branches/master/latest-version/can-i-deploy/to-environment/production/badge)](https://test.pactflow.io/pacticipants/pactflow-example-provider/branches/master/latest-version/can-i-deploy/to-environment/production/badge)
[![Can I deploy Status](https://test.pactflow.io/pacticipants/pactflow-example-provider-legacy/branches/master/latest-version/can-i-deploy/to-environment/production/badge)](https://test.pactflow.io/pacticipants/pactflow-example-provider-legacy/branches/master/latest-version/can-i-deploy/to-environment/production/badge)

[![pactflow-example-provider/pactflow-example-consumer](https://test.pactflow.io/pacts/provider/pactflow-example-provider/consumer/pactflow-example-consumer/latest/master/badge.svg)](https://test.pactflow.io/pacts/provider/pactflow-example-provider/consumer/pactflow-example-consumer/latest/master)
[![pactflow-example-provider-legacy/pactflow-example-consumer-legacy](https://test.pactflow.io/pacts/provider/pactflow-example-provider-legacy/consumer/pactflow-example-consumer-legacy/latest/master/badge.svg)](https://test.pactflow.io/pacts/provider/pactflow-example-provider-legacy/consumer/pactflow-example-consumer-legacy/latest/master)

This is an example of a Node provider that uses Pact, [Pactflow](https://pactflow.io) and Github Actions to ensure that it is compatible with the expectations its consumers have of it.

It is using a public tenant on Pactflow, which you can access [here](https://test.pactflow.io/) using the credentials `dXfltyFMgNOFZAxr8io9wJ37iUpY42M`/`O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1`. The latest version of the Example Pactflow Consumer/Example Pactflow Provider pact is published [here](https://test.pactflow.io/overview/provider/pactflow-example-provider/consumer/pactflow-example-consumer).
It is using a public tenant on Pactflow, which you can access [here](https://test.pactflow.io/) using the credentials `dXfltyFMgNOFZAxr8io9wJ37iUpY42M`/`O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1`. The latest version of the Example Pactflow Consumer/Example Pactflow Provider pact is published [here](https://test.pactflow.io/overview/provider/pactflow-example-provider-legacy/consumer/pactflow-example-consumer-legacy).

## Pact verifications

Expand All @@ -26,26 +26,11 @@ To ensure that a verification is also run whenever a pact changes, we create a w
* In [.github/workflows/build.yml](.github/workflows/build.yml)
* Our PACT_BROKER_TOKEN environment variable is set from a Github Secret. This is a read/write token. For normal development use, you would use a read only token, as you would not be publishing verification results from your local machine.

* In [.github/workflows/contract_requiring_verification_published.yml](.github/workflows/contract_requiring_verification_published.yml)
* Recommended - This build is triggered by a webhook event, when a pact file changes and passes the url of the changed pact to the provider pact test to verify, as well as the branch and commit sha and checks out the provider codebase on the specified sha. See [here](https://docs.pact.io/pact_broker/webhooks#the-contract-requiring-verification-published-event) for details on the `contract_requiring_verification_published` webhook

* In [.github/workflows/verify_changed_pact.yml](.github/workflows/verify_changed_pact.yml)
* Superceded by above - This build is triggered by a webhook event, when a pact file changes and passes the url of the changed pact to the provider pact test to verify. See [here](hhttps://docs.pact.io/pact_broker/webhooks#the-contract-published-event) for details on the `contract_content_changed` webhook
* This build is triggered by a webhook event, when a pact file changes and passes the url of the changed pact to the provider pact test to verify. See [here](hhttps://docs.pact.io/pact_broker/webhooks#the-contract-published-event) for details on the `contract_content_changed` webhook

* In the [Makefile](Makefile):
* Recommended - Using the [contract requiring verification published](https://docs.pact.io/pact_broker/webhooks#the-contract-requiring-verification-published-event) event
* The target `create_or_update_contract_requiring_verification_published_webhook` creates the Pactflow webhook that will trigger a build of the provider when any of its consumers publishes a pact with changed content.
* To call the Github API that triggers the build, the webhook uses a bearer token that is stored in a Pactflow secret called `${user.githubToken}`. The secret can be created using the `create_github_token_secret` target, or through the Pactflow UI.
* The target `ci` runs when the provider has pushed a new commit. It performs the following tasks:
* Run the isolated tests (including the pact verification tests, which publish the verification results)
* If we are on master:
* Check if we are safe to deploy to prod using `can-i-deploy` (ie. do we have a succesfully verified pact with the version of the consumer that is currently in production)
* Deploy (just pretend!)
* Record the deployed application version in Pactflow so Pactflow knows which version of the provider is in production when the consumer runs `can-i-deploy`.
* The target `ci_webhook` just runs the pact verification step, and is used when the build is triggered by the webhook.
* The target `create_or_update_contract_requiring_verification_published_webhook` creates the Pactflow webhook that will trigger a build of the provider when any of its consumers publishes a pact with changed content.
* To call the Github API that triggers the build, the webhook uses a bearer token that is stored in a Pactflow secret called `${user.githubToken}`. The secret can be created using the `create_github_token_secret` target, or through the Pactflow UI.
* Superceded by above - Using the [contract content changed](https://docs.pact.io/pact_broker/webhooks#the-contract-content-changed-event) event
* Using the [contract content changed](https://docs.pact.io/pact_broker/webhooks#the-contract-content-changed-event) event
* The target `create_or_update_pact_changed_webhook` creates the Pactflow webhook that will trigger a build of the provider when any of its consumers publishes a pact with changed content.
* To call the Github API that triggers the build, the webhook uses a bearer token that is stored in a Pactflow secret called `${user.githubToken}`. The secret can be created using the `create_github_token_secret` target, or through the Pactflow UI.
* The target `ci` runs when the provider has pushed a new commit. It performs the following tasks:
Expand All @@ -59,11 +44,10 @@ To ensure that a verification is also run whenever a pact changes, we create a w
* To call the Github API that triggers the build, the webhook uses a bearer token that is stored in a Pactflow secret called `${user.githubToken}`. The secret can be created using the `create_github_token_secret` target, or through the Pactflow UI.

* In [src/product/product.pact.test.js](src/product/product.pact.test.js):
* When the `$PACT_URL` is not set (ie. the build is running because the provider changed), the provider is configured to fetch all the pacts for the 'example-provider' provider which belong to the latest consumer versions tagged with `master` and those currently deployed to an environment. This ensures the provider is compatible with the latest changes that the consumer has made, and is also backwards compatible with the production/test versions of the consumer.
* When the `$PACT_URL` is not set (ie. the build is running because the provider changed), the provider is configured to fetch all the pacts for the 'example-provider-legacy' provider which belong to the latest consumer versions tagged with `master` and those currently deployed to an environment. This ensures the provider is compatible with the latest changes that the consumer has made, and is also backwards compatible with the production/test versions of the consumer.
* When the `$PACT_URL` is set (ie. the build is running because it was triggered by the 'contract content changed' webhook), we just verify the pact at the `$PACT_URL`.
* Pact-JS has a very flexible verification task configuration that allows us to use the same code for both the main pipeline verifications and the webhook-triggered verifications, with dynamically set options. Depending on your pact implementation, you may need to define separate tasks for each of these concerns.
* When the verification results are published, the provider version number is set to the git sha, and the provider version branch is the git branch name (recommended) or provider version tag is the git branch name (superceded by branch property). You can read more about versioning [here](https://docs.pact.io/getting_started/versioning_in_the_pact_broker).
* Recommended - The `providerVersionBranch` property is set, so that verification results are published with the branch property which is important for the [contract requiring verification published](https://docs.pact.io/pact_broker/webhooks#the-contract-requiring-verification-published-event) event
* When the verification results are published, the provider version number is set to the git sha, and the provider version tag is the git branch name. You can read more about versioning [here](https://docs.pact.io/getting_started/versioning_in_the_pact_broker).

## Usage

Expand Down
25 changes: 11 additions & 14 deletions src/product/product.pact.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,30 @@ describe("Pact Verification", () => {
logLevel: "INFO",
providerBaseUrl: "http://localhost:8080",
providerVersion: process.env.GIT_COMMIT,
// providerVersionTags: process.env.GIT_BRANCH ? [process.env.GIT_BRANCH] : [], // the old way of publishing verification results with the tag property
providerVersionBranch: process.env.GIT_BRANCH
? process.env.GIT_BRANCH
: "", // the recommended way of publishing verification results with the branch property
providerVersionTags: process.env.GIT_BRANCH ? [process.env.GIT_BRANCH] : [], // the old way of publishing verification results with the tag property
// providerVersionBranch: process.env.GIT_BRANCH
// ? process.env.GIT_BRANCH
// : "", // the recommended way of publishing verification results with the branch property
verbose: process.env.VERBOSE === "true",
};

// For builds triggered by a 'contract_content_changed' just verify the changed pact.
// https://docs.pact.io/pact_broker/webhooks#the-contract-content-changed-event
// For builds trigged by a 'contract_requiring_verification_published' webhook, verify the changed pact against latest of mainBranch and any version currently deployed to an environment
// https://docs.pact.io/pact_broker/webhooks#using-webhooks-with-the-contract_requiring_verification_published-event
// The URL will bave been passed in from the webhook to the CI job.
const pactChangedOpts = {
pactUrls: [process.env.PACT_URL],
};

// For 'normal' provider builds, fetch the the latest version from the main branch of each consumer, as specified by
// the consumer's mainBranch property and all the currently deployed and currently released and supported versions of each consumer.
// the consumer's selectors branch property and all the currently deployed of each consumer.
// https://docs.pact.io/pact_broker/advanced_topics/consumer_version_selectors
const fetchPactsDynamicallyOpts = {
provider: "pactflow-example-provider",
//consumerVersionTag: ['master', 'prod'], // the old way of specifying which pacts to verify if using tags
// consumerVersionSelectors: [{ tag: 'master', latest: true }, { deployed: true } ], // the newer way of specifying which pacts to verify if using tags
consumerVersionSelectors: [
{ mainBranch: true },
{ deployed: true },
], // the new way of specifying which pacts to verify if using branches (recommended)
provider: "pactflow-example-provider-legacy",
consumerVersionSelectors: [{ tag: 'master', latest: true }, { deployed: true } ], // the way of specifying which pacts to verify if using tags
// consumerVersionSelectors: [
// { mainBranch: true },
// { deployed: true },
// ], // the new way of specifying which pacts to verify if using branches (recommended)
pactBrokerUrl: process.env.PACT_BROKER_BASE_URL,
enablePending: false,
includeWipPactsSince: undefined,
Expand Down

0 comments on commit 7d93354

Please sign in to comment.