
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
- Local: Include a file from the SAME repository. (Best for splitting a giant file).
- Project: Include a file from ANOTHER repository in GitLab. (Best for company standards).
- Remote: Include any YAML file from the public internet (via URL).
- 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?
- Maintenance: If the "Node version" changes from 18 to 20, you only update ONE file in the
shared-templatesrepo. All 500 projects are updated instantly! - Consistency: You ensure that EVERY project uses the exact same security flags.
- Speed: A new developer can setup a full production pipe in 3 lines of code.
Exercise: The Template Architect
- Create a "Shared-Config" project.
- Add a
base.ymlwith a simpletest-job. - In a second project, use the
includekeyword to call that job. - Try to "Override" the script in the second project. Which script wins? (Test it!)
- 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.