2015-10-02 07:46:38 -04:00
|
|
|
module Ci
|
|
|
|
class CreateBuildsService
|
2015-10-15 09:08:31 -04:00
|
|
|
def execute(commit, stage, ref, tag, user, trigger_request, status)
|
2015-10-05 06:02:26 -04:00
|
|
|
builds_attrs = commit.config_processor.builds_for_stage_and_ref(stage, ref, tag)
|
|
|
|
|
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'
|
|
|
|
when 'always'
|
|
|
|
%w(success failed).include?(status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-05 06:02:26 -04:00
|
|
|
builds_attrs.map do |build_attrs|
|
2015-10-05 07:12:00 -04:00
|
|
|
# don't create the same build twice
|
|
|
|
unless commit.builds.find_by(ref: ref, tag: tag, trigger_request: trigger_request, name: build_attrs[:name])
|
|
|
|
build_attrs.slice!(:name,
|
|
|
|
:commands,
|
|
|
|
:tag_list,
|
|
|
|
:options,
|
|
|
|
:allow_failure,
|
|
|
|
:stage,
|
|
|
|
:stage_idx)
|
2015-10-05 06:02:26 -04:00
|
|
|
|
2015-10-05 07:12:00 -04:00
|
|
|
build_attrs.merge!(ref: ref,
|
|
|
|
tag: tag,
|
|
|
|
trigger_request: trigger_request,
|
2015-12-04 06:55:23 -05:00
|
|
|
user: user,
|
2015-12-10 12:47:22 -05:00
|
|
|
project: commit.project)
|
2015-10-05 06:02:26 -04:00
|
|
|
|
2015-12-07 07:23:23 -05:00
|
|
|
build = commit.builds.create!(build_attrs)
|
|
|
|
build.execute_hooks
|
2015-10-05 07:12:00 -04:00
|
|
|
end
|
2015-10-02 07:46:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|