1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
|
---
stage: Verify
group: Pipeline Authoring
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://handbook.gitlab.com/handbook/product/ux/technical-writing/#assignments
---
# Use CI/CD configuration from other files
DETAILS:
**Tier:** Free, Premium, Ultimate
**Offering:** GitLab.com, Self-managed, GitLab Dedicated
You can use [`include`](index.md#include) to include external YAML files in your CI/CD jobs.
## Include a single configuration file
To include a single configuration file, use `include` by itself with a single file
with either of these syntax options:
- On the same line:
```yaml
include: 'my-config.yml'
```
- As a single item in an array:
```yaml
include:
- 'my-config.yml'
```
If the file is a local file, the behavior is the same as [`include:local`](index.md#includelocal).
If the file is a remote file, it is the same as [`include:remote`](index.md#includeremote).
## Include an array of configuration files
You can include an array of configuration files:
- If you do not specify an `include` type, each array item defaults to [`include:local`](index.md#includelocal)
or [`include:remote`](index.md#includeremote), as needed:
```yaml
include:
- 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
- 'templates/.after-script-template.yml'
```
- You can define a single item array:
```yaml
include:
- remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
```
- You can define an array and explicitly specify multiple `include` types:
```yaml
include:
- remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
- local: 'templates/.after-script-template.yml'
- template: Auto-DevOps.gitlab-ci.yml
```
- You can define an array that combines both default and specific `include` types:
```yaml
include:
- 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
- 'templates/.after-script-template.yml'
- template: Auto-DevOps.gitlab-ci.yml
- project: 'my-group/my-project'
ref: main
file: 'templates/.gitlab-ci-template.yml'
```
## Use `default` configuration from an included configuration file
You can define a [`default`](index.md#default) section in a
configuration file. When you use a `default` section with the `include` keyword, the defaults apply to
all jobs in the pipeline.
For example, you can use a `default` section with [`before_script`](index.md#before_script).
Content of a custom configuration file named `/templates/.before-script-template.yml`:
```yaml
default:
before_script:
- apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev nodejs
- gem install bundler --no-document
- bundle install --jobs $(nproc) "${FLAGS[@]}"
```
Content of `.gitlab-ci.yml`:
```yaml
include: 'templates/.before-script-template.yml'
rspec1:
script:
- bundle exec rspec
rspec2:
script:
- bundle exec rspec
```
The default `before_script` commands execute in both `rspec` jobs, before the `script` commands.
## Override included configuration values
When you use the `include` keyword, you can override the included configuration values to adapt them
to your pipeline requirements.
The following example shows an `include` file that is customized in the
`.gitlab-ci.yml` file. Specific YAML-defined variables and details of the
`production` job are overridden.
Content of a custom configuration file named `autodevops-template.yml`:
```yaml
variables:
POSTGRES_USER: user
POSTGRES_PASSWORD: testing_password
POSTGRES_DB: $CI_ENVIRONMENT_SLUG
production:
stage: production
script:
- install_dependencies
- deploy
environment:
name: production
url: https://$CI_PROJECT_PATH_SLUG.$KUBE_INGRESS_BASE_DOMAIN
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
```
Content of `.gitlab-ci.yml`:
```yaml
include: 'https://company.com/autodevops-template.yml'
default:
image: alpine:latest
variables:
POSTGRES_USER: root
POSTGRES_PASSWORD: secure_password
stages:
- build
- test
- production
production:
environment:
url: https://domain.com
```
The `POSTGRES_USER` and `POSTGRES_PASSWORD` variables
and the `environment:url` of the `production` job defined in the `.gitlab-ci.yml` file
override the values defined in the `autodevops-template.yml` file. The other keywords
do not change. This method is called *merging*.
### Merge method for `include`
The `include` configuration merges with the main configuration file with this process:
- Included files are read in the order defined in the configuration file, and
the included configuration is merged together in the same order.
- If an included file also uses `include`, that nested `include` configuration is merged first (recursively).
- If parameters overlap, the last included file takes precedence when merging the configuration
from the included files.
- After all configuration added with `include` is merged together, the main configuration
is merged with the included configuration.
This merge method is a _deep merge_, where hash maps are merged at any depth in the
configuration. To merge hash map "A" (that contains the configuration merged so far) and "B" (the next piece
of configuration), the keys and values are processed as follows:
- When the key only exists in A, use the key and value from A.
- When the key exists in both A and B, and their values are both hash maps, merge those hash maps.
- When the key exists in both A and B, and one of the values is not a hash map, use the value from B.
- Otherwise, use the key and value from B.
For example, with a configuration that consists of two files:
- The `.gitlab-ci.yml` file:
```yaml
include: 'common.yml'
variables:
POSTGRES_USER: username
test:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: manual
artifacts:
reports:
junit: rspec.xml
```
- The `common.yml` file:
```yaml
variables:
POSTGRES_USER: common_username
POSTGRES_PASSWORD: testing_password
test:
rules:
- when: never
script:
- echo LOGIN=${POSTGRES_USER} > deploy.env
- rake spec
artifacts:
reports:
dotenv: deploy.env
```
The merged result is:
```yaml
variables:
POSTGRES_USER: username
POSTGRES_PASSWORD: testing_password
test:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: manual
script:
- echo LOGIN=${POSTGRES_USER} > deploy.env
- rake spec
artifacts:
reports:
junit: rspec.xml
dotenv: deploy.env
```
In this example:
- Variables are only evaluated after all the files are merged together. A job in an included file
might end up using a variable value defined in a different file.
- `rules` is an array so it cannot be merged. The top-level file takes precedence.
- `artifacts` is a hash map so it can be deep merged.
## Override included configuration arrays
You can use merging to extend and override configuration in an included template, but
you cannot add or modify individual items in an array. For example, to add
an additional `notify_owner` command to the extended `production` job's `script` array:
Content of `autodevops-template.yml`:
```yaml
production:
stage: production
script:
- install_dependencies
- deploy
```
Content of `.gitlab-ci.yml`:
```yaml
include: 'autodevops-template.yml'
stages:
- production
production:
script:
- install_dependencies
- deploy
- notify_owner
```
If `install_dependencies` and `deploy` are not repeated in
the `.gitlab-ci.yml` file, the `production` job would have only `notify_owner` in the script.
## Use nested includes
You can nest `include` sections in configuration files that are then included
in another configuration. For example, for `include` keywords nested three deep:
Content of `.gitlab-ci.yml`:
```yaml
include:
- local: /.gitlab-ci/another-config.yml
```
Content of `/.gitlab-ci/another-config.yml`:
```yaml
include:
- local: /.gitlab-ci/config-defaults.yml
```
Content of `/.gitlab-ci/config-defaults.yml`:
```yaml
default:
after_script:
- echo "Job complete."
```
### Use nested includes with duplicate `include` entries
You can include the same configuration file multiple times in the main configuration file and
in nested includes.
If any file changes the included configuration using [overrides](#override-included-configuration-values),
then the order of the `include` entries might affect the final configuration. The last time
the configuration is included overrides any previous times the file was included.
For example:
- Contents of a `defaults.gitlab-ci.yml` file:
```yaml
default:
before_script: echo "Default before script"
```
- Contents of a `unit-tests.gitlab-ci.yml` file:
```yaml
include:
- template: defaults.gitlab-ci.yml
default: # Override the included default
before_script: echo "Unit test default override"
unit-test-job:
script: unit-test.sh
```
- Contents of a `smoke-tests.gitlab-ci.yml` file:
```yaml
include:
- template: defaults.gitlab-ci.yml
default: # Override the included default
before_script: echo "Smoke test default override"
smoke-test-job:
script: smoke-test.sh
```
With these three files, the order they are included changes the final configuration.
With:
- `unit-tests` included first, the contents of the `.gitlab-ci.yml` file is:
```yaml
include:
- local: unit-tests.gitlab-ci.yml
- local: smoke-tests.gitlab-ci.yml
```
The final configuration would be:
```yaml
unit-test-job:
before_script: echo "Smoke test default override"
script: unit-test.sh
smoke-test-job:
before_script: echo "Smoke test default override"
script: smoke-test.sh
```
- `unit-tests` included last, the contents of the `.gitlab-ci.yml` file is:
```yaml
include:
- local: smoke-tests.gitlab-ci.yml
- local: unit-tests.gitlab-ci.yml
```
- The final configuration would be:
```yaml
unit-test-job:
before_script: echo "Unit test default override"
script: unit-test.sh
smoke-test-job:
before_script: echo "Unit test default override"
script: smoke-test.sh
```
If no file overrides the included configuration, the order of the `include` entries
does not affect the final configuration
## Use variables with `include`
In `include` sections in your `.gitlab-ci.yml` file, you can use:
- [Project variables](../variables/index.md#for-a-project).
- [Group variables](../variables/index.md#for-a-group).
- [Instance variables](../variables/index.md#for-an-instance).
- Project [predefined variables](../variables/predefined_variables.md) (`CI_PROJECT_*`).
- [Trigger variables](../triggers/index.md#pass-cicd-variables-in-the-api-call).
- [Scheduled pipeline variables](../pipelines/schedules.md#add-a-pipeline-schedule).
- [Manual pipeline run variables](../pipelines/index.md#run-a-pipeline-manually).
- The `CI_PIPELINE_SOURCE` and `CI_PIPELINE_TRIGGERED` [predefined variables](../variables/predefined_variables.md).
- The `$CI_COMMIT_REF_NAME` [predefined variable](../variables/predefined_variables.md).
For example:
```yaml
include:
project: '$CI_PROJECT_PATH'
file: '.compliance-gitlab-ci.yml'
```
You cannot use variables defined in jobs, or in a global [`variables`](../yaml/index.md#variables)
section which defines the default variables for all jobs. Includes are evaluated before jobs,
so these variables cannot be used with `include`.
For an example of how you can include predefined variables, and the variables' impact on CI/CD jobs,
see this [CI/CD variable demo](https://youtu.be/4XR8gw3Pkos).
You cannot use CI/CD variables in an `include` section in a dynamic child pipeline's configuration.
[Issue 378717](https://gitlab.com/gitlab-org/gitlab/-/issues/378717) proposes fixing
this issue.
## Use `rules` with `include`
> - Support for `needs` job dependency [introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/345377) in GitLab 15.11.
You can use [`rules`](index.md#rules) with `include` to conditionally include other configuration files.
You can only use `rules` with [certain variables](#use-variables-with-include), and
these keywords:
- [`rules:if`](index.md#rulesif).
- [`rules:exists`](index.md#rulesexists).
- [`rules:changes`](index.md#ruleschanges).
### `include` with `rules:if`
> - Support for `when: never` and `when:always` [introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/348146) in GitLab 16.1 [with a flag](../../administration/feature_flags.md) named `ci_support_include_rules_when_never`. Disabled by default.
> - Support for `when: never` and `when:always` [generally available](https://gitlab.com/gitlab-org/gitlab/-/issues/414517) in GitLab 16.2. Feature flag `ci_support_include_rules_when_never` removed.
Use [`rules:if`](index.md#rulesif) to conditionally include other configuration files
based on the status of CI/CD variables. For example:
```yaml
include:
- local: builds.yml
rules:
- if: $DONT_INCLUDE_BUILDS == "true"
when: never
- local: builds.yml
rules:
- if: $ALWAYS_INCLUDE_BUILDS == "true"
when: always
- local: builds.yml
rules:
- if: $INCLUDE_BUILDS == "true"
- local: deploys.yml
rules:
- if: $CI_COMMIT_BRANCH == "main"
test:
stage: test
script: exit 0
```
### `include` with `rules:exists`
> - Support for `when: never` and `when:always` [introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/348146) in GitLab 16.1 [with a flag](../../administration/feature_flags.md) named `ci_support_include_rules_when_never`. Disabled by default.
> - Support for `when: never` and `when:always` [generally available](https://gitlab.com/gitlab-org/gitlab/-/issues/414517) in GitLab 16.2. Feature flag `ci_support_include_rules_when_never` removed.
Use [`rules:exists`](index.md#rulesexists) to conditionally include other configuration files
based on the existence of files. For example:
```yaml
include:
- local: builds.yml
rules:
- exists:
- exception-file.md
when: never
- local: builds.yml
rules:
- exists:
- important-file.md
when: always
- local: builds.yml
rules:
- exists:
- file.md
test:
stage: test
script: exit 0
```
In this example, GitLab checks for the existence of `file.md` in the current project.
Review your configuration carefully if you use `include` with `rules:exists` in an include file
from a different project. GitLab checks for the existence of the file in the _other_ project.
For example:
```yaml
# Pipeline configuration in my-group/my-project
include:
- project: my-group/other-project
ref: other_branch
file: other-file.yml
test:
script: exit 0
# other-file.yml in my-group/other-project on ref other_branch
include:
- project: my-group/my-project
ref: main
file: my-file.yml
rules:
- exists:
- file.md
```
In this example, GitLab searches for the existence of `file.md` in `my-group/other-project`
on commit ref `other_branch`, not the project/ref in which the pipeline runs.
To change the search context you can use [`rules:exists:paths`](index.md#rulesexistspaths)
with [`rules:exists:project`](index.md#rulesexistsproject).
For example:
```yaml
include:
- project: my-group/my-project
ref: main
file: my-file.yml
rules:
- exists:
paths:
- file.md
project: my-group/my-project
ref: main
```
### `include` with `rules:changes`
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/342209) in GitLab 16.4.
Use [`rules:changes`](index.md#ruleschanges) to conditionally include other configuration files
based on changed files. For example:
```yaml
include:
- local: builds1.yml
rules:
- changes:
- Dockerfile
- local: builds2.yml
rules:
- changes:
paths:
- Dockerfile
compare_to: 'refs/heads/branch1'
when: always
- local: builds3.yml
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
changes:
paths:
- Dockerfile
test:
stage: test
script: exit 0
```
In this example:
- `builds1.yml` is included when `Dockerfile` has changed.
- `builds2.yml` is included when `Dockerfile` has changed relative to `refs/heads/branch1`.
- `builds3.yml` is included when `Dockerfile` has changed and the pipeline source is a merge request event.
## Use `include:local` with wildcard file paths
You can use wildcard paths (`*` and `**`) with `include:local`.
Example:
```yaml
include: 'configs/*.yml'
```
When the pipeline runs, GitLab:
- Adds all `.yml` files in the `configs` directory into the pipeline configuration.
- Does not add `.yml` files in subfolders of the `configs` directory. To allow this,
add the following configuration:
```yaml
# This matches all `.yml` files in `configs` and any subfolder in it.
include: 'configs/**.yml'
# This matches all `.yml` files only in subfolders of `configs`.
include: 'configs/**/*.yml'
```
## Troubleshooting
### `Maximum of 150 nested includes are allowed!` error
The maximum number of [nested included files](#use-nested-includes) for a pipeline is 150.
If you receive the `Maximum 150 includes are allowed` error message in your pipeline,
it's likely that either:
- Some of the nested configuration includes an overly large number of additional nested `include` configuration.
- There is an accidental loop in the nested includes. For example, `include1.yml` includes
`include2.yml` which includes `include1.yml`, creating a recursive loop.
To help reduce the risk of this happening, edit the pipeline configuration file
with the [pipeline editor](../pipeline_editor/index.md), which validates if the
limit is reached. You can remove one included file at a time to try to narrow down
which configuration file is the source of the loop or excessive included files.
In [GitLab 16.0 and later](https://gitlab.com/gitlab-org/gitlab/-/issues/207270) self-managed users can
change the [maximum includes](../../administration/settings/continuous_integration.md#maximum-includes) value.
### `SSL_connect SYSCALL returned=5 errno=0 state=SSLv3/TLS write client hello` and other network failures
When using [`include:remote`](index.md#includeremote), GitLab tries to fetch the remote file
through HTTP(S). This process can fail because of a variety of connectivity issues.
The `SSL_connect SYSCALL returned=5 errno=0 state=SSLv3/TLS write client hello` error
happens when GitLab can't establish an HTTPS connection to the remote host. This issue
can be caused if the remote host has rate limits to prevent overloading the server
with requests.
For example, the [GitLab Pages](../../user/project/pages/index.md) server for GitLab.com
is rate limited. Repeated attempts to fetch CI/CD configuration files hosted on GitLab Pages
can cause the rate limit to be reached and cause the error. You should avoid hosting
CI/CD configuration files on a GitLab Pages site.
When possible, use [`include:project`](index.md#includeproject) to fetch configuration
files from other projects within the GitLab instance without making external HTTP(S) requests.
|