Merging/overriding values with !reference tags

In YAML, it is possible to use anchors to define a hash and then override values using the “merge” operator <<, as so:

.basic-cache: &readonly-cache
  key: cache-key
  paths: 
    - path/to/whatever
  policy: pull

my-job:
  cache:
    <<: &readonly-cache
    policy: pull-push

However, anchors can only be used in a single YAML file (no includes), which led to the invention of !reference tags. However, it appears to me that !reference tags cannot be used in a merge, and also cannot be combined with an anchor to allow overriding of properties.

Here are the syntaxes I tried (in a separate included file):

.downstream-cache:
  rw:
    <<: !reference [.basic-cache]

This fails with an uninformative error message:
“Pipeline cannot be run.
Undefined error (01H55FBTT1GRW0WJV2M76KGJ1Y)”

.downstream-cache:
  readonly: !reference [.basic-cache] &readonly-cache
  rw:
    <<: *readonly-cache
    policy: pull-push

When this file is included, GitLab responds with
“Pipeline cannot be run.
[included file]: content does not have a valid YAML syntax”

I believe this message is correct: the anchor should immediately follow the key that it is anchoring, in this case readonly. (VSCode also reports that this YAML document is invalid.) However, when I switch the order of !reference and anchor:

.downstream-cache:
  readonly: &readonly-cache !reference [.basic-cache]
  rw:
    <<: *readonly-cache
    policy: pull-push

which fixes the local syntax warning, GitLab still reports “content does not have a valid YAML syntax”.

So the question is: is there a way to attach an anchor to a property filled in via a !reference from another file, or to override one of its sub-properties another way?

We’re on a self-hosted GitLab EE v16.1.2-ee

!reference does not support overrides at the moment like YAML anchors do.

1 Like