Skip to content

Commit

Permalink
feat(can-i-deploy): add --to-environment option
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 21, 2021
1 parent 0fcaf04 commit 05ae44c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
3 changes: 2 additions & 1 deletion lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Broker < CustomThor
method_option :version, required: false, aliases: "-e", desc: "The pacticipant version. Must be entered after the --pacticipant that it relates to."
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."
method_option :to, required: false, banner: 'TAG', desc: "This is too hard to explain in a short sentence. Look at the examples.", default: nil
method_option :to_environment, required: false, banner: 'ENVIRONMENT', desc: "The environment into which the pacticipant(s) are to be deployed", default: nil, hide: true
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker"
method_option :broker_username, aliases: "-u", desc: "Pact Broker basic auth username"
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password"
Expand All @@ -37,7 +38,7 @@ def can_i_deploy(*ignored_but_necessary)
selectors = VersionSelectorOptionsParser.call(ARGV)
validate_can_i_deploy_selectors(selectors)
can_i_deploy_options = { output: options.output, retry_while_unknown: options.retry_while_unknown, retry_interval: options.retry_interval }
result = CanIDeploy.call(options.broker_base_url, selectors, {to_tag: options.to, limit: options.limit}, can_i_deploy_options, pact_broker_client_options)
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)
$stdout.puts result.message
exit(1) unless result.success
end
Expand Down
27 changes: 18 additions & 9 deletions lib/pact_broker/client/cli/can_i_deploy_long_desc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,27 @@ Using a specific version is the easiest way to ensure you get an accurate respon

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

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:
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.

How you do this depends on the version of the Pact Broker you are running.

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:

$ pact-broker record-deployment --pacticipant Foo --version 173153ae0 --environment test

This assumes that you have already set up an environment named "test" in the Broker.

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:

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

This allows you to use the following simple command to find out if you are safe to deploy:
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):

$ pact-broker can-i-deploy --pacticipant PACTICIPANT --version VERSION \
--to ENVIRONMENT \
$ pact-broker can-i-deploy --pacticipant PACTICIPANT --version VERSION
[--to-environment ENVIRONMENT | --to ENVIRONMENT_TAG ]
--broker-base-url BROKER_BASE_URL

If the `--to` tag is omitted, then the query will return the compatiblity with the overall latest version of each of the other applications.
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.

Examples:

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


$ pact-broker can-i-deploy --pacticipant Foo --version 173153ae0 \
--to test \
--to-environment test \
--broker-base-url https://my-pact-broker


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



#### Alternate usage - specifying dependencies explicitly

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.

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.)

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.
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.

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.


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

Examples:
Expand Down
3 changes: 2 additions & 1 deletion lib/pact_broker/client/matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ def query_options(options, selectors)
opts[:success] = [*options[:success]]
end
opts[:limit] = options[:limit] if options[:limit]
opts[:environment] = options[:to_environment] if options[:to_environment]
if options[:to_tag]
opts[:latest] = 'true'
opts[:tag] = options[:to_tag]
elsif selectors.size == 1
elsif selectors.size == 1 && !options[:to_environment]
opts[:latest] = 'true'
end
opts
Expand Down
15 changes: 13 additions & 2 deletions spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module CLI
end

it "invokes the CanIDeploy service" do
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'})
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'})
invoke_can_i_deploy
end

Expand All @@ -53,7 +53,18 @@ module CLI
end

it "passes the value as the matrix options" do
expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: 'prod', limit: 1000}, anything, anything)
expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: 'prod', to_environment: nil, limit: 1000}, anything, anything)
invoke_can_i_deploy
end
end

context "with --to-environment" do
before do
subject.options.to_environment = 'prod'
end

it "passes the value as the matrix options" do
expect(CanIDeploy).to receive(:call).with(anything, anything, {to_tag: nil, to_environment: 'prod', limit: 1000}, anything, anything)
invoke_can_i_deploy
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/service_providers/pact_broker_client_matrix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ module PactBroker::Client
expect(matrix[:matrix].size).to eq 1
end
end

context "with an environment name" do
it "passes the environment name in the options"
end
end
end
end

0 comments on commit 05ae44c

Please sign in to comment.