Commit 66cd7d8f authored by Evan Read's avatar Evan Read

Merge branch 'patch-128' into 'master'

GitLab CI: Improve include, extends, & YAML anchors details

See merge request gitlab-org/gitlab!32943
parents c39072a6 3f9b79ce
...@@ -383,7 +383,7 @@ NOTE: **Note:** ...@@ -383,7 +383,7 @@ NOTE: **Note:**
The configuration is a snapshot in time and persisted in the database. Any changes to The configuration is a snapshot in time and persisted in the database. Any changes to
referenced `.gitlab-ci.yml` configuration won't be reflected in GitLab until the next pipeline is created. referenced `.gitlab-ci.yml` configuration won't be reflected in GitLab until the next pipeline is created.
The files defined in `include` are: The files defined by `include` are:
- Deep merged with those in `.gitlab-ci.yml`. - Deep merged with those in `.gitlab-ci.yml`.
- Always evaluated first and merged with the content of `.gitlab-ci.yml`, - Always evaluated first and merged with the content of `.gitlab-ci.yml`,
...@@ -391,11 +391,11 @@ The files defined in `include` are: ...@@ -391,11 +391,11 @@ The files defined in `include` are:
TIP: **Tip:** TIP: **Tip:**
Use merging to customize and override included CI/CD configurations with local Use merging to customize and override included CI/CD configurations with local
definitions. definitions. Local definitions in `.gitlab-ci.yml` will override included definitions.
NOTE: **Note:** NOTE: **Note:**
Using YAML aliases across different YAML files sourced by `include` is not Using [YAML anchors](#anchors) across different YAML files sourced by `include` is not
supported. You must only refer to aliases in the same file. Instead supported. You must only refer to anchors in the same file. Instead
of using YAML anchors, you can use the [`extends` keyword](#extends). of using YAML anchors, you can use the [`extends` keyword](#extends).
#### `include:local` #### `include:local`
...@@ -972,24 +972,38 @@ spinach: ...@@ -972,24 +972,38 @@ spinach:
``` ```
In GitLab 12.0 and later, it's also possible to use multiple parents for In GitLab 12.0 and later, it's also possible to use multiple parents for
`extends`. The algorithm used for merge is "closest scope wins", so `extends`.
keys from the last member will always shadow anything defined on other
#### Merge details
`extends` is able to merge hashes but not arrays.
The algorithm used for merge is "closest scope wins", so
keys from the last member will always override anything defined on other
levels. For example: levels. For example:
```yaml ```yaml
.only-important: .only-important:
variables:
URL: "http://my-url.internal"
IMPORTANT_VAR: "the details"
only: only:
- master - master
- stable - stable
tags: tags:
- production - production
script:
- echo "Hello world!"
.in-docker: .in-docker:
variables:
URL: "http://docker-url.internal"
tags: tags:
- docker - docker
image: alpine image: alpine
rspec: rspec:
variables:
GITLAB: "is-awesome"
extends: extends:
- .only-important - .only-important
- .in-docker - .in-docker
...@@ -1001,6 +1015,10 @@ This results in the following `rspec` job: ...@@ -1001,6 +1015,10 @@ This results in the following `rspec` job:
```yaml ```yaml
rspec: rspec:
variables:
URL: "http://docker-url.internal"
IMPORTANT_VAR: "the details"
GITLAB: "is-awesome"
only: only:
- master - master
- stable - stable
...@@ -1011,6 +1029,15 @@ rspec: ...@@ -1011,6 +1029,15 @@ rspec:
- rake rspec - rake rspec
``` ```
Note that in the example above:
- `variables` sections have been merged but that `URL: "http://my-url.internal"`
has been overwritten by `URL: "http://docker-url.internal"`.
- `tags: ['production']` has been overwritten by `tags: ['docker']`.
- `script` has not been merged but rather `script: ['echo "Hello world!"']` has
been overwritten by `script: ['rake rspec']`. Arrays can be
merged using [YAML anchors](#anchors).
#### Using `extends` and `include` together #### Using `extends` and `include` together
`extends` works across configuration files combined with `include`. `extends` works across configuration files combined with `include`.
...@@ -4149,6 +4176,10 @@ of `.gitlab-ci.yml`. ...@@ -4149,6 +4176,10 @@ of `.gitlab-ci.yml`.
Read more about the various [YAML features](https://learnxinyminutes.com/docs/yaml/). Read more about the various [YAML features](https://learnxinyminutes.com/docs/yaml/).
In most cases, the [`extends` keyword](#extends) is more user friendly and should
be used over these special YAML features. YAML anchors may still
need to be used to merge arrays.
### Anchors ### Anchors
> Introduced in GitLab 8.6 and GitLab Runner v1.1.1. > Introduced in GitLab 8.6 and GitLab Runner v1.1.1.
...@@ -4273,7 +4304,8 @@ You can see that the hidden jobs are conveniently used as templates. ...@@ -4273,7 +4304,8 @@ You can see that the hidden jobs are conveniently used as templates.
NOTE: **Note:** NOTE: **Note:**
You can't use YAML anchors across multiple files when leveraging the [`include`](#include) You can't use YAML anchors across multiple files when leveraging the [`include`](#include)
feature. Anchors are only valid within the file they were defined in. feature. Anchors are only valid within the file they were defined in. Instead
of using YAML anchors, you can use the [`extends` keyword](#extends).
#### YAML anchors for `before_script` and `after_script` #### YAML anchors for `before_script` and `after_script`
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment