From 771b97394a16ae8dcd9acc84ab6a076f68726fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Fri, 22 Dec 2017 23:06:15 +0100 Subject: [PATCH] Use Project.cache_index in Build#cache --- app/models/ci/build.rb | 6 +++++- spec/models/ci/build_spec.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index 83fe23606d1..e4ca74f87f2 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -461,7 +461,11 @@ module Ci end def cache - [options[:cache]] + if options[:cache] && project.cache_index + options[:cache].merge(key: "#{options[:cache][:key]}:#{project.cache_index}") + else + [options[:cache]] + end end def credentials diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb index 871e8b47650..96513281994 100644 --- a/spec/models/ci/build_spec.rb +++ b/spec/models/ci/build_spec.rb @@ -255,6 +255,42 @@ describe Ci::Build do end end + describe '#cache' do + let(:options) { { cache: { key: "key", paths: ["public"], policy: "pull-push" } } } + + subject { build.cache } + + context 'when build has cache' do + before do + allow(build).to receive(:options).and_return(options) + end + + context 'when project has cache_index' do + before do + allow_any_instance_of(Project).to receive(:cache_index).and_return(1) + end + + it { is_expected.to include(key: "key:1") } + end + + context 'when project does not have cache_index' do + before do + allow_any_instance_of(Project).to receive(:cache_index).and_return(nil) + end + + it { is_expected.to eq([options[:cache]]) } + end + end + + context 'when build does not have cache' do + before do + allow(build).to receive(:options).and_return({}) + end + + it { is_expected.to eq([nil]) } + end + end + describe '#depends_on_builds' do let!(:build) { create(:ci_build, pipeline: pipeline, name: 'build', stage_idx: 0, stage: 'build') } let!(:rspec_test) { create(:ci_build, pipeline: pipeline, name: 'rspec', stage_idx: 1, stage: 'test') }