2016-06-16 23:09:34 +05:30
|
|
|
class CreateDeploymentService < BaseService
|
|
|
|
def execute(deployable = nil)
|
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
|
|
|
|
@deployable = deployable
|
|
|
|
|
|
|
|
@environment = environment
|
|
|
|
@environment.external_url = expanded_url if expanded_url
|
|
|
|
@environment.fire_state_event(action)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
return unless @environment.save
|
|
|
|
return if @environment.stopped?
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
deploy.tap do |deployment|
|
|
|
|
deployment.update_merge_request_metrics!
|
|
|
|
end
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def executable?
|
|
|
|
project && name.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])
|
|
|
|
end
|
|
|
|
|
|
|
|
def environment
|
|
|
|
@environment ||= project.environments.find_or_create_by(name: expanded_name)
|
2016-09-29 09:46:39 +05:30
|
|
|
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]
|
|
|
|
end
|
|
|
|
|
|
|
|
def options
|
|
|
|
params[:options] || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def variables
|
|
|
|
params[:variables] || []
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
def action
|
|
|
|
options[:action] || 'start'
|
|
|
|
end
|
2016-06-16 23:09:34 +05:30
|
|
|
end
|