2018-08-11 03:00:39 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-27 01:04:35 -04:00
|
|
|
module Ci
|
|
|
|
class BuildRunnerPresenter < SimpleDelegator
|
2019-02-14 01:25:10 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2019-09-04 14:10:43 -04:00
|
|
|
RUNNER_REMOTE_TAG_PREFIX = 'refs/tags/'
|
|
|
|
RUNNER_REMOTE_BRANCH_PREFIX = 'refs/remotes/origin/'
|
2019-02-14 01:25:10 -05:00
|
|
|
|
2018-07-27 01:04:35 -04:00
|
|
|
def artifacts
|
|
|
|
return unless options[:artifacts]
|
|
|
|
|
|
|
|
list = []
|
|
|
|
list << create_archive(options[:artifacts])
|
|
|
|
list << create_reports(options[:artifacts][:reports], expire_in: options[:artifacts][:expire_in])
|
|
|
|
list.flatten.compact
|
|
|
|
end
|
|
|
|
|
2019-02-14 01:25:10 -05:00
|
|
|
def ref_type
|
|
|
|
if tag
|
|
|
|
'tag'
|
|
|
|
else
|
|
|
|
'branch'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def git_depth
|
2019-05-30 02:40:53 -04:00
|
|
|
if git_depth_variable
|
|
|
|
git_depth_variable[:value]
|
2020-05-05 20:09:39 -04:00
|
|
|
else
|
2019-06-07 07:51:42 -04:00
|
|
|
project.ci_default_git_depth
|
2019-05-30 02:40:53 -04:00
|
|
|
end.to_i
|
2019-02-14 01:25:10 -05:00
|
|
|
end
|
|
|
|
|
2021-03-02 16:11:07 -05:00
|
|
|
def runner_variables
|
2021-11-08 22:42:22 -05:00
|
|
|
variables.sort_and_expand_all(project, keep_undefined: true).to_runner_variables
|
2021-03-02 16:11:07 -05:00
|
|
|
end
|
|
|
|
|
2019-02-14 01:25:10 -05:00
|
|
|
def refspecs
|
|
|
|
specs = []
|
2019-09-27 14:06:20 -04:00
|
|
|
specs << refspec_for_persistent_ref if persistent_ref_exist?
|
2019-02-14 01:25:10 -05:00
|
|
|
|
|
|
|
if git_depth > 0
|
2019-03-21 09:08:32 -04:00
|
|
|
specs << refspec_for_branch(ref) if branch? || legacy_detached_merge_request_pipeline?
|
2019-02-14 01:25:10 -05:00
|
|
|
specs << refspec_for_tag(ref) if tag?
|
|
|
|
else
|
|
|
|
specs << refspec_for_branch
|
|
|
|
specs << refspec_for_tag
|
|
|
|
end
|
|
|
|
|
|
|
|
specs
|
|
|
|
end
|
|
|
|
|
2021-04-01 08:08:56 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def all_dependencies
|
|
|
|
dependencies = super
|
2021-08-15 23:10:06 -04:00
|
|
|
ActiveRecord::Associations::Preloader.new.preload(dependencies, :job_artifacts_archive)
|
2021-04-01 08:08:56 -04:00
|
|
|
dependencies
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2018-07-27 01:04:35 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def create_archive(artifacts)
|
|
|
|
return unless artifacts[:untracked] || artifacts[:paths]
|
|
|
|
|
2020-05-13 14:08:47 -04:00
|
|
|
archive = {
|
2018-07-27 01:04:35 -04:00
|
|
|
artifact_type: :archive,
|
|
|
|
artifact_format: :zip,
|
|
|
|
name: artifacts[:name],
|
|
|
|
untracked: artifacts[:untracked],
|
|
|
|
paths: artifacts[:paths],
|
|
|
|
when: artifacts[:when],
|
|
|
|
expire_in: artifacts[:expire_in]
|
|
|
|
}
|
2020-05-13 14:08:47 -04:00
|
|
|
|
2021-06-23 11:07:50 -04:00
|
|
|
if artifacts.dig(:exclude).present?
|
2020-05-13 14:08:47 -04:00
|
|
|
archive.merge(exclude: artifacts[:exclude])
|
|
|
|
else
|
|
|
|
archive
|
|
|
|
end
|
2018-07-27 01:04:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_reports(reports, expire_in:)
|
|
|
|
return unless reports&.any?
|
|
|
|
|
2018-10-15 06:17:21 -04:00
|
|
|
reports.map do |report_type, report_paths|
|
2018-07-27 01:04:35 -04:00
|
|
|
{
|
2018-10-15 06:17:21 -04:00
|
|
|
artifact_type: report_type.to_sym,
|
|
|
|
artifact_format: ::Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.fetch(report_type.to_sym),
|
|
|
|
name: ::Ci::JobArtifact::DEFAULT_FILE_NAMES.fetch(report_type.to_sym),
|
|
|
|
paths: report_paths,
|
2018-07-27 01:04:35 -04:00
|
|
|
when: 'always',
|
|
|
|
expire_in: expire_in
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2019-02-14 01:25:10 -05:00
|
|
|
|
|
|
|
def refspec_for_branch(ref = '*')
|
|
|
|
"+#{Gitlab::Git::BRANCH_REF_PREFIX}#{ref}:#{RUNNER_REMOTE_BRANCH_PREFIX}#{ref}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def refspec_for_tag(ref = '*')
|
|
|
|
"+#{Gitlab::Git::TAG_REF_PREFIX}#{ref}:#{RUNNER_REMOTE_TAG_PREFIX}#{ref}"
|
|
|
|
end
|
2019-03-21 09:08:32 -04:00
|
|
|
|
2019-09-27 14:06:20 -04:00
|
|
|
def refspec_for_persistent_ref
|
2021-01-20 01:11:00 -05:00
|
|
|
# Use persistent_ref.sha because it sometimes causes 'git fetch' to do
|
|
|
|
# less work. See
|
2021-01-14 01:11:16 -05:00
|
|
|
# https://gitlab.com/gitlab-com/gl-infra/scalability/-/issues/746.
|
2021-01-20 01:11:00 -05:00
|
|
|
"+#{pipeline.persistent_ref.sha}:#{pipeline.persistent_ref.path}"
|
2019-09-27 14:06:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def persistent_ref_exist?
|
2020-05-18 11:08:15 -04:00
|
|
|
##
|
|
|
|
# Persistent refs for pipelines definitely exist from GitLab 12.4,
|
|
|
|
# hence, we don't need to check the ref existence before passing it to runners.
|
|
|
|
# Checking refs pressurizes gitaly node and should be avoided.
|
|
|
|
# Issue: https://gitlab.com/gitlab-com/gl-infra/production/-/issues/2143
|
|
|
|
return true if Feature.enabled?(:ci_skip_persistent_ref_existence_check)
|
|
|
|
|
2019-09-27 14:06:20 -04:00
|
|
|
pipeline.persistent_ref.exist?
|
|
|
|
end
|
|
|
|
|
2019-05-30 02:40:53 -04:00
|
|
|
def git_depth_variable
|
|
|
|
strong_memoize(:git_depth_variable) do
|
|
|
|
variables&.find { |variable| variable[:key] == 'GIT_DEPTH' }
|
|
|
|
end
|
|
|
|
end
|
2018-07-27 01:04:35 -04:00
|
|
|
end
|
|
|
|
end
|
2020-07-29 08:09:45 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Ci::BuildRunnerPresenter.prepend_mod_with('Ci::BuildRunnerPresenter')
|