2021-04-17 20:07:23 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Ci
|
|
|
|
module Build
|
|
|
|
class Cache
|
|
|
|
include ::Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
def initialize(cache, pipeline)
|
2021-06-08 01:23:25 +05:30
|
|
|
cache = Array.wrap(cache)
|
2023-03-04 22:38:38 +05:30
|
|
|
@cache = cache.map.with_index do |cache, index|
|
2023-07-09 08:55:56 +05:30
|
|
|
prefix = cache_prefix(cache, index)
|
2023-06-20 00:43:36 +05:30
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
Gitlab::Ci::Pipeline::Seed::Build::Cache
|
|
|
|
.new(pipeline, cache, prefix)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_attributes
|
|
|
|
strong_memoize(:cache_attributes) do
|
2021-06-08 01:23:25 +05:30
|
|
|
if @cache.empty?
|
|
|
|
{}
|
2021-04-17 20:07:23 +05:30
|
|
|
else
|
2021-06-08 01:23:25 +05:30
|
|
|
{ options: { cache: @cache.map(&:attributes) } }
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-06-20 00:43:36 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
# The below method fixes a bug related to incorrect caches being used
|
|
|
|
# For more details please see: https://gitlab.com/gitlab-org/gitlab/-/issues/388374
|
2023-06-20 00:43:36 +05:30
|
|
|
def cache_prefix(cache, index)
|
|
|
|
files = cache.dig(:key, :files) if cache.is_a?(Hash) && cache[:key].is_a?(Hash)
|
|
|
|
|
|
|
|
return index if files.blank?
|
|
|
|
|
|
|
|
filenames = files.map { |file| file.split('.').first }.join('_')
|
|
|
|
|
|
|
|
"#{index}_#{filenames}"
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|