GitLab builds every pipeline from one entry-point configuration file, .gitlab-ci.yml by default, but that file can pull in any number of other YAML files with the include keyword. This page explains the four include methods shown below. It then covers what the original leaves out: what the included files should contain, how GitLab merges and overrides keys, conditional includes, reusable inputs, how to validate the merged result, and the errors you are likely to hit.
By default, GitLab only uses one primary .gitlab-ci.yml file located at the root of your repository. However, you can modularize and use multiple CI/CD YAML files by including them from other locations. Here’s how it works:
โ
Ways to Use Multiple .gitlab-ci.yml Files
You can split your CI/CD logic into multiple files and include them in the main .gitlab-ci.yml using the include keyword:
1. Include Local Files
include:
- local: 'ci-templates/build.yml'
- local: 'ci-templates/deploy.yml'
Code language: PHP (php)
2. Include from Other Projects
include:
- project: 'mygroup/ci-templates'
file: '/templates/test.yml'
ref: main
Code language: PHP (php)
3. Include from Remote URLs
include:
- remote: 'https://example.com/gitlab-ci-templates/security.yml'
Code language: PHP (php)
4. Include Templates Provided by GitLab
include:
- template: 'Security/SAST.gitlab-ci.yml'
Code language: PHP (php)
๐ Merge Strategy
When GitLab reads your .gitlab-ci.yml, it merges the included files before executing the pipeline. All the jobs across all included files are considered part of the same pipeline.
๐ ๏ธ Use Case Example
# .gitlab-ci.yml at the root
include:
- local: 'ci/build.yml'
- local: 'ci/test.yml'
- local: 'ci/deploy.yml'
stages:
- build
- test
- deploy
Code language: PHP (php)
Each of the build.yml, test.yml, and deploy.yml would define jobs related to their stage.
๐ง Important Notes
- Only one
.gitlab-ci.ymlat the root is processed directly. - You must use
includeto leverage other YAMLs. - GitLab 12.3+ supports multiple includes.
The main keywords in a .gitlab-ci.yml file
Before you split a pipeline across files, you need to know which keys are global and which belong to jobs. Global keys apply to the whole pipeline. GitLab treats any other top-level key that is not reserved as a job name.
| Keyword | Level | Purpose |
|---|---|---|
include | Global | Pulls in other YAML files before the pipeline is created |
stages | Global | Ordered list of stages. Jobs in the same stage run in parallel |
variables | Global or job | CI/CD variables available to jobs |
default | Global | Default values (for example image, before_script, tags) inherited by every job |
workflow | Global | rules that decide whether a pipeline is created at all |
stage, script, image | Job | Where the job runs, what it executes and in which container image |
rules | Job | Conditions that add the job to a pipeline or skip it |
needs | Job | Lets a job start as soon as specific jobs finish, without waiting for its whole stage |
extends | Job | Inherits configuration from another job or hidden job (names starting with .) |
artifacts, cache | Job | Files passed to later jobs, and files reused between pipelines |
Note: Hidden jobs have names that start with a dot, such as .docker-base, and never run. They exist only to be reused through extends or !reference, which makes them a good fit for shared template files.
A complete multi-file example
The use case example above shows only the root file. Here is a working layout that also shows what goes in the included files, so the pipeline actually has jobs to run.
repo/
โโโ .gitlab-ci.yml
โโโ ci/
โโโ base.yml
โโโ build.yml
โโโ test.yml
โโโ deploy.yml
# .gitlab-ci.yml
stages:
- build
- test
- deploy
include:
- local: ci/base.yml
- local: ci/build.yml
- local: ci/test.yml
- local: ci/deploy.ymlCode language: PHP (php)
# ci/base.yml - shared, non-running configuration
.python-base:
image: python:3.12-slim
before_script:
- pip install -r requirements.txt
# ci/build.yml
build-package:
stage: build
extends: .python-base
script:
- python -m build
artifacts:
paths:
- dist/
# ci/test.yml
unit-tests:
stage: test
extends: .python-base
script:
- pytest -q
# ci/deploy.yml
deploy-staging:
stage: deploy
script:
- ./scripts/deploy.sh staging
environment: staging
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCHCode language: PHP (php)
extends: .python-base works across files because GitLab merges all includes into one configuration before it resolves extends. Plain YAML anchors (&anchor and *anchor) do not work across files, because the YAML parser resolves anchors one file at a time. Use extends or !reference for anything shared between files.
Choosing an include method
| Method | Source | Typical use | Watch out for |
|---|---|---|---|
local | Same repository and same commit | Splitting one project's pipeline | Paths are relative to the repository root. Wildcards such as ci/*.yml are supported |
project | Another project on the same GitLab instance | Central templates shared by a team or group | The user running the pipeline needs read access to that project. Pin ref to a tag for stability |
remote | Any HTTP(S) URL | Templates hosted outside GitLab | The URL must be reachable without authentication. Its content can change without warning |
template | Templates shipped with GitLab | SAST, dependency scanning, Auto DevOps | Template content changes when GitLab is upgraded |
component | CI/CD component project, often from the CI/CD Catalog | Versioned, reusable pipeline building blocks with inputs | Requires a component project with published releases |
include:project can load several files from the same project. Pin it to a tag or commit SHA rather than a moving branch:
include:
- project: mygroup/ci-templates
ref: v1.4.0
file:
- /templates/test.yml
- /templates/lint.yml
- component: $CI_SERVER_FQDN/mygroup/components/secret-scan@1.0.0Code language: JavaScript (javascript)
How GitLab merges included files
The merge strategy above says GitLab combines included files before the pipeline runs. The rules that decide which value wins matter just as much:
- Included files are merged in the order they are listed. The main
.gitlab-ci.ymlis applied last, so its values override anything included. - Hashes (maps), such as a job definition or
variables, are deep-merged key by key. - Arrays such as
script,stagesorrulesare replaced, never concatenated. - Included files can include other files (nested includes), up to depth and total-count limits set by the instance.
# ci/test.yml
unit-tests:
stage: test
image: python:3.12-slim
script:
- pytest -q
variables:
LOG_LEVEL: info
# .gitlab-ci.yml
include:
- local: ci/test.yml
unit-tests:
variables:
LOG_LEVEL: debugCode language: PHP (php)
The resulting unit-tests job keeps the image, stage and script from ci/test.yml but runs with LOG_LEVEL: debug. This is a clean way to adjust a shared template for one project. If the main file defined script: instead, it would replace the whole script list.
Note: Define stages once, in the main file. If an included file also defines stages, the later definition replaces the earlier one entirely. That can leave jobs pointing at a stage that no longer exists.
Conditional includes and reusable inputs
An include can have its own rules, so GitLab loads a file only when it is relevant. Only if, exists and changes are supported here. You can only use variables that are known when the pipeline is created: predefined, project, group and pipeline variables, but not job variables.
include:
- local: ci/deploy.yml
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
- local: ci/docker.yml
rules:
- exists:
- DockerfileCode language: PHP (php)
To reuse one template with different values, declare spec:inputs in a header section separated by ---, then reference the inputs with $[[ inputs.name ]]. GitLab resolves inputs when it builds the configuration, before any job runs.
# templates/deploy.yml
spec:
inputs:
environment:
default: staging
---
deploy-$[[ inputs.environment ]]:
stage: deploy
script:
- ./scripts/deploy.sh $[[ inputs.environment ]]
environment: $[[ inputs.environment ]]
# .gitlab-ci.yml
include:
- local: templates/deploy.yml
inputs:
environment: productionCode language: PHP (php)
Validating the merged configuration
Because GitLab assembles the pipeline from several files, check the final merged result, not just each file on its own.
- In the project, open Build > Pipeline editor.
- Use the Validate tab to run a lint check and simulate a pipeline for a branch.
- Open the Full configuration tab to see every include expanded into one YAML document. This is the quickest way to see which value won a merge.
- From a terminal, the GitLab CLI can lint the current file with
glab ci lint.
glab ci lint
Common errors and fixes
| Error (paraphrased) | Cause | Fix |
|---|---|---|
Local file ci/build.yml does not exist | Wrong path or letter case, or the file is not committed on this branch | Check the path from the repository root and commit the file |
| Project not found or access denied | The pipeline user cannot read the template project, or the path is wrong | Use the full group/project path and grant at least read access |
| Job's chosen stage does not exist | The job's stage is not in the final stages list | Add the stage to stages in the main file |
| Remote file could not be fetched | The URL needs authentication, times out, or returns something that is not YAML | Host the file publicly or switch to include:project |
| Unknown alias or anchor | The file references a YAML anchor defined in another file | Replace anchors with extends or !reference |
Corrections to the original
- The entry file does not have to be at the repository root. The root
.gitlab-ci.ymlis only the default. Under Settings > CI/CD > General pipelines, you can set the CI/CD configuration file to another path or file name, or even to a file in a different project. - The version note is outdated. The
includekeyword, including multiple and nested includes, has been available in all GitLab tiers for many major releases. Any supported GitLab version supports it, so there is no version to check. - The merge description is incomplete. GitLab does more than collect jobs: keys in the main file override included keys, maps are deep-merged and arrays are replaced, as described above.
- The use case example has no jobs. It cannot run until the included files define jobs. The complete example above shows the missing files.
Best practices
- Keep
stages,workflowand globalvariablesin the main file, so the pipeline's overall layout is visible in one place. - Pin
include:projectandcomponentto a tag or SHA. With a branch reference, template changes reach every project that uses the template immediately. - Avoid
include:remotefor anything security-sensitive, because you do not control what that URL returns. - Put reusable logic in hidden jobs and expose options through
inputsinstead of copying jobs. - Check the Full configuration view after every template change to confirm which values won the merge.
- To learn the wider pipeline model, see What is GitLab CI/CD and use cases of GitLab CI/CD?.
Frequently asked questions
Can a GitLab project have more than one .gitlab-ci.yml file?
Only one file is the pipeline entry point, but it can include any number of other YAML files with include. These files can have any name, such as ci/build.yml.
Does the main .gitlab-ci.yml override included files?
Yes. The main file is merged last, so its keys override included values. Maps are deep-merged, and arrays such as script are replaced completely.
Why does my YAML anchor not work in an included file?
YAML anchors are resolved within a single file, before GitLab merges includes. Use extends or !reference to share configuration across files.
How do I see the final merged pipeline configuration?
Open Build > Pipeline editor and select the Full configuration tab. It shows every include expanded into a single YAML document.
What is the difference between include:project and CI/CD components?
include:project loads any YAML file from another project. Components are versioned templates that take inputs, are published from a component project and are referenced with include:component.
Related tutorials
- What is GitLab CI/CD and use cases of GitLab CI/CD?
- Gitlab โ Top 30 GitLab Pipeline Automation FAQ
- GitLab for Practical Pipelines, Secure Delivery, and Faster Releases
- GitLab Hierarchy: Organization, Namespace, Group & Project
- More GitLab tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals