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
|
2021-12-10 13:14:42 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
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
|
2021-12-10 10:10:24 -05:00
|
|
|
job_variables_attributes resource_group scheduling_type].freeze
|
2020-08-28 14:10:51 -04:00
|
|
|
end
|
2017-02-16 07:13:10 -05:00
|
|
|
|
2021-06-18 08:10:03 -04:00
|
|
|
def self.extra_accessors
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2017-02-14 04:38:17 -05:00
|
|
|
def execute(build)
|
2020-05-15 11:08:04 -04:00
|
|
|
build.ensure_scheduling_type!
|
|
|
|
|
2021-09-23 18:34:10 -04:00
|
|
|
clone!(build).tap do |new_build|
|
2021-06-18 14:10:13 -04:00
|
|
|
check_assignable_runners!(new_build)
|
|
|
|
next if new_build.failed?
|
|
|
|
|
2021-03-02 10:10:57 -05:00
|
|
|
Gitlab::OptimisticLocking.retry_lock(new_build, name: 'retry_build', &:enqueue)
|
2021-04-02 11:09:19 -04:00
|
|
|
AfterRequeueJobService.new(project, current_user).execute(build)
|
2017-02-14 04:38:17 -05:00
|
|
|
end
|
2017-02-10 09:43:15 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2021-09-23 18:34:10 -04:00
|
|
|
def clone!(build)
|
2021-09-22 14:11:13 -04:00
|
|
|
# Cloning a build requires a strict type check to ensure
|
|
|
|
# the attributes being used for the clone are taken straight
|
|
|
|
# from the model and not overridden by other abstractions.
|
|
|
|
raise TypeError unless build.instance_of?(Ci::Build)
|
|
|
|
|
2021-05-19 02:10:37 -04:00
|
|
|
check_access!(build)
|
2017-02-10 09:43:15 -05:00
|
|
|
|
2021-07-08 11:10:06 -04:00
|
|
|
new_build = clone_build(build)
|
2021-12-22 04:13:51 -05:00
|
|
|
|
2022-01-19 10:14:05 -05:00
|
|
|
new_build.run_after_commit do
|
2022-01-24 13:14:42 -05:00
|
|
|
::Deployments::CreateForBuildService.new.execute(new_build)
|
|
|
|
|
2022-01-19 10:14:05 -05:00
|
|
|
::MergeRequests::AddTodoWhenBuildFailsService
|
|
|
|
.new(project: project)
|
|
|
|
.close(new_build)
|
|
|
|
end
|
|
|
|
|
2021-07-08 11:10:06 -04:00
|
|
|
::Ci::Pipelines::AddJobService.new(build.pipeline).execute!(new_build) do |job|
|
|
|
|
BulkInsertableAssociations.with_bulk_insert do
|
|
|
|
job.save!
|
2020-01-17 10:08:37 -05:00
|
|
|
end
|
2017-04-16 07:14:39 -04:00
|
|
|
end
|
2021-12-10 13:14:42 -05:00
|
|
|
|
2021-07-08 11:10:06 -04:00
|
|
|
build.reset # refresh the data to get new values of `retried` and `processed`.
|
|
|
|
|
|
|
|
new_build
|
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
|
|
|
|
|
2021-05-19 02:10:37 -04:00
|
|
|
def check_access!(build)
|
|
|
|
unless can?(current_user, :update_build, build)
|
|
|
|
raise Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-18 14:10:13 -04:00
|
|
|
def check_assignable_runners!(build); end
|
|
|
|
|
2021-07-08 11:10:06 -04:00
|
|
|
def clone_build(build)
|
2022-01-24 13:14:42 -05:00
|
|
|
project.builds.new(build_attributes(build))
|
2021-07-08 11:10:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_attributes(build)
|
2021-12-10 10:10:24 -05:00
|
|
|
attributes = self.class.clone_accessors.to_h do |attribute|
|
2021-07-08 11:10:06 -04:00
|
|
|
[attribute, build.public_send(attribute)] # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
|
|
|
|
2022-01-24 13:14:42 -05:00
|
|
|
if build.persisted_environment.present?
|
2021-12-10 13:14:42 -05:00
|
|
|
attributes[:metadata_attributes] ||= {}
|
|
|
|
attributes[:metadata_attributes][:expanded_environment_name] = build.expanded_environment_name
|
|
|
|
end
|
|
|
|
|
2021-07-08 11:10:06 -04:00
|
|
|
attributes[:user] = current_user
|
|
|
|
attributes
|
2019-10-15 08:06:06 -04:00
|
|
|
end
|
2017-02-10 09:43:15 -05:00
|
|
|
end
|
|
|
|
end
|
2020-08-28 14:10:51 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Ci::RetryBuildService.prepend_mod_with('Ci::RetryBuildService')
|