2020-05-29 14:08:26 -04:00
---
stage: Verify
2021-08-31 14:10:24 -04:00
group: Pipeline Authoring
2020-11-26 01:09:20 -05:00
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-05-29 14:08:26 -04:00
type: reference
---
2021-06-21 20:07:53 -04:00
# GitLab CI/CD include examples **(FREE)**
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
You can use [`include` ](index.md#include ) to include external YAML files in your CI/CD jobs.
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
## Include a single configuration file
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
To include a single configuration file, use either of these syntax options:
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
- `include` by itself with a single file, which is the same as
[`include:local` ](index.md#includelocal ):
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
```yaml
include: '/templates/.after-script-template.yml'
```
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
- `include` with a single file, and you specify the `include` type:
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
```yaml
include:
remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
```
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
## Include an array of configuration files
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
You can include an array of configuration files:
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
- If you do not specify an `include` type, the type defaults to [`include:local` ](index.md#includelocal ):
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
```yaml
include:
- 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
- '/templates/.after-script-template.yml'
```
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
- You can define a single item array:
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
```yaml
include:
- remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
```
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
- You can define an array and explicitly specify multiple `include` types:
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
```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-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
## Use `default` configuration from an included configuration file
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
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-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
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-04-27 23:09:53 -04:00
```yaml
2020-10-19 11:08:58 -04:00
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-04-27 23:09:53 -04:00
```
Content of `.gitlab-ci.yml` :
```yaml
2021-09-09 23:10:59 -04:00
include: '/templates/.before-script-template.yml'
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
rspec1:
script:
- bundle exec rspec
rspec2:
2020-04-27 23:09:53 -04:00
script:
- bundle exec rspec
```
2021-09-09 23:10:59 -04:00
The default `before_script` commands execute in both `rspec` jobs, before the `script` commands.
## Override included configuration values
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
When you use the `include` keyword, you can override the included configuration values to adapt them
to your pipeline requirements.
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
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-04-27 23:09:53 -04:00
```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-06-15 11:10:04 -04:00
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
2020-04-27 23:09:53 -04:00
```
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-09-09 23:10:59 -04:00
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-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
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-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
Content of `autodevops-template.yml` :
2020-04-27 23:09:53 -04:00
```yaml
production:
stage: production
script:
- install_dependencies
- deploy
```
Content of `.gitlab-ci.yml` :
```yaml
2021-09-09 23:10:59 -04:00
include: 'autodevops-template.yml'
2020-04-27 23:09:53 -04:00
stages:
- production
production:
script:
- install_dependencies
- deploy
- notify_owner
```
2021-09-09 23:10:59 -04:00
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-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
## Use nested includes
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
You can nest `include` sections in configuration files that are then included
in another configuration. For example, for `include` keywords nested three deep:
2020-04-27 23:09:53 -04:00
2021-09-09 23:10:59 -04:00
Content of `.gitlab-ci.yml` :
2020-04-27 23:09:53 -04:00
```yaml
include:
- local: /.gitlab-ci/another-config.yml
```
2021-09-09 23:10:59 -04:00
Content of `/.gitlab-ci/another-config.yml` :
2020-04-27 23:09:53 -04:00
```yaml
include:
2021-09-09 23:10:59 -04:00
- local: /.gitlab-ci/config-defaults.yml
2020-04-27 23:09:53 -04:00
```
2021-09-09 23:10:59 -04:00
Content of `/.gitlab-ci/config-defaults.yml` :
2020-04-27 23:09:53 -04:00
```yaml
2021-09-09 23:10:59 -04:00
default:
after_script:
- echo "Job complete."
2020-04-27 23:09:53 -04:00
```