yamllint "duplication of key" error in CI
yamllint's key-duplicates rule catches a mapping that defines the same key twice. YAML keeps only the last one, so the duplicate quietly discards your earlier value - a real bug, not just style.
What this error means
yamllint reports "duplication of key \"env\" in mapping (key-duplicates)" pointing at the second occurrence. The config parses but behaves unexpectedly because one value won.
service.yml
12:3 error duplication of key "env" in mapping (key-duplicates)Common causes
A merged or copy-pasted block repeated a key
Editing two sections that each set env: (or ports:) leaves the same key twice in one mapping.
Anchors and overrides expanded to a duplicate
A merge key or copied anchor reintroduced a key already present at the same level.
How to fix it
Merge the duplicate keys into one
- Open the mapping at the reported line and find both occurrences.
- Combine their values under a single key, or rename one if they are distinct.
- Re-run
yamllintto confirm the duplicate is gone.
# wrong: env defined twice, first is discarded
env:
A: "1"
env:
B: "2"
# right: one mapping
env:
A: "1"
B: "2"Keep key-duplicates enabled everywhere
Ensure the rule is on (it is by default) so silent overrides never ship.
rules:
key-duplicates: enableHow to prevent it
- Run yamllint in a pre-commit hook so duplicates fail before merge.
- Review large YAML diffs for repeated top-level keys.
- Prefer one mapping over anchors when they risk key collisions.