2015-10-02 07:46:38 -04:00
|
|
|
module Ci
|
|
|
|
class CreateBuildsService
|
2016-06-03 10:22:53 -04:00
|
|
|
def initialize(pipeline)
|
|
|
|
@pipeline = pipeline
|
2016-06-14 07:44:03 -04:00
|
|
|
@config = pipeline.config_processor
|
2016-03-31 13:51:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute(stage, user, status, trigger_request = nil)
|
2016-06-14 07:44:03 -04:00
|
|
|
builds_attrs = @config.builds_for_stage_and_ref(stage, @pipeline.ref, @pipeline.tag, trigger_request)
|
2015-10-05 06:02:26 -04:00
|
|
|
|
2015-10-15 09:08:31 -04:00
|
|
|
# check when to create next build
|
|
|
|
builds_attrs = builds_attrs.select do |build_attrs|
|
|
|
|
case build_attrs[:when]
|
|
|
|
when 'on_success'
|
|
|
|
status == 'success'
|
|
|
|
when 'on_failure'
|
|
|
|
status == 'failed'
|
2016-07-18 08:48:36 -04:00
|
|
|
when 'always', 'manual'
|
2015-10-15 09:08:31 -04:00
|
|
|
%w(success failed).include?(status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 09:55:38 -04:00
|
|
|
# don't create the same build twice
|
|
|
|
builds_attrs.reject! do |build_attrs|
|
2016-06-15 08:03:43 -04:00
|
|
|
@pipeline.builds.find_by(ref: @pipeline.ref,
|
|
|
|
tag: @pipeline.tag,
|
2016-06-14 07:44:03 -04:00
|
|
|
trigger_request: trigger_request,
|
|
|
|
name: build_attrs[:name])
|
2016-06-02 09:55:38 -04:00
|
|
|
end
|
|
|
|
|
2015-10-05 06:02:26 -04:00
|
|
|
builds_attrs.map do |build_attrs|
|
2016-06-02 09:55:38 -04:00
|
|
|
build_attrs.slice!(:name,
|
|
|
|
:commands,
|
|
|
|
:tag_list,
|
|
|
|
:options,
|
|
|
|
:allow_failure,
|
|
|
|
:stage,
|
2016-06-15 14:14:25 -04:00
|
|
|
:stage_idx,
|
2016-07-16 14:10:22 -04:00
|
|
|
:environment,
|
|
|
|
:when,
|
|
|
|
:yaml_variables)
|
2015-10-05 06:02:26 -04:00
|
|
|
|
2016-06-14 07:44:03 -04:00
|
|
|
build_attrs.merge!(pipeline: @pipeline,
|
|
|
|
ref: @pipeline.ref,
|
|
|
|
tag: @pipeline.tag,
|
2016-06-02 09:55:38 -04:00
|
|
|
trigger_request: trigger_request,
|
|
|
|
user: user,
|
2016-06-14 07:44:03 -04:00
|
|
|
project: @pipeline.project)
|
2015-10-05 06:02:26 -04:00
|
|
|
|
2016-07-18 08:59:01 -04:00
|
|
|
# TODO: The proper implementation for this is in
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/5295
|
2016-07-18 08:48:36 -04:00
|
|
|
build_attrs[:status] = 'skipped' if build_attrs[:when] == 'manual'
|
|
|
|
|
2016-06-02 09:55:38 -04:00
|
|
|
##
|
|
|
|
# We do not persist new builds here.
|
2016-06-14 07:44:03 -04:00
|
|
|
# Those will be persisted when @pipeline is saved.
|
2016-06-02 09:55:38 -04:00
|
|
|
#
|
2016-06-14 07:44:03 -04:00
|
|
|
@pipeline.builds.new(build_attrs)
|
2015-10-02 07:46:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|