README.md 63,7 КБ
Newer Older
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
1
# Configuration of your jobs with .gitlab-ci.yml
Douwe Maan's avatar
Douwe Maan включено в состав коммита
2

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
3
This document describes the usage of `.gitlab-ci.yml`, the file that is used by
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
4
GitLab Runner to manage your project's jobs.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
5

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
6
7
8
9
From version 7.12, GitLab CI uses a [YAML](https://en.wikipedia.org/wiki/YAML)
file (`.gitlab-ci.yml`) for the project configuration. It is placed in the root
of your repository and contains definitions of how your project should be built.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
10
11
12
If you want a quick introduction to GitLab CI, follow our
[quick start guide](../quick_start/README.md).

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
13
14
15
16
17
NOTE: **Note:**
If you have a [mirrored repository where GitLab pulls from](https://docs.gitlab.com/ee/workflow/repository_mirroring.html#pulling-from-a-remote-repository),
you may need to enable pipeline triggering in your project's
**Settings > Repository > Pull from a remote repository > Trigger pipelines for mirror updates**.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
18
19
## Jobs

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
20
The YAML file defines a set of jobs with constraints stating when they should
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
21
22
23
be run. You can specify an unlimited number of jobs which are defined as
top-level elements with an arbitrary name and always have to contain at least
the `script` clause.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
24
25
26
27
28
29
30
31
32

```yaml
job1:
  script: "execute-script-for-job1"

job2:
  script: "execute-script-for-job2"
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
33
The above example is the simplest possible CI/CD configuration with two separate
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
34
35
36
jobs, where each of the jobs executes a different command.
Of course a command can execute code directly (`./configure;make;make install`)
or run a script (`test.sh`) in the repository.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
37

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
38
39
40
Jobs are picked up by [Runners](../runners/README.md) and executed within the
environment of the Runner. What is important, is that each job is run
independently from each other.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
41

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
42
43
Each job must have a unique name, but there are a few **reserved `keywords` that
cannot be used as job names**:
Douwe Maan's avatar
Douwe Maan включено в состав коммита
44

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
45
46
47
48
49
50
51
52
- `image`
- `services`
- `stages`
- `types`
- `before_script`
- `after_script`
- `variables`
- `cache`
Douwe Maan's avatar
Douwe Maan включено в состав коммита
53

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
54
A job is defined by a list of parameters that define the job behavior.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
55

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
56
57
58
| Keyword       | Required | Description |
|---------------|----------|-------------|
| script        | yes      | Defines a shell script which is executed by Runner |
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
59
| extends       | no       | Defines a configuration entry that this job is going to inherit from |
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
| image         | no       | Use docker image, covered in [Using Docker Images](../docker/using_docker_images.md#define-image-and-services-from-gitlab-ciyml) |
| services      | no       | Use docker services, covered in [Using Docker Images](../docker/using_docker_images.md#define-image-and-services-from-gitlab-ciyml) |
| stage         | no       | Defines a job stage (default: `test`) |
| type          | no       | Alias for `stage` |
| variables     | no       | Define job variables on a job level |
| only          | no       | Defines a list of git refs for which job is created |
| except        | no       | Defines a list of git refs for which job is not created |
| tags          | no       | Defines a list of tags which are used to select Runner |
| allow_failure | no       | Allow job to fail. Failed job doesn't contribute to commit status |
| when          | no       | Define when to run job. Can be `on_success`, `on_failure`, `always` or `manual` |
| dependencies  | no       | Define other jobs that a job depends on so that you can pass artifacts between them|
| artifacts     | no       | Define list of [job artifacts](#artifacts) |
| cache         | no       | Define list of files that should be cached between subsequent runs |
| before_script | no       | Override a set of commands that are executed before job |
| after_script  | no       | Override a set of commands that are executed after job |
| environment   | no       | Defines a name of environment to which deployment is done by this job |
| coverage      | no       | Define code coverage settings for a given job |
Markus Doits's avatar
Markus Doits включено в состав коммита
77
| retry         | no       | Define when and how many times a job can be auto-retried in case of a failure |
Matija Čupić's avatar
Matija Čupić включено в состав коммита
78
| parallel      | no       | Defines how many instances of a job should be run in parallel |
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
79

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
80
81
### `extends`

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
82
> Introduced in GitLab 11.3.
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
83

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
84
`extends` defines an entry name that a job that uses `extends` is going to
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
85
86
inherit from.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
87
88
It is an alternative to using [YAML anchors](#anchors) and is a little
more flexible and readable:
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
89
90
91

```yaml
.tests:
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
92
93
  script: rake test
  stage: test
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
94
95
96
97
98
99
100
101
102
103
104
105
  only:
    refs:
      - branches

rspec:
  extends: .tests
  script: rake rspec
  only:
    variables:
      - $RSPEC
```

Olivier Gonzalez's avatar
Olivier Gonzalez включено в состав коммита
106
In the example above, the `rspec` job inherits from the `.tests` template job.
Ronald van Zon's avatar
Ronald van Zon включено в состав коммита
107
108
109
110
111
112
GitLab will perform a reverse deep merge based on the keys. GitLab will:

- Merge the `rspec` contents into `.tests` recursively.
- Not merge the values of the keys.

This results in the following `rspec` job:
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
113
114
115
116
117
118
119
120
121
122
123
124

```yaml
rspec:
  script: rake rspec
  stage: test
  only:
    refs:
      - branches
    variables:
      - $RSPEC
```

Ronald van Zon's avatar
Ronald van Zon включено в состав коммита
125
126
127
128
129
NOTE: **Note:**
Note that `script: rake test` has been overwritten by `script: rake rspec`.

If you do want to include the `rake test`, have a look at [before_script-and-after_script](#before_script-and-after_script).

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
130
`.tests` in this example is a [hidden key](#hidden-keys-jobs), but it's
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
131
132
133
possible to inherit from regular jobs as well.

`extends` supports multi-level inheritance, however it is not recommended to
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
134
135
use more than three levels. The maximum nesting level that is supported is 10.
The following example has two levels of inheritance:
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
136
137
138
139

```yaml
.tests:
  only:
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
140
    - pushes
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
141
142
143
144
145
146

.rspec:
  extends: .tests
  script: rake rspec

rspec 1:
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
147
148
  variables:
    RSPEC_SUITE: '1'
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
149
150
151
  extends: .rspec

rspec 2:
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
152
153
  variables:
    RSPEC_SUITE: '2'
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
154
155
156
157
158
159
160
  extends: .rspec

spinach:
  extends: .tests
  script: rake spinach
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
161
162
`extends` works across configuration files combined with [`include`](#include).

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
163
### `pages`
Douwe Maan's avatar
Douwe Maan включено в состав коммита
164

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
165
166
167
168
169
170
171
172
173
174
175
`pages` is a special job that is used to upload static content to GitLab that
can be used to serve your website. It has a special syntax, so the two
requirements below must be met:

1. Any static content must be placed under a `public/` directory
1. `artifacts` with a path to the `public/` directory must be defined

The example below simply moves all files from the root of the project to the
`public/` directory. The `.public` workaround is so `cp` doesn't also copy
`public/` to itself in an infinite loop:

pityonline's avatar
pityonline включено в состав коммита
176
```yaml
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
177
178
pages:
  stage: deploy
Douwe Maan's avatar
Douwe Maan включено в состав коммита
179
  script:
pityonline's avatar
pityonline включено в состав коммита
180
181
182
    - mkdir .public
    - cp -r * .public
    - mv .public public
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
183
184
  artifacts:
    paths:
pityonline's avatar
pityonline включено в состав коммита
185
      - public
Douwe Maan's avatar
Douwe Maan включено в состав коммита
186
  only:
pityonline's avatar
pityonline включено в состав коммита
187
    - master
Douwe Maan's avatar
Douwe Maan включено в состав коммита
188
189
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
190
Read more on [GitLab Pages user documentation](../../user/project/pages/index.md).
Douwe Maan's avatar
Douwe Maan включено в состав коммита
191

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
192
## `image` and `services`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
193
194

This allows to specify a custom Docker image and a list of services that can be
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
195
used for time of the job. The configuration of this feature is covered in
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
196
[a separate document](../docker/README.md).
Douwe Maan's avatar
Douwe Maan включено в состав коммита
197

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
198
## `before_script` and `after_script`
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
199

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
200
> Introduced in GitLab 8.7 and requires GitLab Runner v1.2
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
201

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
202
203
204
205
`before_script` is used to define the command that should be run before all
jobs, including deploy jobs, but after the restoration of [artifacts](#artifacts).
This can be an array or a multi-line string.

Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
206
`after_script` is used to define the command that will be run after for all
Damian Nowak's avatar
Damian Nowak включено в состав коммита
207
jobs, including failed ones. This has to be an array or a multi-line string.
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
208

Martin Nowak's avatar
Martin Nowak включено в состав коммита
209
210
211
212
213
The `before_script` and the main `script` are concatenated and run in a single context/container.
The `after_script` is run separately, so depending on the executor, changes done
outside of the working tree might not be visible, e.g. software installed in the
`before_script`.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
214
215
It's possible to overwrite the globally defined `before_script` and `after_script`
if you set it per-job:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
216

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
217
218
```yaml
before_script:
pityonline's avatar
pityonline включено в состав коммита
219
  - global before script
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
220
221
222

job:
  before_script:
pityonline's avatar
pityonline включено в состав коммита
223
    - execute this instead of global before script
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
224
  script:
pityonline's avatar
pityonline включено в состав коммита
225
    - my command
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
226
  after_script:
pityonline's avatar
pityonline включено в состав коммита
227
    - execute this after my script
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
228
229
230
```

## `stages`
Douwe Maan's avatar
Douwe Maan включено в состав коммита
231

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
232
233
234
235
`stages` is used to define stages that can be used by jobs and is defined
globally.

The specification of `stages` allows for having flexible multi stage pipelines.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
236
The ordering of elements in `stages` defines the ordering of jobs' execution:
Douwe Maan's avatar
Douwe Maan включено в состав коммита
237

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
238
239
1. Jobs of the same stage are run in parallel.
1. Jobs of the next stage are run after the jobs from the previous stage
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
240
   complete successfully.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
241
242

Let's consider the following example, which defines 3 stages:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
243
244

```yaml
Douwe Maan's avatar
Douwe Maan включено в состав коммита
245
246
247
248
249
250
stages:
  - build
  - test
  - deploy
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
251
1. First, all jobs of `build` are executed in parallel.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
252
253
1. If all jobs of `build` succeed, the `test` jobs are executed in parallel.
1. If all jobs of `test` succeed, the `deploy` jobs are executed in parallel.
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
254
1. If all jobs of `deploy` succeed, the commit is marked as `passed`.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
255
256
1. If any of the previous jobs fails, the commit is marked as `failed` and no
   jobs of further stage are executed.
Nick Thomas's avatar
Nick Thomas включено в состав коммита
257

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
258
There are also two edge cases worth mentioning:
Nick Thomas's avatar
Nick Thomas включено в состав коммита
259

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
260
261
262
1. If no `stages` are defined in `.gitlab-ci.yml`, then the `build`,
   `test` and `deploy` are allowed to be used as job's stage by default.
2. If a job doesn't specify a `stage`, the job is assigned the `test` stage.
Nick Thomas's avatar
Nick Thomas включено в состав коммита
263

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
264
## `stage`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
265

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
266
267
268
`stage` is defined per-job and relies on [`stages`](#stages) which is defined
globally. It allows to group jobs into different stages, and jobs of the same
`stage` are executed in `parallel`. For example:
Douwe Maan's avatar
Douwe Maan включено в состав коммита
269
270

```yaml
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
271
272
273
274
275
276
277
278
279
280
281
282
283
284
stages:
  - build
  - test
  - deploy

job 1:
  stage: build
  script: make build dependencies

job 2:
  stage: build
  script: make build artifacts

job 3:
Douwe Maan's avatar
Douwe Maan включено в состав коммита
285
  stage: test
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
286
287
288
289
290
  script: make test

job 4:
  stage: deploy
  script: make deploy
Douwe Maan's avatar
Douwe Maan включено в состав коммита
291
292
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
293
## `types`
Douwe Maan's avatar
Douwe Maan включено в состав коммита
294

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
295
296
297
CAUTION: **Deprecated:**
`types` is deprecated, and could be removed in one of the future releases.
Use [stages](#stages) instead.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
298

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
299
300
301
302
## `script`

`script` is the only required keyword that a job needs. It's a shell script
which is executed by the Runner. For example:
Douwe Maan's avatar
Douwe Maan включено в состав коммита
303
304
305
306
307
308
309

```yaml
job:
  script: "bundle exec rspec"
```

This parameter can also contain several commands using an array:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
310

Douwe Maan's avatar
Douwe Maan включено в состав коммита
311
312
313
314
315
316
317
```yaml
job:
  script:
    - uname -a
    - bundle exec rspec
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
318
319
320
321
322
Sometimes, `script` commands will need to be wrapped in single or double quotes.
For example, commands that contain a colon (`:`) need to be wrapped in quotes so
that the YAML parser knows to interpret the whole thing as a string rather than
a "key: value" pair. Be careful when using special characters:
`:`, `{`, `}`, `[`, `]`, `,`, `&`, `*`, `#`, `?`, `|`, `-`, `<`, `>`, `=`, `!`, `%`, `@`, `` ` ``.
Mark Pundsack's avatar
Mark Pundsack включено в состав коммита
323

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
324
## `only` and `except` (simplified)
Douwe Maan's avatar
Douwe Maan включено в состав коммита
325

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
326
327
`only` and `except` are two parameters that set a job policy to limit when
jobs are created:
Douwe Maan's avatar
Douwe Maan включено в состав коммита
328

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
329
1. `only` defines the names of branches and tags for which the job will run.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
330
2. `except` defines the names of branches and tags for which the job will
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
331
    **not** run.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
332

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
333
There are a few rules that apply to the usage of job policy:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
334
335
336
337
338
339
340

* `only` and `except` are inclusive. If both `only` and `except` are defined
   in a job specification, the ref is filtered by `only` and `except`.
* `only` and `except` allow the use of regular expressions.
* `only` and `except` allow to specify a repository path to filter jobs for
   forks.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
341
342
343
344
345
346
347
348
349
350
351
352
353
354
In addition, `only` and `except` allow the use of special keywords:

| **Value** |  **Description**  |
| --------- |  ---------------- |
| `branches`  | When a branch is pushed.  |
| `tags`      | When a tag is pushed.  |
| `api`       | When pipeline has been triggered by a second pipelines API (not triggers API).  |
| `external`  | When using CI services other than GitLab. |
| `pipelines` | For multi-project triggers, created using the API with `CI_JOB_TOKEN`. |
| `pushes`    | Pipeline is triggered by a `git push` by the user. |
| `schedules` | For [scheduled pipelines][schedules]. |
| `triggers`  | For pipelines created using a trigger token. |
| `web`       | For pipelines created using **Run pipeline** button in GitLab UI (under your project's **Pipelines**). |

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
355
In the example below, `job` will run only for refs that start with `issue-`,
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
356
whereas all branches will be skipped:
Douwe Maan's avatar
Douwe Maan включено в состав коммита
357
358
359

```yaml
job:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
360
  # use regexp
Douwe Maan's avatar
Douwe Maan включено в состав коммита
361
  only:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
362
363
    - /^issue-.*$/
  # use special keyword
Douwe Maan's avatar
Douwe Maan включено в состав коммита
364
  except:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
365
    - branches
Douwe Maan's avatar
Douwe Maan включено в состав коммита
366
367
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
368
In this example, `job` will run only for refs that are tagged, or if a build is
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
369
explicitly requested via an API trigger or a [Pipeline Schedule][schedules]:
Jason Roehm's avatar
Jason Roehm включено в состав коммита
370
371
372
373
374
375
376

```yaml
job:
  # use special keywords
  only:
    - tags
    - triggers
Filip Krakowski's avatar
Filip Krakowski включено в состав коммита
377
    - schedules
Jason Roehm's avatar
Jason Roehm включено в состав коммита
378
379
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
380
381
The repository path can be used to have jobs executed only for the parent
repository and not forks:
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
382
383
384
385
386
387
388
389

```yaml
job:
  only:
    - branches@gitlab-org/gitlab-ce
  except:
    - master@gitlab-org/gitlab-ce
```
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
390
391
392

The above example will run `job` for all branches on `gitlab-org/gitlab-ce`,
except master.
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
393

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
394
## `only` and `except` (complex)
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
395

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
396
> `refs` and `kubernetes` policies introduced in GitLab 10.0
Brett Walker's avatar
Brett Walker включено в состав коммита
397
>
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
398
> `variables` policy introduced in 10.7
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
399
>
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
400
> `changes` policy [introduced](https://gitlab.com/gitlab-org/gitlab-ce/issues/19232) in 11.4
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
401

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
402
403
404
CAUTION: **Warning:**
This an _alpha_ feature, and it it subject to change at any time without
prior notice!
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
405

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
406
407
Since GitLab 10.0 it is possible to define a more elaborate only/except job
policy configuration.
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
408
409

GitLab now supports both, simple and complex strategies, so it is possible to
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
410
use an array and a hash configuration scheme.
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
411

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
412
413
414
415
Four keys are now available: `refs`, `kubernetes` and `variables` and `changes`.

### `refs` and `kubernetes`

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
416
417
418
Refs strategy equals to simplified only/except configuration, whereas
kubernetes strategy accepts only `active` keyword.

Yuping Zuo's avatar
Yuping Zuo включено в состав коммита
419
### `only:variables`
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
420

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
421
`variables` keyword is used to define variables expressions. In other words
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
422
you can use predefined variables / project / group or
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
423
424
environment-scoped variables to define an expression GitLab is going to
evaluate in order to decide whether a job should be created or not.
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
425
426
427
428
429
430
431
432
433
434
435
436
437
438

See the example below. Job is going to be created only when pipeline has been
scheduled or runs for a `master` branch, and only if kubernetes service is
active in the project.

```yaml
job:
  only:
    refs:
      - master
      - schedules
    kubernetes: active
```

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
439
Examples of using variables expressions:
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
440
441
442

```yaml
deploy:
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
443
  script: cap staging deploy
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
444
445
446
447
448
449
450
451
  only:
    refs:
      - branches
    variables:
      - $RELEASE == "staging"
      - $STAGING
```

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
452
453
454
455
456
457
458
459
460
461
Another use case is exluding jobs depending on a commit message _(added in 11.0)_:

```yaml
end-to-end:
  script: rake test:end-to-end
  except:
    variables:
      - $CI_COMMIT_MESSAGE =~ /skip-end-to-end-tests/
```

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
462
Learn more about variables expressions on [a separate page][variables-expressions].
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
463

Yuping Zuo's avatar
Yuping Zuo включено в состав коммита
464
### `only:changes`
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
465
466

Using `changes` keyword with `only` or `except` makes it possible to define if
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
467
468
469
a job should be created based on files modified by a git push event.

For example:
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
470
471
472
473
474
475
476
477

```yaml
docker build:
  script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
  only:
    changes:
      - Dockerfile
      - docker/scripts/*
Lukas Schneider's avatar
Lukas Schneider включено в состав коммита
478
      - dockerfiles/**/*
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
479
480
```

Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
481
482
483
484
485
486
In the scenario above, if you are pushing multiple commits to GitLab to an
existing branch, GitLab creates and triggers `docker build` job, provided that
one of the commits contains changes to either:

- The `Dockerfile` file.
- Any of the files inside `docker/scripts/` directory.
Lukas Schneider's avatar
Lukas Schneider включено в состав коммита
487
- Any of the files and subfolders inside `dockerfiles` directory.
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
488
489

CAUTION: **Warning:**
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
490
491
492
493
494
495
There are some caveats when using this feature with new branches and tags. See
the section below.

#### Using `changes` with new branches and tags

If you are pushing a **new** branch or a **new** tag to GitLab, the policy
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
496
always evaluates to true and GitLab will create a job. This feature is not
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
497
connected with merge requests yet, and because GitLab is creating pipelines
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
498
before an user can create a merge request we don't know a target branch at
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
499
500
501
502
503
504
this point.

Without a target branch, it is not possible to know what the common ancestor is,
thus we always create a job in that case. This feature works best for stable
branches like `master` because in that case GitLab uses the previous commit
that is present in a branch to compare against the latest SHA that was pushed.
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
505

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
506
## `tags`
Douwe Maan's avatar
Douwe Maan включено в состав коммита
507

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
508
`tags` is used to select specific Runners from the list of all Runners that are
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
509
allowed to run this project.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
510

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
511
During the registration of a Runner, you can specify the Runner's tags, for
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
512
513
example `ruby`, `postgres`, `development`.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
514
`tags` allow you to run jobs with Runners that have the specified tags
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
515
516
517
assigned to them:

```yaml
Douwe Maan's avatar
Douwe Maan включено в состав коммита
518
519
520
521
522
523
job:
  tags:
    - ruby
    - postgres
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
524
The specification above, will make sure that `job` is built by a Runner that
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
525
has both `ruby` AND `postgres` tags defined.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
526

Mart Sõmermaa's avatar
Mart Sõmermaa включено в состав коммита
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
Tags are also a great way to run different jobs on different platforms, for
example, given an OS X Runner with tag `osx` and Windows Runner with tag
`windows`, the following jobs run on respective platforms:

```yaml
windows job:
  stage:
    - build
  tags:
    - windows
  script:
    - echo Hello, %USERNAME%!

osx job:
  stage:
    - build
  tags:
    - osx
  script:
    - echo "Hello, $USER!"
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
549
## `allow_failure`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
550

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
551
552
`allow_failure` is used when you want to allow a job to fail without impacting
the rest of the CI suite. Failed jobs don't contribute to the commit status.
C.J. Jameson's avatar
C.J. Jameson включено в состав коммита
553
The default value is `false`.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
554

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
555
When enabled and the job fails, the pipeline will be successful/green for all
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
556
intents and purposes, but a "CI build passed with warnings" message  will be
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
557
558
displayed on the merge request or commit or job page. This is to be used by
jobs that are allowed to fail, but where failure indicates some other (manual)
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
559
560
561
562
563
564
565
566
567
568
steps should be taken elsewhere.

In the example below, `job1` and `job2` will run in parallel, but if `job1`
fails, it will not stop the next stage from running, since it's marked with
`allow_failure: true`:

```yaml
job1:
  stage: test
  script:
pityonline's avatar
pityonline включено в состав коммита
569
    - execute_script_that_will_fail
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
570
571
572
573
574
  allow_failure: true

job2:
  stage: test
  script:
pityonline's avatar
pityonline включено в состав коммита
575
    - execute_script_that_will_succeed
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
576
577
578
579

job3:
  stage: deploy
  script:
pityonline's avatar
pityonline включено в состав коммита
580
    - deploy_to_staging
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
581
582
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
583
## `when`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
584
585
586

`when` is used to implement jobs that are run in case of failure or despite the
failure.
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
587

Robert Speicher's avatar
Robert Speicher включено в состав коммита
588
589
`when` can be set to one of the following values:

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
590
1. `on_success` - execute job only when all jobs from prior stages
Mark Pundsack's avatar
Mark Pundsack включено в состав коммита
591
    succeed. This is the default.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
592
1. `on_failure` - execute job only when at least one job from prior stages
Mark Pundsack's avatar
Mark Pundsack включено в состав коммита
593
    fails.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
594
595
1. `always` - execute job regardless of the status of jobs from prior stages.
1. `manual` - execute job manually (added in GitLab 8.10). Read about
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
596
    [manual actions](#when-manual) below.
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
597

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
598
599
600
For example:

```yaml
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
601
stages:
pityonline's avatar
pityonline включено в состав коммита
602
603
604
605
606
  - build
  - cleanup_build
  - test
  - deploy
  - cleanup
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
607

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
608
build_job:
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
609
610
  stage: build
  script:
pityonline's avatar
pityonline включено в состав коммита
611
    - make build
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
612

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
613
cleanup_build_job:
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
614
615
  stage: cleanup_build
  script:
pityonline's avatar
pityonline включено в состав коммита
616
    - cleanup build when failed
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
617
618
  when: on_failure

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
619
test_job:
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
620
621
  stage: test
  script:
pityonline's avatar
pityonline включено в состав коммита
622
    - make test
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
623

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
624
deploy_job:
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
625
626
  stage: deploy
  script:
pityonline's avatar
pityonline включено в состав коммита
627
    - make deploy
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
628
  when: manual
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
629

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
630
cleanup_job:
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
631
632
  stage: cleanup
  script:
pityonline's avatar
pityonline включено в состав коммита
633
    - cleanup after jobs
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
634
635
636
637
  when: always
```

The above script will:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
638

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
639
640
641
642
1. Execute `cleanup_build_job` only when `build_job` fails.
2. Always execute `cleanup_job` as the last step in pipeline regardless of
   success or failure.
3. Allow you to manually execute `deploy_job` from GitLab's UI.
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
643

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
644
### `when:manual`
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
645

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
646
> **Notes:**
Brett Walker's avatar
Brett Walker включено в состав коммита
647
648
649
650
>
> - Introduced in GitLab 8.10.
> - Blocking manual actions were introduced in GitLab 9.0.
> - Protected actions were introduced in GitLab 9.2.
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
651

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
652
653
654
655
656
Manual actions are a special type of job that are not executed automatically,
they need to be explicitly started by a user. An example usage of manual actions
would be a deployment to a production environment. Manual actions can be started
from the pipeline, job, environment, and deployment views. Read more at the
[environments documentation][env-manual].
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
657

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
658
659
Manual actions can be either optional or blocking. Blocking manual actions will
block the execution of the pipeline at the stage this action is defined in. It's
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
660
possible to resume execution of the pipeline when someone executes a blocking
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
661
manual action by clicking a _play_ button.
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
662

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
663
When a pipeline is blocked, it will not be merged if Merge When Pipeline Succeeds
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
664
665
666
667
668
is set. Blocked pipelines also do have a special status, called _manual_.
Manual actions are non-blocking by default. If you want to make manual action
blocking, it is necessary to add `allow_failure: false` to the job's definition
in `.gitlab-ci.yml`.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
669
670
671
Optional manual actions have `allow_failure: true` set by default and their
Statuses do not contribute to the overall pipeline status. So, if a manual
action fails, the pipeline will eventually succeed.
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
672

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
673
674
675
676
677
Manual actions are considered to be write actions, so permissions for
[protected branches](../../user/project/protected_branches.md) are used when
user wants to trigger an action. In other words, in order to trigger a manual
action assigned to a branch that the pipeline is running for, user needs to
have ability to merge to this branch.
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
678

Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
679
680
681
682
683
684
685
686
### `when:delayed`

> [Introduced](https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/21767) in GitLab 11.4.

Delayed job are for executing scripts after a certain period.
This is useful if you want to avoid jobs entering `pending` state immediately.

You can set the period with `start_in` key. The value of `start_in` key is an elapsed time in seconds, unless a unit is
Shane A. Stillwell's avatar
Shane A. Stillwell включено в состав коммита
687
provided. `start_in` key must be less than or equal to one hour. Examples of valid values include:
Winnie Hellmann's avatar
Winnie Hellmann включено в состав коммита
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714

- `10 seconds`
- `30 minutes`
- `1 hour`

When there is a delayed job in a stage, the pipeline will not progress until the delayed job has finished.
This means this keyword can also be used for inserting delays between different stages.

The timer of a delayed job starts immediately after the previous stage has completed.
Similar to other types of jobs, a delayed job's timer will not start unless the previous stage passed.

The following example creates a job named `timed rollout 10%` that is executed 30 minutes after the previous stage has completed:

```yaml
timed rollout 10%:
  stage: deploy
  script: echo 'Rolling out 10% ...'
  when: delayed
  start_in: 30 minutes
```

You can stop the active timer of a delayed job by clicking the **Unschedule** button.
This job will never be executed in the future unless you execute the job manually.

You can start a delayed job immediately by clicking the **Play** button.
GitLab runner will pick your job soon and start the job.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
715
## `environment`
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
716

Brett Walker's avatar
Brett Walker включено в состав коммита
717
> **Notes:**
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
718
>
Brett Walker's avatar
Brett Walker включено в состав коммита
719
720
721
> - Introduced in GitLab 8.9.
> - You can read more about environments and find more examples in the
>   [documentation about environments][environment].
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
722

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
723
`environment` is used to define that a job deploys to a specific environment.
Mark Pundsack's avatar
Mark Pundsack включено в состав коммита
724
725
If `environment` is specified and no environment under that name exists, a new
one will be created automatically.
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
726

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
727
In its simplest form, the `environment` keyword can be defined like:
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
728

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
729
```yaml
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
730
731
732
deploy to production:
  stage: deploy
  script: git push production HEAD:master
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
733
734
  environment:
    name: production
Kamil Trzcinski's avatar
Kamil Trzcinski включено в состав коммита
735
736
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
737
738
739
In the above example, the `deploy to production` job will be marked as doing a
deployment to the `production` environment.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
740
### `environment:name`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
741

Brett Walker's avatar
Brett Walker включено в состав коммита
742
> **Notes:**
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
743
>
Brett Walker's avatar
Brett Walker включено в состав коммита
744
745
746
747
748
749
750
> - Introduced in GitLab 8.11.
> - Before GitLab 8.11, the name of an environment could be defined as a string like
>   `environment: production`. The recommended way now is to define it under the
>   `name` keyword.
> - The `name` parameter can use any of the defined CI variables,
>   including predefined, secure variables and `.gitlab-ci.yml` [`variables`](#variables).
>   You however cannot use variables defined under `script`.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
751

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
The `environment` name can contain:

- letters
- digits
- spaces
- `-`
- `_`
- `/`
- `$`
- `{`
- `}`

Common names are `qa`, `staging`, and `production`, but you can use whatever
name works with your workflow.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
767
768
769
770
Instead of defining the name of the environment right after the `environment`
keyword, it is also possible to define it as a separate value. For that, use
the `name` keyword under `environment`:

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
771
```yaml
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
772
773
774
775
776
777
778
deploy to production:
  stage: deploy
  script: git push production HEAD:master
  environment:
    name: production
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
779
### `environment:url`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
780

Brett Walker's avatar
Brett Walker включено в состав коммита
781
> **Notes:**
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
782
>
Brett Walker's avatar
Brett Walker включено в состав коммита
783
784
785
786
787
788
> - Introduced in GitLab 8.11.
> - Before GitLab 8.11, the URL could be added only in GitLab's UI. The
>   recommended way now is to define it in `.gitlab-ci.yml`.
> - The `url` parameter can use any of the defined CI variables,
>   including predefined, secure variables and `.gitlab-ci.yml` [`variables`](#variables).
>   You however cannot use variables defined under `script`.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
789
790
791
792
793
794
795
796

This is an optional value that when set, it exposes buttons in various places
in GitLab which when clicked take you to the defined URL.

In the example below, if the job finishes successfully, it will create buttons
in the merge requests and in the environments/deployments pages which will point
to `https://prod.example.com`.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
797
```yaml
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
798
799
800
801
802
803
804
805
deploy to production:
  stage: deploy
  script: git push production HEAD:master
  environment:
    name: production
    url: https://prod.example.com
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
806
### `environment:on_stop`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
807

Brett Walker's avatar
Brett Walker включено в состав коммита
808
> **Notes:**
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
809
>
Brett Walker's avatar
Brett Walker включено в состав коммита
810
811
812
813
> - [Introduced][ce-6669] in GitLab 8.13.
> - Starting with GitLab 8.14, when you have an environment that has a stop action
>   defined, GitLab will automatically trigger a stop action when the associated
>   branch is deleted.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
814

Bryce Chidester's avatar
Bryce Chidester включено в состав коммита
815
Closing (stopping) environments can be achieved with the `on_stop` keyword defined under
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
816
817
818
819
820
`environment`. It declares a different job that runs in order to close
the environment.

Read the `environment:action` section for an example.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
821
### `environment:action`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
822
823
824
825
826
827

> [Introduced][ce-6669] in GitLab 8.13.

The `action` keyword is to be used in conjunction with `on_stop` and is defined
in the job that is called to close the environment.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
Take for instance:

```yaml
review_app:
  stage: deploy
  script: make deploy-app
  environment:
    name: review
    on_stop: stop_review_app

stop_review_app:
  stage: deploy
  script: make delete-app
  when: manual
  environment:
    name: review
    action: stop
```

In the above example we set up the `review_app` job to deploy to the `review`
environment, and we also defined a new `stop_review_app` job under `on_stop`.
Once the `review_app` job is successfully finished, it will trigger the
`stop_review_app` job based on what is defined under `when`. In this case we
set it up to `manual` so it will need a [manual action](#manual-actions) via
GitLab's web interface in order to run.

The `stop_review_app` job is **required** to have the following keywords defined:

- `when` - [reference](#when)
- `environment:name`
- `environment:action`
- `stage` should be the same as the `review_app` in order for the environment
  to stop automatically when the branch is deleted

### Dynamic environments

Brett Walker's avatar
Brett Walker включено в состав коммита
864
> **Notes:**
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
865
>
Brett Walker's avatar
Brett Walker включено в состав коммита
866
867
868
869
870
> - [Introduced][ce-6323] in GitLab 8.12 and GitLab Runner 1.6.
> - The `$CI_ENVIRONMENT_SLUG` was [introduced][ce-7983] in GitLab 8.15.
> - The `name` and `url` parameters can use any of the defined CI variables,
>   including predefined, secure variables and `.gitlab-ci.yml` [`variables`](#variables).
>   You however cannot use variables defined under `script`.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899

For example:

```yaml
deploy as review app:
  stage: deploy
  script: make deploy
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: https://$CI_ENVIRONMENT_SLUG.example.com/
```

The `deploy as review app` job will be marked as deployment to dynamically
create the `review/$CI_COMMIT_REF_NAME` environment, where `$CI_COMMIT_REF_NAME`
is an [environment variable][variables] set by the Runner. The
`$CI_ENVIRONMENT_SLUG` variable is based on the environment name, but suitable
for inclusion in URLs. In this case, if the `deploy as review app` job was run
in a branch named `pow`, this environment would be accessible with an URL like
`https://review-pow.example.com/`.

This of course implies that the underlying server which hosts the application
is properly configured.

The common use case is to create dynamic environments for branches and use them
as Review Apps. You can see a simple example using Review Apps at
<https://gitlab.com/gitlab-examples/review-apps-nginx/>.

## `cache`

Brett Walker's avatar
Brett Walker включено в состав коммита
900
> **Notes:**
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
901
>
Brett Walker's avatar
Brett Walker включено в состав коммита
902
903
904
905
906
> - Introduced in GitLab Runner v0.7.0.
> - `cache` can be set globally and per-job.
> - From GitLab 9.0, caching is enabled and shared between pipelines and jobs
>   by default.
> - From GitLab 9.2, caches are restored before [artifacts](#artifacts).
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
907

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
908
909
910
911
TIP: **Learn more:**
Read how caching works and find out some good practices in the
[caching dependencies documentation](../caching/index.md).

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
912
913
914
915
916
917
918
`cache` is used to specify a list of files and directories which should be
cached between jobs. You can only use paths that are within the project
workspace.

If `cache` is defined outside the scope of jobs, it means it is set
globally and all jobs will use that definition.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
919
### `cache:paths`
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
920

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
921
922
Use the `paths` directive to choose which files or directories will be cached.
Wildcards can be used as well.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
923

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
924
Cache all files in `binaries` that end in `.apk` and the `.config` file:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
925
926
927
928
929
930

```yaml
rspec:
  script: test
  cache:
    paths:
pityonline's avatar
pityonline включено в состав коммита
931
932
      - binaries/*.apk
      - .config
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
933
934
935
936
937
938
939
940
```

Locally defined cache overrides globally defined options. The following `rspec`
job will cache only `binaries/`:

```yaml
cache:
  paths:
pityonline's avatar
pityonline включено в состав коммита
941
    - my/files
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
942
943
944
945

rspec:
  script: test
  cache:
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
946
    key: rspec
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
947
    paths:
pityonline's avatar
pityonline включено в состав коммита
948
      - binaries/
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
949
950
```

Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
951
952
953
954
Note that since cache is shared between jobs, if you're using different
paths for different jobs, you should also set a different **cache:key**
otherwise cache content can be overwritten.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
955
956
957
958
### `cache:key`

> Introduced in GitLab Runner v1.0.0.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
959
960
961
Since the cache is shared between jobs, if you're using different
paths for different jobs, you should also set a different `cache:key`
otherwise cache content can be overwritten.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
962

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
963
964
965
966
The `key` directive allows you to define the affinity of caching between jobs,
allowing to have a single cache for all jobs, cache per-job, cache per-branch
or any other way that fits your workflow. This way, you can fine tune caching,
allowing you to cache data between different jobs or even different branches.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
967
968

The `cache:key` variable can use any of the
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
969
970
971
[predefined variables](../variables/README.md), and the default key, if not
set, is just literal `default` which means everything is shared between each
pipelines and jobs by default, starting from GitLab 9.0.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
972
973
974
975
976

NOTE: **Note:**
The `cache:key` variable cannot contain the `/` character, or the equivalent
URI-encoded `%2F`; a value made only of dots (`.`, `%2E`) is also forbidden.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
977
For example, to enable per-branch caching:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
978
979
980
981

```yaml
cache:
  key: "$CI_COMMIT_REF_SLUG"
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
982
  paths:
pityonline's avatar
pityonline включено в состав коммита
983
    - binaries/
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
984
985
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
986
987
If you use **Windows Batch** to run your shell scripts you need to replace
`$` with `%`:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
988
989
990

```yaml
cache:
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
991
  key: "%CI_COMMIT_REF_SLUG%"
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
992
  paths:
pityonline's avatar
pityonline включено в состав коммита
993
    - binaries/
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
994
995
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
996
997
998
999
### `cache:untracked`

Set `untracked: true` to cache all files that are untracked in your Git
repository:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
1000

Для ускорения просмотра не вся история отображается Просмотреть всю вину