What Are YAML Anchors?
YAML anchors let you define a named block of configuration that can be duplicated or inherited later. In GitLab CI/CD, you typically combine anchors with hidden jobs (names starting with a dot) to build templates.Hidden jobs are not executed directly. They serve as templates when you use the
<<: merge key to inherit their configuration.Basic Anchor Example
Merging Entire Job Configurations
You can reuse a full job template by defining it as a hidden job and then merging it into other jobs:test1 and test2 inherit image: ruby:2.6 and the two services from the hidden .job_template.
Combining Multiple Anchors
You can break a job template into smaller anchors—for example, separate anchors forscript, services, or tags—and then merge only the pieces you need:
test_postgres inherits the base script and tags, then overrides services.
Real-World Pipeline: Reusing Deployment Configuration
Bothk8s_dev_deploy and k8s_stage_deploy share:
alpine:3.7as the job image- No dependencies
- Identical
before_scriptsteps to installkubectlandgettext
Dev Deploy Job
Stage Deploy Job
image, dependencies, and before_script steps without duplication.

Visualizing the Pipeline
On the CI/CD > Pipelines page or the Visualization tab, confirm that:k8s_dev_deployruns immediately afterdocker_pushthanks toneeds:k8s_stage_deployis manual (when: manual)- Both jobs share the anchored configuration
