Anchor does not work for global cache

I tried to use an anchor for a global cache definition but got the following error:
This GitLab CI configuration is invalid: jobs:build:cache config contains unknown keys: <<

Why does anchor not work in this case?

default:
    cache: &global_cache
    - key: "$CI_COMMIT_REF_NAME"
      policy: pull
      paths:
        - build
    - key:
        files:
          - gradle/wrapper/gradle-wrapper.properties
      policy: pull          
      paths: 
        - .gradle/wrapper
        - .gradle/notifications   
 
build:
  cache:
    <<: *global_cache
    policy: push
  stage: build
  script: ./gradlew --build-cache  assemble
...

Thanks

Because it’s not supported.

I ran into this same error today–anchors are advised for use in this exact scenario in the docs: Caching in GitLab CI/CD | GitLab

Can the docs be updated to advise a different course of action if this is unsupported? Thanks.

@elitree
issue in OP’s post is not in the anchors, it is in the way how he defined cache. He has multiple key in cache in default, but none of that in the job spec. The example in docs is for a single key and is correct.