9. Deploy your consumer to Production
Important
If you are running in github actions, we will do the publish and deploy step all in one here, as we are running our example from the master/main branch.
Now that we have tested our consumer and published our consumer contract, we can deploy the application to production.
Just like our provider counterpart, we're going to call can-i-deploy
to check if it's safe before we do.
REMEMBER: The can-i-deploy
command is an important part of a CI/CD workflow, adding stage gates to prevent deploying incompatible applications to environments such as production.
This diagram shows an illustrative CI/CD pipeline as it relates to our progress to date:
Deploying the consumer build to Production locally
Let's run the command:
npm run can-i-deploy
This should pass, because the provider has already pulbished its contract and deployed to production, and we believe the consumer is compatible with the provider OAS:
$ npx pact-broker can-i-deploy --pacticipant pactflow-example-consumer-mountebank --version $GIT_COMMIT --to-environment production Computer says yes \o/ CONSUMER | C.VERSION | PROVIDER | P.VERSION | SUCCESS? | RESULT# -------------------------------------|-----------|---------------------------------|-----------|----------|-------- pactflow-example-consumer-mountebank | 5009e94 | pactflow-example-bi-directional-provider-dredd | 6559541 | true | 1 VERIFICATION RESULTS -------------------- 1. https://test.pactflow.io/hal-browser/browser.html#https://test.pactflow.io/contracts/provider/pactflow-example-bi-directional-provider-dredd/version/6559541/consumer/pactflow-example-consumer-mountebank/pact-version/ce2a9dfed28309e26288b9c9333529c92762d36a/verification-results (success) All required verification results are published and successful
We can now deploy our consumer to production. Once we have deployed, we let API Hub for Contract Testing know that the new version of the consumer has been promoted to that environment:
npm run deploy
This allows API Hub for Contract Testing to prevent any providers from deploying an incompatible change to production
.
Check
Your dashboard should look something like this, where both your consumer and consumer are marked as having been deployed to production
.
Deploying the consumer build in Github Actions to Production
Configure consumer pipeline
The source repositories are configured to use the the public broker at test.pactflow.io. You will need to update the credentials to point to your own API Hub for Contract Testing account. To do this, we need to update the PACT_BROKER_BASE_URL
environment variable in the Github workflow file, and create a Github Secret to store the API Hub for Contract Testing API token in.
Create a Github Secret to store your API Hub for Contract Testing API token in.
In API Hub for Contract Testing:
Log in to your API Hub for Contract Testing account (
https://<your-subdomain>.pactflow.io
), and go to Settings > API Tokens.Click the Copy button for the read/write CI token (make sure it's the read write one, not the read only one).
In Github:
Open your forked
example-bi-directional-consumer-<tool>
project (https://github.com/<your-username>/example-bi-directional-consumer-<tool>
).Click on the
Settings
tab.Select
Secrets
from the side menu.Click
New repository secret
(the button is to the right of the "Actions secrets" heading).Set the name of the secret to
PACTFLOW_TOKEN_FOR_CI_CD_WORKSHOP
.Paste in the API Hub for Contract Testing API token value you copied in the previous step.
Update your workflow files in GitHub to point at your API Hub for Contract Testing Broker
In API Hub for Contract Testing:
Go to Settings > API Tokens.
Click the
COPY PACTFLOW BASE URL
button.
In Github:
Open your forked
example-bi-directional-consumer-<tool>
projecthttps://github.com/<your-username>/example-bi-directional-consumer-<tool>
.Open
.github/workflows/build.yml
.In the upper right corner of the file view, click 🖊️ to open the file editor.
Update the value of
PACT_BROKER_BASE_URL
to the base URL of your own API Hub for Contract Testing account. You can easily get this by clicking the COPY PACTFLOW BASE URL button on the API Tokens page in API Hub for Contract Testing.Press the green
Commit changes
button.
View the build:
In Github:
Select the most recent build, this will have been triggered when you committed the changes in the last page.
Important
This build should now successfully publish the pact, and it will pass on the can-i-deploy
step before it tries to deploy. This is because the provider and consumer has been checked to ensure they are cross-compatible.
Expected state by the end of this step
The consumer build is passing, and it is deployed to production ✅.