diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/models/ci/build.rb | 10 | ||||
-rw-r--r-- | app/services/create_deployment_service.rb | 78 | ||||
-rw-r--r-- | app/workers/build_success_worker.rb | 11 |
3 files changed, 40 insertions, 59 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 85a7d64c804..3c7a3a1ccab 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -138,6 +138,11 @@ module Ci ExpandVariables.expand(environment, simple_variables) if environment end + def expanded_environment_url + ExpandVariables.expand(environment_url, simple_variables) if + environment_url + end + def ci_environment_url expanded_environment_url || persisted_environment&.external_url end @@ -526,11 +531,6 @@ module Ci variables end - def expanded_environment_url - ExpandVariables.expand(environment_url, simple_variables) if - environment_url - end - def environment_url options.dig(:environment, :url) end diff --git a/app/services/create_deployment_service.rb b/app/services/create_deployment_service.rb index 47f9b2c621c..75f729e838d 100644 --- a/app/services/create_deployment_service.rb +++ b/app/services/create_deployment_service.rb @@ -1,71 +1,61 @@ -class CreateDeploymentService < BaseService - def execute(deployable = nil) +class CreateDeploymentService + attr_reader :job + + delegate :expanded_environment_name, + :expanded_environment_url, + :project, + to: :job + + def initialize(job) + @job = job + end + + def execute return unless executable? ActiveRecord::Base.transaction do - @deployable = deployable + environment.external_url = expanded_environment_url if + expanded_environment_url + environment.fire_state_event(action) - @environment = environment - @environment.external_url = expanded_url if expanded_url - @environment.fire_state_event(action) + return unless environment.save + return if environment.stopped? - return unless @environment.save - return if @environment.stopped? - - deploy.tap do |deployment| - deployment.update_merge_request_metrics! - end + deploy.tap(&:update_merge_request_metrics!) end end private def executable? - project && name.present? + project && job.environment.present? end def deploy project.deployments.create( - environment: @environment, - ref: params[:ref], - tag: params[:tag], - sha: params[:sha], - user: current_user, - deployable: @deployable, - on_stop: options[:on_stop]) + environment: environment, + ref: job.ref, + tag: job.tag, + sha: job.sha, + user: job.user, + deployable: job, + on_stop: on_stop) end def environment - @environment ||= project.environments.find_or_create_by(name: expanded_name) - end - - def expanded_name - ExpandVariables.expand(name, variables) - end - - def expanded_url - return unless url - - @expanded_url ||= ExpandVariables.expand(url, variables) - end - - def name - params[:environment] - end - - def url - options[:url] + @environment ||= + project.environments.find_or_create_by(name: expanded_environment_name) end - def options - params[:options] || {} + def environment_options + @environment_options ||= job.options[:environment] || {} end - def variables - params[:variables] || [] + def on_stop + environment_options[:on_stop] end def action - options[:action] || 'start' + environment_options[:action] || 'start' end end diff --git a/app/workers/build_success_worker.rb b/app/workers/build_success_worker.rb index e17add7421f..bf009dfab0f 100644 --- a/app/workers/build_success_worker.rb +++ b/app/workers/build_success_worker.rb @@ -11,15 +11,6 @@ class BuildSuccessWorker private def create_deployment(build) - service = CreateDeploymentService.new( - build.project, build.user, - environment: build.environment, - sha: build.sha, - ref: build.ref, - tag: build.tag, - options: build.options.to_h[:environment], - variables: build.variables) - - service.execute(build) + CreateDeploymentService.new(build).execute end end |