Make it possible to inherit global cache config, but override specific settings per job without anchors

Problem to solve

The advice in the docs is to use an anchor to override the global cache config for a single job:

cache: &global_cache
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
      - public/
      - vendor/
    policy: pull-push

job:
  cache:
    # inherit all global cache settings
    <<: *global_cache
    # override the policy
    policy: pull

This is not possible if the global cache config is specified in a different YAML file that is included:

### FILE: template.yml

cache: &global_cache
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
      - public/
      - vendor/
    policy: pull-push

### FILE: .gitlab-ci.yml that includes template.yml

job:
  cache:
    # inherit all global cache settings
    <<: *global_cache
    # override the policy
    policy: pull

I originally expected that this would work, but it just overwrites the global cache config:

### FILE: template.yml

cache: &global_cache
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/
      - public/
      - vendor/
    policy: pull-push

### FILE: .gitlab-ci.yml that includes template.yml

job:
  # I expected this to inherit the global config and override `policy`
  cache:
    policy: pull

Thinking about it, it makes sense that this wouldn't work, as the job is specifying it's own cache, which is something it needs to be able to do independent of whether using include or not.

Is there a way forward that would let the job modify the global cache config without anchors and without having to duplicate the config?

Assignee Loading
Time tracking Loading