Commit 916ce7d1 authored by Marcel Amirault's avatar Marcel Amirault Committed by Suzanne Selhorn

CTRT edits to YAML includes doc

parent 6b5e1923
...@@ -7,70 +7,75 @@ type: reference ...@@ -7,70 +7,75 @@ type: reference
# GitLab CI/CD include examples **(FREE)** # GitLab CI/CD include examples **(FREE)**
In addition to the [`includes` examples](index.md#include) listed in the You can use [`include`](index.md#include) to include external YAML files in your CI/CD jobs.
[GitLab CI YAML reference](index.md), this page lists more variations of `include`
usage.
## Single string or array of multiple values ## Include a single configuration file
You can include your extra YAML file(s) either as a single string or To include a single configuration file, use either of these syntax options:
an array of multiple values. The following examples are all valid.
Single string with the `include:local` method implied: - `include` by itself with a single file, which is the same as
[`include:local`](index.md#includelocal):
```yaml ```yaml
include: '/templates/.after-script-template.yml' include: '/templates/.after-script-template.yml'
``` ```
Array with `include` method implied: - `include` with a single file, and you specify the `include` type:
```yaml ```yaml
include: include:
- 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml' remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
- '/templates/.after-script-template.yml' ```
```
Single string with `include` method specified explicitly: ## Include an array of configuration files
```yaml You can include an array of configuration files:
include:
remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
```
Array with `include:remote` being the single item: - If you do not specify an `include` type, the type defaults to [`include:local`](index.md#includelocal):
```yaml ```yaml
include: 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' - remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
``` ```
Array with multiple `include` methods specified explicitly: - You can define an array and explicitly specify multiple `include` types:
```yaml ```yaml
include: include:
- remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml' - remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
- local: '/templates/.after-script-template.yml' - local: '/templates/.after-script-template.yml'
- template: Auto-DevOps.gitlab-ci.yml - template: Auto-DevOps.gitlab-ci.yml
``` ```
Array mixed syntax: - You can define an array that combines both default and specific `include` type:
```yaml ```yaml
include: include:
- 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml' - 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
- '/templates/.after-script-template.yml' - '/templates/.after-script-template.yml'
- template: Auto-DevOps.gitlab-ci.yml - template: Auto-DevOps.gitlab-ci.yml
- project: 'my-group/my-project' - project: 'my-group/my-project'
ref: main ref: main
file: '/templates/.gitlab-ci-template.yml' file: '/templates/.gitlab-ci-template.yml'
``` ```
## Re-using a `before_script` template ## Use `default` configuration from an included configuration file
In the following example, the content of `.before-script-template.yml` is You can define a [`default`](index.md#custom-default-keyword-values) section in a
automatically fetched and evaluated along with the content of `.gitlab-ci.yml`. configuration file. When you use a `default` section with the `include` keyword, the defaults apply to
all jobs in the pipeline.
Content of `https://gitlab.com/awesome-project/raw/main/.before-script-template.yml`: 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 ```yaml
default: default:
...@@ -83,19 +88,29 @@ default: ...@@ -83,19 +88,29 @@ default:
Content of `.gitlab-ci.yml`: Content of `.gitlab-ci.yml`:
```yaml ```yaml
include: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml' include: '/templates/.before-script-template.yml'
rspec: rspec1:
script:
- bundle exec rspec
rspec2:
script: script:
- bundle exec rspec - bundle exec rspec
``` ```
## Overriding external template values 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 specific YAML-defined variables and details of the The following example shows an `include` file that is customized in the
`production` job from an include file being customized in `.gitlab-ci.yml`. `.gitlab-ci.yml` file. Specific YAML-defined variables and details of the
`production` job are overridden.
Content of `https://company.com/autodevops-template.yml`: Content of a custom configuration file named `autodevops-template.yml`:
```yaml ```yaml
variables: variables:
...@@ -136,17 +151,18 @@ production: ...@@ -136,17 +151,18 @@ production:
url: https://domain.com url: https://domain.com
``` ```
In this case, the variables `POSTGRES_USER` and `POSTGRES_PASSWORD` along The `POSTGRES_USER` and `POSTGRES_PASSWORD` variables
with the environment URL of the `production` job defined in and the `environment:url` of the `production` job defined in the `.gitlab-ci.yml` file
`autodevops-template.yml` have been overridden by new values defined in override the values defined in the `autodevops-template.yml` file. The other keywords
`.gitlab-ci.yml`. do not change. This method is called *merging*.
The merging lets you extend and override dictionary mappings, but ## Override included configuration arrays
you cannot add or modify items to an included array. For example, to add
an additional item to the production job script, you must repeat the
existing script items:
Content of `https://company.com/autodevops-template.yml`: 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 ```yaml
production: production:
...@@ -159,7 +175,7 @@ production: ...@@ -159,7 +175,7 @@ production:
Content of `.gitlab-ci.yml`: Content of `.gitlab-ci.yml`:
```yaml ```yaml
include: 'https://company.com/autodevops-template.yml' include: 'autodevops-template.yml'
stages: stages:
- production - production
...@@ -171,51 +187,32 @@ production: ...@@ -171,51 +187,32 @@ production:
- notify_owner - notify_owner
``` ```
In this case, if `install_dependencies` and `deploy` were not repeated in If `install_dependencies` and `deploy` are not repeated in
`.gitlab-ci.yml`, they would not be part of the script for the `production` the `.gitlab-ci.yml` file, the `production` job would have only `notify_owner` in the script.
job in the combined CI configuration.
## Using nested includes ## Use nested includes
The examples below show how includes can be nested from different sources You can nest `include` sections in configuration files that are then included
using a combination of different methods. in another configuration. For example, for `include` keywords nested three deep:
In this example, `.gitlab-ci.yml` includes local the file `/.gitlab-ci/another-config.yml`: Content of `.gitlab-ci.yml`:
```yaml ```yaml
include: include:
- local: /.gitlab-ci/another-config.yml - local: /.gitlab-ci/another-config.yml
``` ```
The `/.gitlab-ci/another-config.yml` includes a template and the `/templates/docker-workflow.yml` file Content of `/.gitlab-ci/another-config.yml`:
from another project:
```yaml
include:
- template: Bash.gitlab-ci.yml
- project: group/my-project
file: /templates/docker-workflow.yml
```
The `/templates/docker-workflow.yml` present in `group/my-project` includes two local files
of the `group/my-project`:
```yaml ```yaml
include: include:
- local: /templates/docker-build.yml - local: /.gitlab-ci/config-defaults.yml
- local: /templates/docker-testing.yml
``` ```
Our `/templates/docker-build.yml` present in `group/my-project` adds a `docker-build` job: Content of `/.gitlab-ci/config-defaults.yml`:
```yaml ```yaml
docker-build: default:
script: docker build -t my-image . after_script:
``` - echo "Job complete."
Our second `/templates/docker-test.yml` present in `group/my-project` adds a `docker-test` job:
```yaml
docker-test:
script: docker run my-image /run/tests.sh
``` ```
...@@ -45,7 +45,7 @@ This template requires: ...@@ -45,7 +45,7 @@ This template requires:
- Use [Pipelines for merge requests](../../../ci/pipelines/merge_request_pipelines.md#configure-pipelines-for-merge-requests) - Use [Pipelines for merge requests](../../../ci/pipelines/merge_request_pipelines.md#configure-pipelines-for-merge-requests)
- [Pipelines for Merged Results](../../../ci/pipelines/pipelines_for_merged_results.md#enable-pipelines-for-merged-results) - [Pipelines for Merged Results](../../../ci/pipelines/pipelines_for_merged_results.md#enable-pipelines-for-merged-results)
enabled in the project settings. enabled in the project settings.
- A Docker image with Ruby available. The template uses `image: ruby:2.6` by default, but you [can override](../../../ci/yaml/includes.md#overriding-external-template-values) this. - A Docker image with Ruby available. The template uses `image: ruby:2.6` by default, but you [can override](../../../ci/yaml/includes.md#override-included-configuration-values) this.
## Configuring Fast RSpec Failure ## Configuring Fast RSpec Failure
......
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