Skip to content

Commit 05ae44c

Browse files
committed
feat(can-i-deploy): add --to-environment option
1 parent 0fcaf04 commit 05ae44c

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

lib/pact_broker/client/cli/broker.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Broker < CustomThor
1818
method_option :version, required: false, aliases: "-e", desc: "The pacticipant version. Must be entered after the --pacticipant that it relates to."
1919
method_option :latest, required: false, aliases: "-l", banner: '[TAG]', desc: "Use the latest pacticipant version. Optionally specify a TAG to use the latest version with the specified tag."
2020
method_option :to, required: false, banner: 'TAG', desc: "This is too hard to explain in a short sentence. Look at the examples.", default: nil
21+
method_option :to_environment, required: false, banner: 'ENVIRONMENT', desc: "The environment into which the pacticipant(s) are to be deployed", default: nil, hide: true
2122
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
2223
method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
2324
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
@@ -37,7 +38,7 @@ def can_i_deploy(*ignored_but_necessary)
3738
selectors = VersionSelectorOptionsParser.call(ARGV)
3839
validate_can_i_deploy_selectors(selectors)
3940
can_i_deploy_options = { output: options.output, retry_while_unknown: options.retry_while_unknown, retry_interval: options.retry_interval }
40-
result = CanIDeploy.call(options.broker_base_url, selectors, {to_tag: options.to, limit: options.limit}, can_i_deploy_options, pact_broker_client_options)
41+
result = CanIDeploy.call(options.broker_base_url, selectors, { to_tag: options.to, to_environment: options.to_environment, limit: options.limit }, can_i_deploy_options, pact_broker_client_options)
4142
$stdout.puts result.message
4243
exit(1) unless result.success
4344
end

lib/pact_broker/client/cli/can_i_deploy_long_desc.txt

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,27 @@ Using a specific version is the easiest way to ensure you get an accurate respon
1919

2020
#### Recommended usage - allowing the Pact Broker to automatically determine the dependencies
2121

22-
Prerequisite: if you would like the Pact Broker to calculate the dependencies for you when you want to deploy an application into a given environment, you will need to let the Broker know which version of each application is in that environment. To do this, the relevant application version resource in the Broker will need to be "tagged" with the name of the environment during the deployment process:
22+
Prerequisite: if you would like the Pact Broker to calculate the dependencies for you when you want to deploy an application into a given environment, you will need to let the Broker know which version of each application is in that environment.
23+
24+
How you do this depends on the version of the Pact Broker you are running.
25+
26+
If you are using a Broker version where deployment versions are supported, then you would notify the Broker of the deployment of this application version like so:
27+
28+
$ pact-broker record-deployment --pacticipant Foo --version 173153ae0 --environment test
29+
30+
This assumes that you have already set up an environment named "test" in the Broker.
31+
32+
If you are using a Broker version that does not support deployment environments, then you will need to use tags to notify the broker of the deployment of this application version, like so:
2333

2434
$ pact-broker create-version-tag --pacticipant Foo --version 173153ae0 --tag test
2535

26-
This allows you to use the following simple command to find out if you are safe to deploy:
36+
Once you have configured your build to notify the Pact Broker of the successful deployment using either method describe above, you can use the following simple command to find out if you are safe to deploy (use either `--to` or `--to-environment` as supported):
2737

28-
$ pact-broker can-i-deploy --pacticipant PACTICIPANT --version VERSION \
29-
--to ENVIRONMENT \
38+
$ pact-broker can-i-deploy --pacticipant PACTICIPANT --version VERSION
39+
[--to-environment ENVIRONMENT | --to ENVIRONMENT_TAG ]
3040
--broker-base-url BROKER_BASE_URL
3141

32-
If the `--to` tag is omitted, then the query will return the compatiblity with the overall latest version of each of the other applications.
42+
If the `--to` or `--to-environment` options are omitted, then the query will return the compatiblity with the overall latest version of each of the other applications.
3343

3444
Examples:
3545

@@ -38,7 +48,7 @@ Can I deploy version 173153ae0 of application Foo to the test environment?
3848

3949

4050
$ pact-broker can-i-deploy --pacticipant Foo --version 173153ae0 \
41-
--to test \
51+
--to-environment test \
4252
--broker-base-url https://my-pact-broker
4353

4454

@@ -56,21 +66,20 @@ Can I deploy the latest version of the application Foo that has the tag "test" t
5666
--broker-base-url https://my-pact-broker
5767

