MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Gitlab – Complete Details of .gitlab-ci.yml

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.yml at the root is processed directly.
  • You must use include to 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.

KeywordLevelPurpose
includeGlobalPulls in other YAML files before the pipeline is created
stagesGlobalOrdered list of stages. Jobs in the same stage run in parallel
variablesGlobal or jobCI/CD variables available to jobs
defaultGlobalDefault values (for example image, before_script, tags) inherited by every job
workflowGlobalrules that decide whether a pipeline is created at all
stage, script, imageJobWhere the job runs, what it executes and in which container image
rulesJobConditions that add the job to a pipeline or skip it
needsJobLets a job start as soon as specific jobs finish, without waiting for its whole stage
extendsJobInherits configuration from another job or hidden job (names starting with .)
artifacts, cacheJobFiles 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

MethodSourceTypical useWatch out for
localSame repository and same commitSplitting one project's pipelinePaths are relative to the repository root. Wildcards such as ci/*.yml are supported
projectAnother project on the same GitLab instanceCentral templates shared by a team or groupThe user running the pipeline needs read access to that project. Pin ref to a tag for stability
remoteAny HTTP(S) URLTemplates hosted outside GitLabThe URL must be reachable without authentication. Its content can change without warning
templateTemplates shipped with GitLabSAST, dependency scanning, Auto DevOpsTemplate content changes when GitLab is upgraded
componentCI/CD component project, often from the CI/CD CatalogVersioned, reusable pipeline building blocks with inputsRequires 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.yml is 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, stages or rules are 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.

  1. In the project, open Build > Pipeline editor.
  2. Use the Validate tab to run a lint check and simulate a pipeline for a branch.
  3. 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.
  4. From a terminal, the GitLab CLI can lint the current file with glab ci lint.
glab ci lint

Common errors and fixes

Error (paraphrased)CauseFix
Local file ci/build.yml does not existWrong path or letter case, or the file is not committed on this branchCheck the path from the repository root and commit the file
Project not found or access deniedThe pipeline user cannot read the template project, or the path is wrongUse the full group/project path and grant at least read access
Job's chosen stage does not existThe job's stage is not in the final stages listAdd the stage to stages in the main file
Remote file could not be fetchedThe URL needs authentication, times out, or returns something that is not YAMLHost the file publicly or switch to include:project
Unknown alias or anchorThe file references a YAML anchor defined in another fileReplace 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.yml is 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 include keyword, 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, workflow and global variables in the main file, so the pipeline's overall layout is visible in one place.
  • Pin include:project and component to a tag or SHA. With a branch reference, template changes reach every project that uses the template immediately.
  • Avoid include:remote for anything security-sensitive, because you do not control what that URL returns.
  • Put reusable logic in hidden jobs and expose options through inputs instead 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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
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.

Related Posts

Gitlab – Top 30 GitLab Pipeline Automation FAQ

Here’s a curated set of 30 most frequently asked real-world questions about GitLab CI/CD pipeline automation, with clear and concise answers โ€” perfect for training sessions or…

Read More

GitLab CI/CD Pipeline Configuration Keywords

Below is a comprehensive table listing the key stanzas (keywords) used in GitLab CI/CD pipeline configuration files (.gitlab-ci.yml). These keywords are categorized based on their scope and…

Read More

GitLab Tutorials: Example of .gitlab-ci.yml

.gitlab-ci.yml file to run with Docker runner Build a Docker image with CI/CD and push to the GitLab registry Rajesh Kumar I’m Rajesh Kumar, a DevOps, SRE,…

Read More

Azure Devops Pipeline YAML Example

This page shows a minimal Azure Pipelines YAML file that builds a Docker image with the Docker@2 task on a Microsoft-hosted Ubuntu agent each time code is…

Read More

Kubernetes ReplicaSet Explained: API Resource, YAML Example, and Use Cases

Rajesh Kumar 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…

Read More

How to Install and Configure pre-commit on macOS, Windows, and Linux: Complete Step-by-Step Guide

SEO Title: How to Install and Configure pre-commit on Mac, Windows & Linux (2026) Meta Description: Learn how to install and configure pre-commit Git hooks on macOS,…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x