2016-06-10 21:36:54 +00:00
|
|
|
require_relative 'base_service'
|
|
|
|
|
|
|
|
class CreateDeploymentService < BaseService
|
|
|
|
def execute(deployable)
|
2016-06-10 22:15:53 +00:00
|
|
|
environment = find_environment(params[:environment])
|
|
|
|
return error('no environment') unless environmnet
|
2016-06-10 21:36:54 +00:00
|
|
|
|
|
|
|
deployment = create_deployment(environment, deployable)
|
|
|
|
if deployment.persisted?
|
|
|
|
success(deployment)
|
|
|
|
else
|
|
|
|
error(deployment.errors)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_environment(environment)
|
|
|
|
project.environments.find_by(name: environment)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_deployment(environment, deployable)
|
|
|
|
environment.deployments.create(
|
|
|
|
project: project,
|
|
|
|
ref: build.ref,
|
|
|
|
tag: build.tag,
|
|
|
|
sha: build.sha,
|
|
|
|
user: current_user,
|
|
|
|
deployable: deployable,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def success(deployment)
|
|
|
|
out = super()
|
|
|
|
out[:deployment] = deployment
|
|
|
|
out
|
|
|
|
end
|
|
|
|
end
|