debian-mirror-gitlab/doc/ci/yaml/includes.md

219 lines
5.5 KiB
Markdown
Raw Normal View History

2020-06-23 00:09:42 +05:30
---
stage: Verify
2021-11-11 11:23:49 +05:30
group: Pipeline Authoring
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-06-23 00:09:42 +05:30
type: reference
---
2021-09-30 23:02:18 +05:30
# GitLab CI/CD include examples **(FREE)**
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
You can use [`include`](index.md#include) to include external YAML files in your CI/CD jobs.
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
## Include a single configuration file
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
To include a single configuration file, use either of these syntax options:
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
- `include` by itself with a single file, which is the same as
[`include:local`](index.md#includelocal):
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
```yaml
include: '/templates/.after-script-template.yml'
```
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
- `include` with a single file, and you specify the `include` type:
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
```yaml
include:
remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
```
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
## Include an array of configuration files
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
You can include an array of configuration files:
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
- If you do not specify an `include` type, the type defaults to [`include:local`](index.md#includelocal):
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
```yaml
include:
- 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
- '/templates/.after-script-template.yml'
```
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
- You can define a single item array:
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
```yaml
include:
- remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
```
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
- You can define an array and explicitly specify multiple `include` types:
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
```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` type:
```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'
```
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
## Use `default` configuration from an included configuration file
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
You can define a [`default`](index.md#custom-default-keyword-values) section in a
configuration file. When you use a `default` section with the `include` keyword, the defaults apply to
all jobs in the pipeline.
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
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`:
2020-05-24 23:13:21 +05:30
```yaml
2021-01-03 14:25:43 +05:30
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[@]}"
2020-05-24 23:13:21 +05:30
```
Content of `.gitlab-ci.yml`:
```yaml
2021-11-11 11:23:49 +05:30
include: '/templates/.before-script-template.yml'
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
rspec1:
script:
- bundle exec rspec
rspec2:
2020-05-24 23:13:21 +05:30
script:
- bundle exec rspec
```
2021-11-11 11:23:49 +05:30
The default `before_script` commands execute in both `rspec` jobs, before the `script` commands.
## Override included configuration values
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
When you use the `include` keyword, you can override the included configuration values to adapt them
to your pipeline requirements.
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
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`:
2020-05-24 23:13:21 +05:30
```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
2021-09-04 01:27:46 +05:30
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
2020-05-24 23:13:21 +05:30
```
Content of `.gitlab-ci.yml`:
```yaml
include: 'https://company.com/autodevops-template.yml'
image: alpine:latest
variables:
POSTGRES_USER: root
POSTGRES_PASSWORD: secure_password
stages:
- build
- test
- production
production:
environment:
url: https://domain.com
```
2021-11-11 11:23:49 +05:30
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*.
## Override included configuration arrays
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
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:
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
Content of `autodevops-template.yml`:
2020-05-24 23:13:21 +05:30
```yaml
production:
stage: production
script:
- install_dependencies
- deploy
```
Content of `.gitlab-ci.yml`:
```yaml
2021-11-11 11:23:49 +05:30
include: 'autodevops-template.yml'
2020-05-24 23:13:21 +05:30
stages:
- production
production:
script:
- install_dependencies
- deploy
- notify_owner
```
2021-11-11 11:23:49 +05:30
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.
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
## Use nested includes
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
You can nest `include` sections in configuration files that are then included
in another configuration. For example, for `include` keywords nested three deep:
2020-05-24 23:13:21 +05:30
2021-11-11 11:23:49 +05:30
Content of `.gitlab-ci.yml`:
2020-05-24 23:13:21 +05:30
```yaml
include:
- local: /.gitlab-ci/another-config.yml
```
2021-11-11 11:23:49 +05:30
Content of `/.gitlab-ci/another-config.yml`:
2020-05-24 23:13:21 +05:30
```yaml
include:
2021-11-11 11:23:49 +05:30
- local: /.gitlab-ci/config-defaults.yml
2020-05-24 23:13:21 +05:30
```
2021-11-11 11:23:49 +05:30
Content of `/.gitlab-ci/config-defaults.yml`:
2020-05-24 23:13:21 +05:30
```yaml
2021-11-11 11:23:49 +05:30
default:
after_script:
- echo "Job complete."
2020-05-24 23:13:21 +05:30
```