2016-08-11 09:22:35 -04:00
|
|
|
module Ci
|
|
|
|
class CreatePipelineBuildsService < BaseService
|
|
|
|
attr_reader :pipeline
|
|
|
|
|
|
|
|
def execute(pipeline)
|
|
|
|
@pipeline = pipeline
|
|
|
|
|
|
|
|
new_builds.map do |build_attributes|
|
|
|
|
create_build(build_attributes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-22 12:51:46 -05:00
|
|
|
delegate :project, to: :pipeline
|
2016-12-08 10:37:41 -05:00
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def create_build(build_attributes)
|
|
|
|
build_attributes = build_attributes.merge(
|
|
|
|
pipeline: pipeline,
|
2016-12-08 10:37:41 -05:00
|
|
|
project: project,
|
2016-08-11 09:22:35 -04:00
|
|
|
ref: pipeline.ref,
|
|
|
|
tag: pipeline.tag,
|
|
|
|
user: current_user,
|
|
|
|
trigger_request: trigger_request
|
|
|
|
)
|
2016-12-08 10:37:41 -05:00
|
|
|
build = pipeline.builds.create(build_attributes)
|
|
|
|
|
|
|
|
# Create the environment before the build starts. This sets its slug and
|
|
|
|
# makes it available as an environment variable
|
|
|
|
project.environments.find_or_create_by(name: build.expanded_environment_name) if
|
|
|
|
build.has_environment?
|
|
|
|
|
|
|
|
build
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_builds
|
2017-02-22 17:50:15 -05:00
|
|
|
@new_builds ||= pipeline.config_builds_attributes.
|
|
|
|
reject { |build| existing_build_names.include?(build[:name]) }
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def existing_build_names
|
|
|
|
@existing_build_names ||= pipeline.builds.pluck(:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def trigger_request
|
|
|
|
return @trigger_request if defined?(@trigger_request)
|
|
|
|
|
|
|
|
@trigger_request ||= pipeline.trigger_requests.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|