debian-mirror-gitlab/app/services/ci/retry_build_service.rb

78 lines
2.3 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Ci
class RetryBuildService < ::BaseService
2021-03-08 18:12:59 +05:30
include Gitlab::OptimisticLocking
2020-11-24 15:15:51 +05:30
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-08-17 22:00:37 +05:30
def execute(build)
2020-05-24 23:13:21 +05:30
build.ensure_scheduling_type!
2017-08-17 22:00:37 +05:30
reprocess!(build).tap do |new_build|
2020-11-24 15:15:51 +05:30
mark_subsequent_stages_as_processable(build)
build.pipeline.reset_ancestor_bridges!
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
Gitlab::OptimisticLocking.retry_lock(new_build, &:enqueue)
2017-08-17 22:00:37 +05:30
MergeRequests::AddTodoWhenBuildFailsService
.new(project, current_user)
.close(new_build)
end
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
def reprocess!(build)
unless can?(current_user, :update_build, build)
raise Gitlab::Access::AccessDeniedError
end
2020-11-24 15:15:51 +05:30
attributes = self.class.clone_accessors.map do |attribute|
2018-03-17 18:26:18 +05:30
[attribute, build.public_send(attribute)] # rubocop:disable GitlabSecurity/PublicSend
2020-03-13 15:44:24 +05:30
end.to_h
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
attributes[:user] = current_user
2020-05-24 23:13:21 +05:30
2017-08-17 22:00:37 +05:30
Ci::Build.transaction do
# mark all other builds of that name as retried
build.pipeline.builds.latest
.where(name: build.name)
2020-03-13 15:44:24 +05:30
.update_all(retried: true, processed: true)
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
create_build!(attributes).tap do
# mark existing object as retried/processed without a reload
build.retried = true
build.processed = true
end
2017-08-17 22:00:37 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2019-12-21 20:55:43 +05:30
private
def create_build!(attributes)
2020-03-13 15:44:24 +05:30
build = project.builds.new(attributes)
2020-04-08 14:13:33 +05:30
build.assign_attributes(::Gitlab::Ci::Pipeline::Seed::Build.environment_attributes_for(build))
2020-03-13 15:44:24 +05:30
build.retried = false
2021-01-03 14:25:43 +05:30
BulkInsertableAssociations.with_bulk_insert do
2020-07-28 23:09:34 +05:30
build.save!
end
2019-12-21 20:55:43 +05:30
build
end
2020-11-24 15:15:51 +05:30
def mark_subsequent_stages_as_processable(build)
2021-03-08 18:12:59 +05:30
build.pipeline.processables.skipped.after_stage(build.stage_idx).find_each do |skipped|
retry_optimistic_lock(skipped) { |build| build.process(current_user) }
2020-11-24 15:15:51 +05:30
end
end
2017-08-17 22:00:37 +05:30
end
end
2020-11-24 15:15:51 +05:30
Ci::RetryBuildService.prepend_if_ee('EE::Ci::RetryBuildService')