Skip to content
Snippets Groups Projects
Commit 484666c5 authored by René Hernández Remedios's avatar René Hernández Remedios Committed by Stan Hu
Browse files

Allow variables in cache:policy field

Update validation to support variables in addition to the previous values.
Validation of variables in cache:policy is done by Gitlab Runner at runtime
Update docs

Changelog: added
parent 1b0a88ad
No related branches found
No related tags found
1 merge request!120381Allow variables in cache policy field
......@@ -1067,11 +1067,7 @@
"type": "string",
"markdownDescription": "Determines the strategy for downloading and updating the cache. [Learn More](https://docs.gitlab.com/ee/ci/yaml/#cachepolicy)",
"default": "pull-push",
"enum": [
"pull",
"push",
"pull-push"
]
"pattern": "pull-push|pull|push|\\$\\w{1,255}"
},
"unprotect": {
"type": "boolean",
......
......@@ -261,6 +261,39 @@ cache:
key: $CI_JOB_NAME
```
### Use a variable to control a job's cache policy
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/371480) in GitLab 16.1.
To reduce duplication of jobs where the only difference is the pull policy, you can use a [CI/CD variable](../variables/index.md).
For example:
```yaml
conditional-policy:
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
variables:
POLICY: pull-push
- if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
variables:
POLICY: pull
stage: build
cache:
key: gems
policy: $POLICY
paths:
- vendor/bundle
script:
- echo "This job pulls and pushes the cache depending on the branch"
- echo "Downloading dependencies..."
```
In this example, the job's cache policy is:
- `pull-push` for changes to the default branch.
- `pull` for changes to other branches.
### Cache Node.js dependencies
If your project uses [npm](https://www.npmjs.com/) to install Node.js
......
......@@ -28,6 +28,7 @@ There are two places defined variables can be used. On the:
| [`artifacts:name`](../yaml/index.md#artifactsname) | yes | Runner | The variable expansion is made by GitLab Runner's shell environment. |
| [`before_script`](../yaml/index.md#before_script) | yes | Script execution shell | The variable expansion is made by the [execution shell environment](#execution-shell-environment) |
| [`cache:key`](../yaml/index.md#cachekey) | yes | Runner | The variable expansion is made by GitLab Runner's [internal variable expansion mechanism](#gitlab-runner-internal-variable-expansion-mechanism). |
| [`cache:policy`](../yaml/index.md#cachepolicy) | yes | Runner | The variable expansion is made by GitLab Runner's [internal variable expansion mechanism](#gitlab-runner-internal-variable-expansion-mechanism). |
| [`environment:name`](../yaml/index.md#environmentname) | yes | GitLab | Similar to `environment:url`, but the variables expansion doesn't support the following:<br/><br/>- `CI_ENVIRONMENT_*` variables.<br/>- [Persisted variables](#persisted-variables). |
| [`environment:url`](../yaml/index.md#environmenturl) | yes | GitLab | The variable expansion is made by the [internal variable expansion mechanism](#gitlab-internal-variable-expansion-mechanism) in GitLab.<br/><br/>Supported are all variables defined for a job (project/group variables, variables from `.gitlab-ci.yml`, variables from triggers, variables from pipeline schedules).<br/><br/>Not supported are variables defined in the GitLab Runner `config.toml` and variables created in the job's `script`. |
| [`environment:auto_stop_in`](../yaml/index.md#environmentauto_stop_in)| yes | GitLab | The variable expansion is made by the [internal variable expansion mechanism](#gitlab-internal-variable-expansion-mechanism) in GitLab.<br/><br/> The value of the variable being substituted should be a period of time in a human readable natural language form. See [possible inputs](../yaml/index.md#environmentauto_stop_in) for more information.|
......
......@@ -1453,6 +1453,7 @@ Must be used with `cache: paths`, or nothing is cached.
- `pull`
- `push`
- `pull-push` (default)
- [CI/CD variables](../variables/where_variables_can_be_used.md#gitlab-ciyml-file).
**Example of `cache:policy`**:
......@@ -1480,6 +1481,10 @@ faster-test-job:
- echo "Running tests..."
```
**Related topics**:
- You can [use a variable to control a job's cache policy](../caching/index.md#use-a-variable-to-control-a-jobs-cache-policy).
#### `cache:fallback_keys`
Use `cache:fallback_keys` to specify a list of keys to try to restore cache from
......
......@@ -10,7 +10,7 @@ class Cache < ::Gitlab::Config::Entry::Node
include ::Gitlab::Config::Entry::Attributable
ALLOWED_KEYS = %i[key untracked paths when policy unprotect fallback_keys].freeze
ALLOWED_POLICY = %w[pull-push push pull].freeze
ALLOWED_POLICY = /pull-push|push|pull|\$\w{1,255}*/
DEFAULT_POLICY = 'pull-push'
ALLOWED_WHEN = %w[on_success on_failure always].freeze
DEFAULT_WHEN = 'on_success'
......@@ -18,9 +18,9 @@ class Cache < ::Gitlab::Config::Entry::Node
validations do
validates :config, type: Hash, allowed_keys: ALLOWED_KEYS
validates :policy, type: String, allow_blank: true, inclusion: {
in: ALLOWED_POLICY,
message: "should be one of: #{ALLOWED_POLICY.join(', ')}"
validates :policy, type: String, allow_blank: true, format: {
with: ALLOWED_POLICY,
message: "should be a variable or one of: pull-push, push, pull"
}
with_options allow_nil: true do
......
......@@ -82,6 +82,7 @@
'pull-push' | 'pull-push'
'push' | 'push'
'pull' | 'pull'
'$VARIABLE' | '$VARIABLE'
'unknown' | 'unknown' # invalid
end
......@@ -145,6 +146,7 @@
'pull-push' | true
'push' | true
'pull' | true
'$VARIABLE' | true
'unknown' | false
end
......@@ -280,7 +282,7 @@
let(:config) { { policy: 'unknown' } }
it 'returns error' do
is_expected.to include('cache policy should be one of: pull-push, push, pull')
is_expected.to include('cache policy should be a variable or one of: pull-push, push, pull')
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment