Giter Club home page Giter Club logo

example-provider's Introduction

Example Provider

Build Status

This is an example of a Node provider that uses Pact, Pactflow 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 using the credentials dXfltyFMgNOFZAxr8io9wJ37iUpY42M/O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1. The latest version of the Example Consumer/Example Provider pact is published here.

Pact verifications

When using Pact in a CI/CD pipeline, there are two reasons for a pact verification task to take place:

  • When the provider changes (to make sure it does not break any existing consumer expectations)
  • When a pact changes (to see if the provider is compatible with the new expectations)

When the provider changes, the pact verification task runs as part the provider's normal build pipeline, generally after the unit tests, and before any deployment takes place. This pact verification task is configured to dynamically fetch all the relevant pacts for the specified provider from Pactflow, verify them, and publish the results back to Pactflow.

To ensure that a verification is also run whenever a pact changes, we create a webhook in Pactflow that triggers a provider build, and passes in the URL of the changed pact. Ideally, this would be a completely separate build from your normal provider pipeline, and it should just verify the changed pact.

Features

  • In .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 the Makefile:

    • The target create_or_update_travis_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!)
        • Tag the deployed application version as prod 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.
  • In 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 prod. This ensures the provider is compatible with the latest changes that the consumer has made, and is also backwards compatible with the production version 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 tag is the git branch name. You can read more about versioning here.

Usage

See the Pactflow CI/CD Workshop.

example-provider's People

Contributors

bethesque avatar mefellows avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.