2016-08-11 13:22:35 +00:00
|
|
|
module Ci
|
|
|
|
class ProcessPipelineService < BaseService
|
|
|
|
attr_reader :pipeline
|
|
|
|
|
|
|
|
def execute(pipeline)
|
|
|
|
@pipeline = pipeline
|
|
|
|
|
2016-11-25 14:11:56 +00:00
|
|
|
ensure_created_builds! # TODO, remove me in 9.0
|
|
|
|
|
2016-10-20 07:33:44 +00:00
|
|
|
new_builds =
|
|
|
|
stage_indexes_of_created_builds.map do |index|
|
|
|
|
process_stage(index)
|
|
|
|
end
|
2016-08-29 14:47:31 +00:00
|
|
|
|
2016-10-20 07:33:44 +00:00
|
|
|
@pipeline.update_status
|
2016-10-07 12:52:30 +00:00
|
|
|
|
2016-10-20 07:33:44 +00:00
|
|
|
new_builds.flatten.any?
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def process_stage(index)
|
|
|
|
current_status = status_for_prior_stages(index)
|
|
|
|
|
2016-10-20 07:33:44 +00:00
|
|
|
if HasStatus::COMPLETED_STATUSES.include?(current_status)
|
|
|
|
created_builds_in_stage(index).select do |build|
|
2016-10-27 11:34:09 +00:00
|
|
|
Gitlab::OptimisticLocking.retry_lock(build) do |subject|
|
|
|
|
process_build(subject, current_status)
|
2016-10-20 07:33:44 +00:00
|
|
|
end
|
2016-09-12 10:49:58 +00:00
|
|
|
end
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_build(build, current_status)
|
|
|
|
if valid_statuses_for_when(build.when).include?(current_status)
|
2016-10-26 21:03:58 +00:00
|
|
|
build.enqueue
|
2016-08-11 13:22:35 +00:00
|
|
|
true
|
2017-03-02 12:45:01 +00:00
|
|
|
elsif build.barrier?
|
2017-02-28 15:48:39 +00:00
|
|
|
build.block
|
2017-03-02 12:45:01 +00:00
|
|
|
false
|
2016-08-11 13:22:35 +00:00
|
|
|
else
|
2016-10-26 21:03:58 +00:00
|
|
|
build.skip
|
2016-08-11 13:22:35 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_statuses_for_when(value)
|
|
|
|
case value
|
|
|
|
when 'on_success'
|
2016-12-05 13:48:37 +00:00
|
|
|
%w[success skipped]
|
2016-08-11 13:22:35 +00:00
|
|
|
when 'on_failure'
|
|
|
|
%w[failed]
|
|
|
|
when 'always'
|
2016-12-05 13:48:37 +00:00
|
|
|
%w[success failed skipped]
|
2016-08-11 13:22:35 +00:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def status_for_prior_stages(index)
|
|
|
|
pipeline.builds.where('stage_idx < ?', index).latest.status || 'success'
|
|
|
|
end
|
|
|
|
|
|
|
|
def stage_indexes_of_created_builds
|
|
|
|
created_builds.order(:stage_idx).pluck('distinct stage_idx')
|
|
|
|
end
|
|
|
|
|
|
|
|
def created_builds_in_stage(index)
|
|
|
|
created_builds.where(stage_idx: index)
|
|
|
|
end
|
|
|
|
|
|
|
|
def created_builds
|
|
|
|
pipeline.builds.created
|
|
|
|
end
|
2016-11-25 14:11:56 +00:00
|
|
|
|
|
|
|
# This method is DEPRECATED and should be removed in 9.0.
|
|
|
|
#
|
|
|
|
# We need it to maintain backwards compatibility with previous versions
|
|
|
|
# when builds were not created within one transaction with the pipeline.
|
|
|
|
#
|
|
|
|
def ensure_created_builds!
|
|
|
|
return if created_builds.any?
|
|
|
|
|
|
|
|
Ci::CreatePipelineBuildsService
|
|
|
|
.new(project, current_user)
|
|
|
|
.execute(pipeline)
|
|
|
|
end
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
|
|
|
end
|