Operations 6 min read

Master YAML Anchors to Eliminate Repetitive Config in CI/CD Pipelines

This guide explains YAML anchors, aliases, and merge keys, showing how they reduce duplication, improve readability, and simplify maintenance in Docker Compose and GitLab CI configurations through clear examples and practical exercises.

Ops Development & AI Practice
Ops Development & AI Practice
Ops Development & AI Practice
Master YAML Anchors to Eliminate Repetitive Config in CI/CD Pipelines

Introduction

YAML provides built‑in mechanisms—anchors, aliases, and merge keys—to avoid duplication in CI/CD pipelines, Kubernetes manifests, Docker Compose files, and similar configurations.

Core Elements

Anchor (&) : tags a node for later reference. Syntax: prepend & and a name to a mapping.

Alias (*) : references a previously defined anchor. Syntax: * followed by the anchor name.

Merge key (<<) : special key that merges one or more mappings into the current mapping, typically used together with an alias.

Simple Example

# Define an anchor
base_user_template: &base_user
  age: 30
  city: "San Francisco"

# Use alias
user_a: *base_user

# Merge with overrides
user_b:
  <<: *base_user
  name: "Bob"
  city: "New York"

Result: user_a resolves to {age: 30, city: "San Francisco"}. user_b resolves to {age: 30, city: "New York", name: "Bob"}.

Practical Exercise: Docker Compose → GitLab CI

Scenario 1 – Simplify Docker Compose

# Common configuration anchor
x-common-config: &common-config
  restart: always
  environment:
    - REDIS_HOST=redis
    - POSTGRES_HOST=postgres

services:
  api_service:
    <<: *common-config
    build: ./api
    ports:
      - "8000:8000"

  worker_service:
    <<: *common-config
    build: ./worker
    environment:
      - QUEUE_NAME=high_priority
      - REDIS_HOST=redis
      - POSTGRES_HOST=postgres

Both services inherit restart and environment from the anchor; updating the anchor updates all services.

Scenario 2 – GitLab CI

# Anchor for unstable‑branch jobs
.unstable_template: &unstable_template
  tags:
    - app-web
  only:
    - unstable

build_unstable:
  <<: *unstable_template
  stage: build
  script:
    - echo "Building for unstable..."

deploy_unstable:
  <<: *unstable_template
  stage: deploy
  script:
    - echo "Deploying to unstable..."

The &unstable_template anchor encapsulates common settings; each job merges it with <<: *unstable_template, keeping the CI file DRY.

UML Modeling of Definition‑Reference Relationship

A component diagram can visualize an anchor as a reusable blueprint and aliases as instances that either merge or directly reference the blueprint.

UML diagram of YAML anchor relationships
UML diagram of YAML anchor relationships

Conclusion

Reduce duplication : define common configuration once.

Improve readability : separate shared logic from service‑specific details.

Simplify maintenance : changing the anchor propagates to all references automatically.

CI/CDConfiguration ManagementGitLab CIYAMLDocker ComposeAnchors
Ops Development & AI Practice
Written by

Ops Development & AI Practice

DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.