diff --git a/app/services/create_deployment_service.rb b/app/services/create_deployment_service.rb index 47c740addb03fe1f2396396c904e5cbc48e4f44b..2e859a30cac751c38f96924751ad43da6467d630 100644 --- a/app/services/create_deployment_service.rb +++ b/app/services/create_deployment_service.rb @@ -6,14 +6,12 @@ class CreateDeploymentService < BaseService ActiveRecord::Base.transaction do @deployable = deployable - @environment = prepare_environment + @environment = environment + @environment.external_url = expanded_url if expanded_url + @environment.state_event = action + @environment.save - if stop? - @environment.stop - return - end - - @environment.start + return if @environment.stopped? deploy.tap do |deployment| deployment.update_merge_request_metrics! @@ -37,10 +35,8 @@ class CreateDeploymentService < BaseService deployable: @deployable) end - def prepare_environment - project.environments.find_or_create_by(name: expanded_name) do |environment| - environment.external_url = expanded_url - end + def environment + @environment ||= project.environments.find_or_create_by(name: expanded_name) end def expanded_name @@ -69,7 +65,7 @@ class CreateDeploymentService < BaseService params[:variables] || [] end - def stop? - params[:options].fetch(:stop, false) + def action + params[:options].fetch(:action, 'start') end end diff --git a/lib/gitlab/ci/config/node/environment.rb b/lib/gitlab/ci/config/node/environment.rb index daa115f9017a7b3c6f9f17faafc4a334bc5f80fd..1c1d07843b117eb7330d2e2496f8a283cf2b8fde 100644 --- a/lib/gitlab/ci/config/node/environment.rb +++ b/lib/gitlab/ci/config/node/environment.rb @@ -8,7 +8,7 @@ module Gitlab class Environment < Entry include Validatable - ALLOWED_KEYS = %i[name url close] + ALLOWED_KEYS = %i[name url action on_stop] validations do validate do @@ -36,7 +36,11 @@ module Gitlab addressable_url: true, allow_nil: true - validates :close, boolean: true, allow_nil: true + validates :action, + inclusion: { in: %w[start stop], message: 'should be start or stop, ' }, + allow_nil: true + + validates :on_stop, string: true, allow_nil: true end end @@ -56,9 +60,13 @@ module Gitlab value[:url] end + def action + value[:action] || 'start' + end + def value case @config - when String then { name: @config } + when String then { name: @config, action: 'start' } when Hash then @config else {} end