Hello, is there recommendation for using pact in a...
# pact-jvm
t
Hello, is there recommendation for using pact in a monorepo in which there are many projects interacting with each other through REST API ? Can a single can-i-deploy at a specific commit of the monorepo tell if all consumers and providers are compatible?
m
You'd need to identify each logical application within the mono repo. Pact can't magically know that your project is a mono repo or not
Hmmm. I wonder if a commit Sha could be used to determine that in the future? Cc: @Beth (pactflow.io/Pact Broker/pact-ruby)
b
You can enter all the applications in the monorepo in a single can-i-deploy command. I’ve done a demo here.
Copy code
pact-broker can-i-deploy --pacticipant monorepo-consumer-1 --version 1 --pacticipant monorepo-consumer-2 --version 1 --to-environment production
Computer says no ¯_(ツ)_/¯
#
CONSUMER            | C.VERSION | PROVIDER   | P.VERSION | SUCCESS? | RESULT#
--------------------|-----------|------------|-----------|----------|--------
monorepo-consumer-1 | 1         | provider-1 | 1         | false    | 1
monorepo-consumer-2 | 1         | provider-2 | 1         | true     | 2
#
VERIFICATION RESULTS
--------------------
1. <http://localhost:9292/pacts/provider/provider-1/consumer/monorepo-consumer-1/pact-version/6e0b1c114e7b8a4775ff584af54bac734408e31a/metadata/Y3ZuPTE/verification-results/109> (failure)
2. <http://localhost:9292/pacts/provider/provider-2/consumer/monorepo-consumer-2/pact-version/dd1edab05d6a9f4ff7dc4f17dbf3ea585c8220cb/metadata/Y3ZuPTE/verification-results/110> (success)
#
The verification for the pact between version 1 of monorepo-consumer-1 and the version of provider-1 currently deployed or released to production (1) failed
The
--to-environment
flag ensures that you are backwards compatible with all the applications that are already deployed in that environment. It may get confused if you have a bi-directional dependency between two apps within the monorepo, and each has a change that depends on the other. In that case, you may want to list each dependency explicitly and not use the --to-environment.
ugh, actually, you currently only can specify branch, tag, or version number in the explicit selectors. but it wouldn’t be much work to support:
Copy code
pact-broker can-i-deploy \
  --pacticipant monorepo-consumer-1 --version 1 \
  --pacticipant monorepo-consumer-2 --version 1 \
  --pacticipant provider-1 --environment production \
  --pacticipant provider-2 --environment production
m
Yeah, so I was suggesting/asking if we could potentially infer all of those by the virtue the git sha being identical.
b
theoretically yes.
👍 1
but currently, no.
1
Ok, I’ve tested the monorepo with bi-directional dependencies between the monorepo apps, and it works as you would hope https://github.com/pact-foundation/pact_broker/blob/master/script/data/monorepo-bi-directional.rb
So, my advice is to use this format
pact-broker can-i-deploy --pacticipant monorepo-app-1 --version 2 --pacticipant monorepo-app-2 --version 2 --to-environment production
you still need to explicitly name each app in the monorepo, but that shouldn’t change very often.
👍 1
t
Thank you very much. I am still confuse about the environment. Let say I have 5 applications in my monorepo con1, con2, con3 and prov1, prov2. All consumer apps uses both prov1 and prov2. Here is the 2 questions: 1/When publishing pact file from consumer test to pact broker, how can i specify the environment on the maven command line? I tried to set the environment in the pom like the screenshot below but i dont see any environment in the pact broker UI. 2/Now assuming that i have all the pact files uploaded to pact broker, i still need to list all the pacticipants in the can-i-deploy to make sure they all compatible, why do i need to specify --to-environment?
b
1. you don’t specify the environment when you publish the pacts. you specify it when you record the deployment using the pact cli. 2. if you only want to check if the monorepo apps are compatible with each other, you don’t need the --to-environment. If you are deploying the monorepo to an an envrionment where they also talk to other applications outside the monorepo apps, you do need the --to-environment.
I think it probably wouldn’t hurt to put the --to-environment in there anyway, because it means that if you add another dependency later on, it will be automatically added to the command (if that’s what you want).
t
You also have to be careful in the order you run commands. I have an application that is its own consumer, and I have to run the pact tests first, and then publish the pact, before I can run the verification.
b
meta
t
thank you for the link to the record deployment, I need to add that to the CI/CD deployment job