5868

59-
6069
#### Alternate usage - specifying dependencies explicitly
6170

6271
If you are unable to use tags, or there is some other limitation that stops you from using the recommended approach, you can specify one or more of the dependencies explictly. You must also do this if you want to use the `--all TAG` option for any of the pacticipants.
6372

6473
You can specify as many application versions as you like, and you can even specify multiple versions of the same application (repeat the `--pacticipant` name and supply a different version.)
6574

66-
You can use explictly declared dependencies with or without the `--to ENVIRONMENT`. For example, if you declare two (or more) application versions with no `--to ENVIRONMENT`, then only the applications you specify will be taken into account when determining if it is safe to deploy. If you declare two (or more) application versions _as well as_ a `--to ENVIRONMENT`, then the Pact Broker will work out what integrations your declared applications will have in that environment when determining if it safe to deploy. When using this script for a production release, and you are using tags, it is always the most future-proof option to use the `--to` if possible, as it will catch any newly added consumers or providers.
75+
You can use explictly declared dependencies with or without the `--to ENVIRONMENT_TAG`. For example, if you declare two (or more) application versions with no `--to ENVIRONMENT_TAG`, then only the applications you specify will be taken into account when determining if it is safe to deploy. If you declare two (or more) application versions _as well as_ a `--to ENVIRONMENT`, then the Pact Broker will work out what integrations your declared applications will have in that environment when determining if it safe to deploy. When using this script for a production release, and you are using tags, it is always the most future-proof option to use the `--to` if possible, as it will catch any newly added consumers or providers.
6776

6877
If you are finding that your dependencies are not being automatically included when you supply multiple pacticipant versions, please upgrade to the latest version of the Pact Broker, as this is a more recently added feature.
6978

7079

7180
$ pact-broker can-i-deploy --pacticipant PACTICIPANT_1 [--version VERSION_1 | --latest [TAG_1] | --all TAG_1] \
7281
--pacticipant PACTICIPANT_2 [--version VERSION_2 | --latest [TAG_2] | --all TAG_2] \
73-
[--to ENVIRONMENT] \
82+
[--to-environment ENVIRONMENT | --to ENVIRONMENT_TAG] \
7483
--broker-base-url BROKER_BASE_URL
7584

7685
Examples:

lib/pact_broker/client/matrix.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ def query_options(options, selectors)
5353
opts[:success] = [*options[:success]]
5454
end
5555
opts[:limit] = options[:limit] if options[:limit]
56+
opts[:environment] = options[:to_environment] if options[:to_environment]
5657
if options[:to_tag]
5758
opts[:latest] = 'true'
5859
opts[:tag] = options[:to_tag]
59-
elsif selectors.size == 1
60+
elsif selectors.size == 1 && !options[:to_environment]
6061
opts[:latest] = 'true'
6162
end
6263
opts

spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module CLI
3535
end
3636

3737
it "invokes the CanIDeploy service" do
38-
expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, {to_tag: nil, limit: 1000}, {output: 'table', retry_while_unknown: 1, retry_interval: 2}, {verbose: 'verbose'})
38+
expect(CanIDeploy).to receive(:call).with('http://pact-broker', version_selectors, {to_tag: nil, to_environment: nil, limit: 1000}, {output: 'table', retry_while_unknown: 1, retry_interval: 2}, {verbose: 'verbose'})
3939
invoke_can_i_deploy
4040
end
4141

@@ -53,7 +53,18 @@ module CLI
5353
end
5454

5555
it "passes the value as the matrix options" do
56-
expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: 'prod', limit: 1000}, anything, anything)
56+
expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: 'prod', to_environment: nil, limit: 1000}, anything, anything)
57+
invoke_can_i_deploy
58+
end
59+
end
60+
61+
context "with --to-environment" do
62+
before do
63+
subject.options.to_environment = 'prod'
64+
end
65+
66+
it "passes the value as the matrix options" do
67+
expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: nil, to_environment: 'prod', limit: 1000}, anything, anything)
5768
invoke_can_i_deploy
5869
end
5970
end

spec/service_providers/pact_broker_client_matrix_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ module PactBroker::Client
279279
expect(matrix[:matrix].size).to eq 1
280280
end
281281
end
282+
283+
context "with an environment name" do
284+
it "passes the environment name in the options"
285+
end
282286
end
283287
end
284288
end

0 commit comments

Comments
 (0)