Commit ac9d7929 authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'set-default-cache-key-for-jobs' into 'master'

Set default cache key to 'default' for jobs

Closes #22419

See merge request !9666
parents 74bfa890 23198320
---
title: Set default cache key to "default" for jobs
merge_request: 9666
author:
...@@ -166,10 +166,11 @@ which can be set in GitLab's UI. ...@@ -166,10 +166,11 @@ which can be set in GitLab's UI.
cached between jobs. You can only use paths that are within the project cached between jobs. You can only use paths that are within the project
workspace. workspace.
**By default the caching is enabled per-job and per-branch.** **By default caching is enabled and shared between pipelines and jobs,
starting from GitLab 9.0**
If `cache` is defined outside the scope of the jobs, it means it is set If `cache` is defined outside the scope of jobs, it means it is set
globally and all jobs will use its definition. globally and all jobs will use that definition.
Cache all files in `binaries` and `.config`: Cache all files in `binaries` and `.config`:
...@@ -202,7 +203,7 @@ rspec: ...@@ -202,7 +203,7 @@ rspec:
- binaries/ - binaries/
``` ```
Locally defined cache overwrites globally defined options. The following `rspec` Locally defined cache overrides globally defined options. The following `rspec`
job will cache only `binaries/`: job will cache only `binaries/`:
```yaml ```yaml
...@@ -213,10 +214,15 @@ cache: ...@@ -213,10 +214,15 @@ cache:
rspec: rspec:
script: test script: test
cache: cache:
key: rspec
paths: paths:
- binaries/ - binaries/
``` ```
Note that since cache is shared between jobs, if you're using different
paths for different jobs, you should also set a different **cache:key**
otherwise cache content can be overwritten.
The cache is provided on a best-effort basis, so don't expect that the cache The cache is provided on a best-effort basis, so don't expect that the cache
will be always present. For implementation details, please check GitLab Runner. will be always present. For implementation details, please check GitLab Runner.
...@@ -233,6 +239,9 @@ different jobs or even different branches. ...@@ -233,6 +239,9 @@ different jobs or even different branches.
The `cache:key` variable can use any of the [predefined variables](../variables/README.md). The `cache:key` variable can use any of the [predefined variables](../variables/README.md).
The default key is **default** across the project, therefore everything is
shared between each pipelines and jobs by default, starting from GitLab 9.0.
--- ---
**Example configurations** **Example configurations**
......
...@@ -22,6 +22,12 @@ module Gitlab ...@@ -22,6 +22,12 @@ module Gitlab
entry :paths, Entry::Paths, entry :paths, Entry::Paths,
description: 'Specify which paths should be cached across builds.' description: 'Specify which paths should be cached across builds.'
helpers :key
def value
super.merge(key: key_value)
end
end end
end end
end end
......
...@@ -11,6 +11,10 @@ module Gitlab ...@@ -11,6 +11,10 @@ module Gitlab
validations do validations do
validates :config, key: true validates :config, key: true
end end
def self.default
'default'
end
end end
end end
end end
......
...@@ -70,6 +70,12 @@ module Gitlab ...@@ -70,6 +70,12 @@ module Gitlab
true true
end end
def inspect
val = leaf? ? config : descendants
unspecified = specified? ? '' : '(unspecified) '
"#<#{self.class.name} #{unspecified}{#{key}: #{val.inspect}}>"
end
def self.default def self.default
end end
......
...@@ -29,6 +29,10 @@ module Gitlab ...@@ -29,6 +29,10 @@ module Gitlab
def relevant? def relevant?
false false
end end
def inspect
"#<#{self.class.name}>"
end
end end
end end
end end
......
...@@ -24,6 +24,20 @@ describe Gitlab::Ci::Config::Entry::Cache do ...@@ -24,6 +24,20 @@ describe Gitlab::Ci::Config::Entry::Cache do
expect(entry).to be_valid expect(entry).to be_valid
end end
end end
context 'when key is missing' do
let(:config) do
{ untracked: true,
paths: ['some/path/'] }
end
describe '#value' do
it 'sets key with the default' do
expect(entry.value[:key])
.to eq(Gitlab::Ci::Config::Entry::Key.default)
end
end
end
end end
context 'when entry value is not correct' do context 'when entry value is not correct' do
......
...@@ -60,13 +60,13 @@ describe Gitlab::Ci::Config::Entry::Factory do ...@@ -60,13 +60,13 @@ describe Gitlab::Ci::Config::Entry::Factory do
end end
context 'when creating entry with nil value' do context 'when creating entry with nil value' do
it 'creates an undefined entry' do it 'creates an unspecified entry' do
entry = factory entry = factory
.value(nil) .value(nil)
.create! .create!
expect(entry) expect(entry)
.to be_an_instance_of Gitlab::Ci::Config::Entry::Unspecified .not_to be_specified
end end
end end
......
...@@ -188,7 +188,7 @@ describe Gitlab::Ci::Config::Entry::Global do ...@@ -188,7 +188,7 @@ describe Gitlab::Ci::Config::Entry::Global do
it 'contains unspecified nodes' do it 'contains unspecified nodes' do
expect(global.descendants.first) expect(global.descendants.first)
.to be_an_instance_of Gitlab::Ci::Config::Entry::Unspecified .not_to be_specified
end end
end end
......
...@@ -31,4 +31,10 @@ describe Gitlab::Ci::Config::Entry::Key do ...@@ -31,4 +31,10 @@ describe Gitlab::Ci::Config::Entry::Key do
end end
end end
end end
describe '.default' do
it 'returns default key' do
expect(described_class.default).to eq 'default'
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment