2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-10 09:43:15 -05:00
|
|
|
module Ci
|
2017-02-14 04:38:17 -05:00
|
|
|
class RetryBuildService < ::BaseService
|
2020-08-28 14:10:51 -04:00
|
|
|
def self.clone_accessors
|
|
|
|
%i[pipeline project ref tag options name
|
|
|
|
allow_failure stage stage_id stage_idx trigger_request
|
|
|
|
yaml_variables when environment coverage_regex
|
|
|
|
description tag_list protected needs_attributes
|
|
|
|
resource_group scheduling_type].freeze
|
|
|
|
end
|
2017-02-16 07:13:10 -05:00
|
|
|
|
2017-02-14 04:38:17 -05:00
|
|
|
def execute(build)
|
2020-05-15 11:08:04 -04:00
|
|
|
build.ensure_scheduling_type!
|
|
|
|
|
2017-04-16 07:14:39 -04:00
|
|
|
reprocess!(build).tap do |new_build|
|
2020-08-21 11:10:03 -04:00
|
|
|
mark_subsequent_stages_as_processable(build)
|
2017-02-14 07:51:12 -05:00
|
|
|
|
2020-01-17 10:08:37 -05:00
|
|
|
Gitlab::OptimisticLocking.retry_lock(new_build, &:enqueue)
|
2017-02-14 06:20:02 -05:00
|
|
|
|
2017-02-14 04:38:17 -05:00
|
|
|
MergeRequests::AddTodoWhenBuildFailsService
|
2017-02-15 05:02:05 -05:00
|
|
|
.new(project, current_user)
|
2017-02-14 04:38:17 -05:00
|
|
|
.close(new_build)
|
|
|
|
end
|
2017-02-10 09:43:15 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-04-16 07:14:39 -04:00
|
|
|
def reprocess!(build)
|
2017-02-14 04:38:17 -05:00
|
|
|
unless can?(current_user, :update_build, build)
|
2017-02-13 10:38:08 -05:00
|
|
|
raise Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
2017-02-10 09:43:15 -05:00
|
|
|
|
2020-08-28 14:10:51 -04:00
|
|
|
attributes = self.class.clone_accessors.map do |attribute|
|
2017-08-10 12:39:26 -04:00
|
|
|
[attribute, build.public_send(attribute)] # rubocop:disable GitlabSecurity/PublicSend
|
2020-02-12 01:09:05 -05:00
|
|
|
end.to_h
|
2017-02-16 07:13:10 -05:00
|
|
|
|
2020-02-12 01:09:05 -05:00
|
|
|
attributes[:user] = current_user
|
2020-05-15 11:08:04 -04:00
|
|
|
|
2017-04-16 07:14:39 -04:00
|
|
|
Ci::Build.transaction do
|
|
|
|
# mark all other builds of that name as retried
|
2017-05-09 07:14:45 -04:00
|
|
|
build.pipeline.builds.latest
|
|
|
|
.where(name: build.name)
|
2020-01-17 10:08:37 -05:00
|
|
|
.update_all(retried: true, processed: true)
|
2017-04-16 07:14:39 -04:00
|
|
|
|
2020-01-17 10:08:37 -05:00
|
|
|
create_build!(attributes).tap do
|
|
|
|
# mark existing object as retried/processed without a reload
|
|
|
|
build.retried = true
|
|
|
|
build.processed = true
|
|
|
|
end
|
2017-04-16 07:14:39 -04:00
|
|
|
end
|
2017-02-10 09:43:15 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2019-10-15 08:06:06 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_build!(attributes)
|
2020-02-12 01:09:05 -05:00
|
|
|
build = project.builds.new(attributes)
|
2020-03-03 19:07:52 -05:00
|
|
|
build.assign_attributes(::Gitlab::Ci::Pipeline::Seed::Build.environment_attributes_for(build))
|
2020-01-17 10:08:37 -05:00
|
|
|
build.retried = false
|
2020-07-15 08:09:26 -04:00
|
|
|
BulkInsertableAssociations.with_bulk_insert(enabled: ::Gitlab::Ci::Features.bulk_insert_on_create?(project)) do
|
|
|
|
build.save!
|
|
|
|
end
|
2019-10-15 08:06:06 -04:00
|
|
|
build
|
|
|
|
end
|
2020-08-21 11:10:03 -04:00
|
|
|
|
|
|
|
def mark_subsequent_stages_as_processable(build)
|
|
|
|
build.pipeline.processables.skipped.after_stage(build.stage_idx).find_each do |processable|
|
|
|
|
Gitlab::OptimisticLocking.retry_lock(processable, &:process)
|
|
|
|
end
|
|
|
|
end
|
2017-02-10 09:43:15 -05:00
|
|
|
end
|
|
|
|
end
|
2020-08-28 14:10:51 -04:00
|
|
|
|
|
|
|
Ci::RetryBuildService.prepend_if_ee('EE::Ci::RetryBuildService')
|