2017-09-10 17:25:29 +05:30
|
|
|
class CreateDeploymentService
|
|
|
|
attr_reader :job
|
|
|
|
|
|
|
|
delegate :expanded_environment_name,
|
|
|
|
:variables,
|
|
|
|
:project,
|
|
|
|
to: :job
|
|
|
|
|
|
|
|
def initialize(job)
|
|
|
|
@job = job
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-11-03 12:29:30 +05:30
|
|
|
return unless executable?
|
2016-06-16 23:09:34 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
ActiveRecord::Base.transaction do
|
2017-09-10 17:25:29 +05:30
|
|
|
environment.external_url = expanded_environment_url if
|
|
|
|
expanded_environment_url
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
environment.fire_state_event(action)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
return unless environment.save
|
|
|
|
return if environment.stopped?
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
deploy.tap(&:update_merge_request_metrics!)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def executable?
|
2017-09-10 17:25:29 +05:30
|
|
|
project && job.environment.present? && environment
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def deploy
|
|
|
|
project.deployments.create(
|
2017-09-10 17:25:29 +05:30
|
|
|
environment: environment,
|
|
|
|
ref: job.ref,
|
|
|
|
tag: job.tag,
|
|
|
|
sha: job.sha,
|
|
|
|
user: job.user,
|
|
|
|
deployable: job,
|
|
|
|
on_stop: on_stop)
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def environment
|
2017-09-10 17:25:29 +05:30
|
|
|
@environment ||= job.persisted_environment
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def environment_options
|
|
|
|
@environment_options ||= job.options&.dig(:environment) || {}
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def expanded_environment_url
|
|
|
|
return @expanded_environment_url if defined?(@expanded_environment_url)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
@expanded_environment_url =
|
|
|
|
ExpandVariables.expand(environment_url, variables) if environment_url
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def environment_url
|
|
|
|
environment_options[:url]
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def on_stop
|
|
|
|
environment_options[:on_stop]
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
def action
|
2017-09-10 17:25:29 +05:30
|
|
|
environment_options[:action] || 'start'
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|