Skip to main content
When setting up an application pipeline, it’s easy to copy-paste previous CI/CD snippets. While this speeds up the initial setup, it makes maintenance and scaling a headache. By modularizing your GitLab CI/CD pipelines, you can:
  • Eliminate duplicated code
  • Standardize workflows
  • Simplify updates as your project grows
The image outlines strategies for optimizing CI pipeline development, focusing on avoiding copy-pasting, maintenance considerations, and modularization benefits. Each strategy is represented with an icon and a colored background.
GitLab CI/CD provides three powerful mechanisms for DRY pipelines:
The image is a visual representation of GitLab CI/CD concepts, featuring five colored cards labeled "Extends Keyword," "Anchors," "Reference Tags," "Templates," and "CI/CD Components," each with an icon.

Hidden Jobs

Jobs prefixed with a dot (.) are hidden: they never run on their own but serve as reusable templates.
Use hidden jobs for:
  • Common cache rules
  • Shared before_script steps
  • Disabling jobs without deleting them

1. The extends Keyword

Use extends to inherit from hidden or other jobs—even across multiple files. GitLab merges parent and child definitions, with child values overriding duplicates.

Example: Node.js Jobs

Any update to .base_nodejs_job automatically applies to all jobs that extend it, keeping your pipeline DRY and consistent.

2. YAML Anchors

YAML anchors (&) and aliases (*) let you reuse blocks within the same file.
YAML anchors do not work across multiple files. Use extends or !reference for cross-file reuse.

3. Reference Tags

GitLab’s custom !reference tag imports specific sections from other jobs even across files.
!reference tags require GitLab Runner 12.6+ and allow fine-grained imports of job snippets.

Further Reading

By leveraging hidden jobs with extends, YAML anchors, and !reference tags, you can keep your GitLab CI/CD pipelines maintainable, scalable, and free from repetition.

Watch Video