summaryrefslogtreecommitdiff
path: root/doc/ci/yaml/includes.md
blob: 673a4e75c358aa49f263bf61effa614e1871296b (plain)
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
---
stage: Verify
group: Pipeline Execution
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
type: reference
---

# GitLab CI/CD include examples **(FREE)**

In addition to the [`includes` examples](index.md#include) listed in the
[GitLab CI YAML reference](index.md), this page lists more variations of `include`
usage.

## Single string or array of multiple values

You can include your extra YAML file(s) either as a single string or
an array of multiple values. The following examples are all valid.

Single string with the `include:local` method implied:

```yaml
include: '/templates/.after-script-template.yml'
```

Array with `include` method implied:

```yaml
include:
  - 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
  - '/templates/.after-script-template.yml'
```

Single string with `include` method specified explicitly:

```yaml
include:
  remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
```

Array with `include:remote` being the single item:

```yaml
include:
  - remote: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'
```

Array with multiple `include` methods specified explicitly:

```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
```

Array mixed syntax:

```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'
```

## Re-using a `before_script` template

In the following example, the content of `.before-script-template.yml` is
automatically fetched and evaluated along with the content of `.gitlab-ci.yml`.

Content of `https://gitlab.com/awesome-project/raw/main/.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: 'https://gitlab.com/awesome-project/raw/main/.before-script-template.yml'

rspec:
  script:
    - bundle exec rspec
```

## Overriding external template values

The following example shows specific YAML-defined variables and details of the
`production` job from an include file being customized in `.gitlab-ci.yml`.

Content of `https://company.com/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'

image: alpine:latest

variables:
  POSTGRES_USER: root
  POSTGRES_PASSWORD: secure_password

stages:
  - build
  - test
  - production

production:
  environment:
    url: https://domain.com
```

In this case, the variables `POSTGRES_USER` and `POSTGRES_PASSWORD` along
with the environment URL of the `production` job defined in
`autodevops-template.yml` have been overridden by new values defined in
`.gitlab-ci.yml`.

The merging lets you extend and override dictionary mappings, but
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`:

```yaml
production:
  stage: production
  script:
    - install_dependencies
    - deploy
```

Content of `.gitlab-ci.yml`:

```yaml
include: 'https://company.com/autodevops-template.yml'

stages:
  - production

production:
  script:
    - install_dependencies
    - deploy
    - notify_owner
```

In this case, if `install_dependencies` and `deploy` were not repeated in
`.gitlab-ci.yml`, they would not be part of the script for the `production`
job in the combined CI configuration.

## Using nested includes

The examples below show how includes can be nested from different sources
using a combination of different methods.

In this example, `.gitlab-ci.yml` includes local the file `/.gitlab-ci/another-config.yml`:

```yaml
include:
  - local: /.gitlab-ci/another-config.yml
```

The `/.gitlab-ci/another-config.yml` includes a template and the `/templates/docker-workflow.yml` file
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
include:
  - local: /templates/docker-build.yml
  - local: /templates/docker-testing.yml
```

Our `/templates/docker-build.yml` present in `group/my-project` adds a `docker-build` job:

```yaml
docker-build:
  script: docker build -t my-image .
```

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
```