Skip to main content
In this lesson you’ll learn how YAML anchors let you define reusable configuration once and reference it across multiple GitLab CI/CD jobs. Anchors reduce duplication, keep your .gitlab-ci.yml DRY, and make it easier to maintain shared settings like container images, before_script commands, caches, and dependencies.

Quick anchor primer

  • &name — define an anchor
  • *name — reference an anchor
  • <<: — YAML merge key to merge an anchored mapping into another mapping
Basic example — define an anchor and reference it:
You can also merge (extend) an entire job configuration using the YAML merge key (<<:). Hidden jobs (names that start with a dot) are commonly used in GitLab CI as templates:
Anchors can be applied at different granularities: an entire job template, or specific keywords like services or before_script. Example with a job template anchor and service-list anchors:
Use YAML anchors (&/* and <<:) to merge YAML fragments at parse time. Use extends when you want GitLab-specific job inheritance that can combine rules, needs, or variables in GitLab-specific ways. You can use anchors and extends together where appropriate.

Apply anchors to a GitLab pipeline — a real example

In this project we already have a hidden job .prepare_nodejs_environment used by unit_testing and code_coverage. We want to reuse the same image, dependencies, and a before_script (that installs kubectl and envsubst) across both the dev and stage Kubernetes deployment jobs. Example slice of the pipeline (abbreviated):
Visualize your pipeline to confirm the current job graph and stages:
A screenshot of the GitLab Pipeline Editor showing a visual pipeline with stages labeled "test", "containerization", "dev-deploy" and "stage-deploy." It displays jobs like unit_testing, docker_build, docker_push and k8s_stage_deploy, and a banner noting "Your changes have been successfully committed."

Example: a simple dev deploy job using needs

This k8s_dev_deploy job is intended to start as soon as docker_push completes. Using needs makes that explicit:

Create a reusable hidden anchor for the deploy environment

Define a hidden job near the top of the file to hold the shared deploy configuration. Use a clear anchor name and reference it from your deploy jobs:
Now merge that anchor into the dev and stage deploy jobs using the YAML merge key (<<:) with the alias *kubernetes_deploy_job. Add job-specific fields (like stage, needs, when, and the script) after the merge so they can override or extend the merged values. k8s_dev_deploy (merge + job-specific script and needs):
k8s_stage_deploy (merge + manual trigger + stage-specific actions):

Notes on correctness and common pitfalls

Confirm the environments and agent After committing the changes to your branch a pipeline will be triggered. Validate environments and agent connectivity in GitLab to ensure deployments succeed:
Screenshot of a GitLab "Environments" page listing active environments (development, staging) with deployment status badges, job/branch details, and the left-side project navigation.
A screenshot of the GitLab web UI on the Kubernetes clusters page, showing an agent section with a connected agent named "kk-gitlab-agent" and a banner about Google Cloud credits. The left sidebar displays navigation items like Code, Build, Secure, Deploy and Kubernetes clusters.

Pipeline run and job logs

When the pipeline runs, both k8s_dev_deploy and k8s_stage_deploy will use the alpine:3.7 image and the shared before_script from the anchor. An abbreviated example of the job log showing before_script actions:
Final pipeline visualization After commits and job execution, the pipeline visualization will reflect the needs dependency (the dev deploy starts when docker_push completes) and both deployment jobs will report their statuses:
A GitLab CI/CD pipeline screenshot titled "Solar System NodeJS Pipeline" showing a passed run with containerization, dev-deploy, and stage-deploy stages and green check marks for completed jobs. The left sidebar shows the project navigation with Pipelines highlighted.
That’s how to use YAML anchors in GitLab CI to reuse deployment configuration across multiple jobs. Another related technique is the !reference tag, which behaves differently from anchors and extends and can be useful for more advanced reuse patterns.

Watch Video