Module 10 Lesson 1: The DRY Principle
·DevOps

Module 10 Lesson 1: The DRY Principle

Stop the copy-paste. Learn how to use GitLab CI/CD templates to share common jobs across 100 different projects, ensuring consistency and ease of maintenance.

Module 10 Lesson 1: The DRY Principle

In Module 2, we learned about YAML anchors for a single file. But in a big company, you have 500 files that all need the same "Build" and "Security Scan" logic. Templates are the solution.

1. What is a Template?

A template is a partial YAML file stored in a central location. Instead of writing 100 lines of build logic in every repo, you just "Include" it.

# Inside your project's .gitlab-ci.yml
include:
  - project: 'my-org/shared-templates'
    file: '/templates/node-build.yml'

# Now you can use the jobs from that template!
build-job:
  extends: .npm_build_template

2. Types of Includes

  1. Local: Include a file from the SAME repository. (Best for splitting a giant file).
  2. Project: Include a file from ANOTHER repository in GitLab. (Best for company standards).
  3. Remote: Include any YAML file from the public internet (via URL).
  4. Template: Include official GitLab templates (like Security/SAST.gitlab-ci.yml).

3. Override and Extend

The best part about templates is that they are Overridable.

include: .base-build.yml

# Change only the timeout for this specific project
build:
  extends: .base
  timeout: 1h 30m

4. Why Use Templates?

  1. Maintenance: If the "Node version" changes from 18 to 20, you only update ONE file in the shared-templates repo. All 500 projects are updated instantly!
  2. Consistency: You ensure that EVERY project uses the exact same security flags.
  3. Speed: A new developer can setup a full production pipe in 3 lines of code.

Exercise: The Template Architect

  1. Create a "Shared-Config" project.
  2. Add a base.yml with a simple test-job.
  3. In a second project, use the include keyword to call that job.
  4. Try to "Override" the script in the second project. Which script wins? (Test it!)
  5. Search: What is the "GitLab CI/CD Catalog"? (Hint: It's the new way to discover community templates).

Summary

Templates turn your DevOps team from "Script writers" into "Platform engineers." By building a library of reusable building blocks, you ensure that your company moves faster while maintaining a high, consistent standard of quality across every project.

Next Lesson: Breaking it down: Modular Pipeline Design.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn