(Side note: This is a follow-up on this https://sourceforge.net/p/ruamel-yaml/tickets/313/)
I'm building a GitLab CI pipeline by defining a .gitlab-ci.yml
file, see https://docs.gitlab.com/ee/ci/yaml/.
As my CI consists of several very similar build steps, I'm using YAML-Anchors quite heavily. For example to define common cache
and before-scripts
.
I saw that "the correct way" of merging several yaml-anchors, due to the spec, is using
befor-script: &before-script
...
cache: &cache
...
ci-step:
image: ABC
<<: [*before-script, *cache]
script: ...
However, using this also works fine with GitLab CI and IMHO is much nicer to read:
...
ci-step:
image: abc
<<: *before-script
script: ...
<<: *cache
This also enables to put different merge keys at different positions.
All is fine so far, because it is working in GitLab CI.
Now we are using https://github.com/pre-commit/pre-commit-hooks to validate YAML-files in our repository. pre-commit-hooks
is using ruamel-yaml internally for yaml-validation.
As a result, the pre-commit-hook fails with the following error message
while construction a mapping
in ".gitlab-ci.yml", line xx, column y
found duplicate key "<<"
in ".gitlab-ci.yml", line zz, column y
How can I prevent this exception from happeing if the key is equal to <<
in the ruamel-yaml library.
It would also be possible to update pre-commit-hooks to set allow_duplicate_keys = True
, see yaml-duplicate-keys.
But this would also allow other duplicate keys, which is not perfect.