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
|
2022-01-26 12:08:38 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
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
|
2022-01-26 12:08:38 +05:30
|
|
|
job_variables_attributes resource_group scheduling_type].freeze
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def self.extra_accessors
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def execute(build)
|
2020-05-24 23:13:21 +05:30
|
|
|
build.ensure_scheduling_type!
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
clone!(build).tap do |new_build|
|
2021-09-30 23:02:18 +05:30
|
|
|
check_assignable_runners!(new_build)
|
|
|
|
next if new_build.failed?
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
Gitlab::OptimisticLocking.retry_lock(new_build, name: 'retry_build', &:enqueue)
|
2021-04-29 21:17:54 +05:30
|
|
|
AfterRequeueJobService.new(project, current_user).execute(build)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
::MergeRequests::AddTodoWhenBuildFailsService
|
2021-06-08 01:23:25 +05:30
|
|
|
.new(project: project, current_user: current_user)
|
2017-08-17 22:00:37 +05:30
|
|
|
.close(new_build)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2021-11-18 22:05:49 +05:30
|
|
|
def clone!(build)
|
|
|
|
# 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-06-08 01:23:25 +05:30
|
|
|
check_access!(build)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
new_build = clone_build(build)
|
|
|
|
::Ci::Pipelines::AddJobService.new(build.pipeline).execute!(new_build) do |job|
|
|
|
|
BulkInsertableAssociations.with_bulk_insert do
|
|
|
|
job.save!
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
if create_deployment_in_separate_transaction?
|
|
|
|
clone_deployment!(new_build, build)
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
build.reset # refresh the data to get new values of `retried` and `processed`.
|
|
|
|
|
|
|
|
new_build
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
def check_access!(build)
|
|
|
|
unless can?(current_user, :update_build, build)
|
|
|
|
raise Gitlab::Access::AccessDeniedError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
def check_assignable_runners!(build); end
|
|
|
|
|
|
|
|
def clone_build(build)
|
|
|
|
project.builds.new(build_attributes(build)).tap do |new_build|
|
2022-01-26 12:08:38 +05:30
|
|
|
unless create_deployment_in_separate_transaction?
|
|
|
|
new_build.assign_attributes(deployment_attributes_for(new_build, build))
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
2021-09-30 23:02:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def build_attributes(build)
|
|
|
|
attributes = self.class.clone_accessors.to_h do |attribute|
|
|
|
|
[attribute, build.public_send(attribute)] # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
end
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
if create_deployment_in_separate_transaction? && build.persisted_environment.present?
|
|
|
|
attributes[:metadata_attributes] ||= {}
|
|
|
|
attributes[:metadata_attributes][:expanded_environment_name] = build.expanded_environment_name
|
|
|
|
end
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
attributes[:user] = current_user
|
|
|
|
attributes
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
def deployment_attributes_for(new_build, old_build)
|
|
|
|
::Gitlab::Ci::Pipeline::Seed::Build
|
|
|
|
.deployment_attributes_for(new_build, old_build.persisted_environment)
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
def clone_deployment!(new_build, old_build)
|
|
|
|
return unless old_build.deployment.present?
|
|
|
|
|
|
|
|
# We should clone the previous deployment attributes instead of initializing
|
|
|
|
# new object with `Seed::Deployment`.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/347206
|
|
|
|
deployment = ::Gitlab::Ci::Pipeline::Seed::Deployment
|
|
|
|
.new(new_build, old_build.persisted_environment).to_resource
|
|
|
|
|
|
|
|
return unless deployment
|
|
|
|
|
|
|
|
new_build.create_deployment!(deployment.attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_deployment_in_separate_transaction?
|
|
|
|
strong_memoize(:create_deployment_in_separate_transaction) do
|
|
|
|
::Feature.enabled?(:create_deployment_in_separate_transaction, project, default_enabled: :yaml)
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Ci::RetryBuildService.prepend_mod_with('Ci::RetryBuildService')
|