Learn to reuse configuration in GitLab CI/CD pipelines using the extends keyword to eliminate duplication and streamline job definitions.
Learn how to eliminate duplication in your GitLab CI/CD pipelines by reusing configuration with the extends keyword. This guide covers three primary strategies:
Method
Description
Example
extends
Inherit settings from hidden jobs (templates)
.rspec → rspec 1
YAML anchors & aliases
Reuse blocks of config (scripts, variables, etc.)
&default_scripts / *default_scripts
Reference tags
Share tags or other fields across multiple jobs
tags: &common_tags / tags: *common_tags
You can combine include with extends to pull in templates from external files and inherit from them in one step.
Imagine a pipeline where the test stage contains two nearly identical NodeJS jobs: unit_testing and code_coverage. To speed up feedback, other stages are commented out.
When you commit these changes, both unit_testing and code_coverage run in parallel with the shared setup:
Copy
Ask AI
$ npm install$ npm test# …Job succeeded
This approach centralizes maintenance: updating .prepare_nodejs_environment applies to all linked jobs.For more on merging hidden jobs or combining include with extends, see the GitLab CI/CD documentation on extends.