README.md 23,7 КБ
Newer Older
Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
1
2
3
4
---
table_display_block: true
---

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
5
# GitLab CI/CD environment variables
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
6

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
7
8
9
10
After a brief overview over the use of environment
variables, this document teaches you how to use GitLab CI/CD's
variables, presents the full reference for predefined variables,
and dives into more advanced applications.
Evan Read's avatar
Evan Read включено в состав коммита
11

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
12
## Overview
Douwe Maan's avatar
Douwe Maan включено в состав коммита
13

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
14
15
16
An environment variable is a dynamic-named value that can
affect the way running processes will behave on an operating
system.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
17

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
18
19
20
21
22
They are part of the environment in which a process runs.
For example, a running process can query the value of the
`TEMP` environment variable to discover a suitable location
to store temporary files, or to define a `URL` for a database
that can be reused in different scripts.
Evan Read's avatar
Evan Read включено в состав коммита
23

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
24
25
Variables are useful for customizing your jobs in GitLab
CI/CD's pipelines. Using variables means no hardcoded values.
Evan Read's avatar
Evan Read включено в состав коммита
26

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
27
### Predefined environment variables
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
28

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
29
30
31
32
33
GitLab CI/CD has a default set of
[predefined variables](predefined_variables.md)
which can be used without any specification needed.
You can call issues numbers, user names, branch names,
pipeline and commit IDs, and much more.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
34

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
35
36
Predefined environment variables are the ones that GitLab
provides out of the box for the local environment of the Runner.
Evan Read's avatar
Evan Read включено в состав коммита
37

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
38
39
40
GitLab reads the .gitlab-ci.yml file, sends the information
to the Runner (which runs the script commands), under which
the variables are exposed.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
41

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
42
43
44
For example, two jobs under the same pipeline can share the same
`CI_PIPELINE_ID` variable, but each one has its own `CI_JOB_ID`
variable.
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
45

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
### Custom environment variables

When your use case requires a specific variable, you can
[set them up easily from the UI](#creating-a-custom-environment-variable)
or directly in the `.gitlab-ci.yml` file and reuse them as you wish.

That can be very powerful as it can be used for scripting without
the need to specify the value itself.

## Getting started

To get started with environment variables in the scope of GitLab
CI/CD, let's go over a few examples.

### Using predefined environment variables

To get started, choose one of the existing
[predefined variables](predefined_variables.md)
to be output by the Runner. For example, let's say that you want
a given job you're running through your script to output the
stage that job is running for. In your `.gitlab-ci.yml` file,
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
67
call the variable from your script according to the [syntaxes](#syntax-of-environment-variables-in-job-scripts) available. To
Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
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
output the job stage, use the predefined variable `CI_JOB_STAGE`:

```yaml
test_variable:
  stage: test
  script:
    - echo $CI_JOB_STAGE
```

For this case, the Runner will output the `stage` for the
job `test_variable`, which is `test`:

![Output `$CI_JOB_STAGE`](img/ci_job_stage_output_example.png)

As another example, let's say you're using your own GitLab
instance you want to know what domain your GitLab Pages are
served under. You can easily call it with the predefined
variable `$CI_PAGES_DOMAIN` in your script:

```yaml
pages:
  script:
    - ...
    - echo $CI_PAGES_DOMAIN
```

For GitLab.com users, the output will be `gitlab.io`. For your
private instance, the output will be whatever your sysadmin has
defined.

### Creating a custom environment variable

Assume you have something you want to repeat through your scripts
in GitLab CI/CD's configuration file. To keep this example simple,
let's say you want to output `HELLO WORLD` for a `TEST` variable.

You can either set the variable directly in the `.gitlab-ci.yml`
file or through the UI.

#### Via [`.gitlab-ci.yml`](../yaml/README.md#variables)

```yaml
variables:
  TEST: "HELLO WORLD"
```

For a deeper look into them, see [`.gitlab-ci.yml` defined variables](#gitlab-ciyml-defined-variables).

#### Via the UI

From the UI, navigate to your project's **Settings > CI/CD** and
expand **Environment variables**. Create a new variable by naming
it in the field **Input variable key**, and define its value in the
**Input variable value** field:

![CI/CD settings - new variable](img/new_custom_variable_example.png)

Once you've set the variables, call them from the `.gitlab-ci.yml` file:

```yaml
test_variable:
  stage: test
  script:
    - echo $CI_JOB_STAGE # calls a predefined variable
    - echo $TEST # calls a custom variable
```

The output will be:

![Output custom variable](img/custom_variable_output.png)

CAUTION: **Important:**
Be aware that variables are not masked, and their values can be shown
in the job logs if explicitly asked to do so. If your project is public or
internal, you can set the pipelines private from your [project's Pipelines
settings](../../user/project/pipelines/settings.md#visibility-of-pipelines).
Follow the discussion in issue [#13784][ce-13784] for masking the variables.

### Syntax of environment variables in job scripts
Tomasz Maczukin's avatar
Tomasz Maczukin включено в состав коммита
147

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
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
All variables are set as environment variables in the build environment, and
they are accessible with normal methods that are used to access such variables.
In most cases `bash` or `sh` is used to execute the job script.

To access environment variables, use the syntax for your Runner's [shell][shellexecutors].

| Shell                | Usage           |
|----------------------|-----------------|
| bash/sh              | `$variable`     |
| windows batch        | `%variable%`    |
| PowerShell           | `$env:variable` |

To access environment variables in bash, prefix the variable name with (`$`):

```yaml
job_name:
  script:
    - echo $CI_JOB_ID
```

To access environment variables in **Windows Batch**, surround the variable
with (`%`):

```yaml
job_name:
  script:
    - echo %CI_JOB_ID%
```
Douwe Maan's avatar
Douwe Maan включено в состав коммита
176

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
To access environment variables in a **Windows PowerShell** environment, prefix
the variable name with (`$env:`):

```yaml
job_name:
  script:
    - echo $env:CI_JOB_ID
```

You can also list all environment variables with the `export` command,
but be aware that this will also expose the values of all the variables
you set, in the job log:

```yaml
job_name:
  script:
    - export
```

Example values:

```bash
export CI_JOB_ID="50"
export CI_COMMIT_SHA="1ecfd275763eff1d6b4844ea3168962458c9f27a"
export CI_COMMIT_SHORT_SHA="1ecfd275"
export CI_COMMIT_REF_NAME="master"
export CI_REPOSITORY_URL="https://gitlab-ci-token:abcde-1234ABCD5678ef@example.com/gitlab-org/gitlab-ce.git"
export CI_COMMIT_TAG="1.0.0"
export CI_JOB_NAME="spec:other"
export CI_JOB_STAGE="test"
export CI_JOB_MANUAL="true"
export CI_JOB_TRIGGERED="true"
export CI_JOB_TOKEN="abcde-1234ABCD5678ef"
export CI_PIPELINE_ID="1000"
export CI_PIPELINE_IID="10"
export CI_PAGES_DOMAIN="gitlab.io"
export CI_PAGES_URL="https://gitlab-org.gitlab.io/gitlab-ce"
export CI_PROJECT_ID="34"
export CI_PROJECT_DIR="/builds/gitlab-org/gitlab-ce"
export CI_PROJECT_NAME="gitlab-ce"
export CI_PROJECT_NAMESPACE="gitlab-org"
export CI_PROJECT_PATH="gitlab-org/gitlab-ce"
export CI_PROJECT_URL="https://example.com/gitlab-org/gitlab-ce"
export CI_REGISTRY="registry.example.com"
export CI_REGISTRY_IMAGE="registry.example.com/gitlab-org/gitlab-ce"
export CI_RUNNER_ID="10"
export CI_RUNNER_DESCRIPTION="my runner"
export CI_RUNNER_TAGS="docker, linux"
export CI_SERVER="yes"
export CI_SERVER_NAME="GitLab"
export CI_SERVER_REVISION="70606bf"
export CI_SERVER_VERSION="8.9.0"
export CI_SERVER_VERSION_MAJOR="8"
export CI_SERVER_VERSION_MINOR="9"
export CI_SERVER_VERSION_PATCH="0"
export GITLAB_USER_ID="42"
export GITLAB_USER_EMAIL="user@example.com"
export CI_REGISTRY_USER="gitlab-ci-token"
export CI_REGISTRY_PASSWORD="longalfanumstring"
```
Douwe Maan's avatar
Douwe Maan включено в состав коммита
237

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
238
### `.gitlab-ci.yml` defined variables
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
239

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
240
NOTE: **Note:**
Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
241
This feature requires GitLab Runner 0.5.0 or higher and GitLab 7.14 or higher.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
242

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
243
244
GitLab CI allows you to add to `.gitlab-ci.yml` variables that are set in the
build environment. The variables are hence saved in the repository, and they
Evan Read's avatar
Evan Read включено в состав коммита
245
are meant to store non-sensitive project configuration. For example, `RAILS_ENV` or
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
246
247
248
249
`DATABASE_URL`.

For example, if you set the variable below globally (not inside a job), it will
be used in all executed commands and scripts:
Douwe Maan's avatar
Douwe Maan включено в состав коммита
250
251
252
253
254
255

```yaml
variables:
  DATABASE_URL: "postgres://postgres@postgres/my_database"
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
256
257
258
259
260
The YAML-defined variables are also set to all created
[service containers](../docker/using_docker_images.md), thus allowing to fine
tune them.

Variables can be defined at a global level, but also at a job level. To turn off
Tor Bechmann Yamamoto-Sørensen's avatar
Tor Bechmann Yamamoto-Sørensen включено в состав коммита
261
global defined variables in your job, define an empty hash:
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
262
263
264

```yaml
job_name:
Sébastien's avatar
Sébastien включено в состав коммита
265
  variables: {}
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
266
267
```

TeNNoX's avatar
TeNNoX включено в состав коммита
268
269
270
271
272
273
274
275
276
277
You are able to use other variables inside your variable definition (or escape them with `$$`):

```yaml
variables:
  LS_CMD: 'ls $FLAGS $$TMP_DIR'
  FLAGS: '-al'
script:
  - 'eval $LS_CMD'  # will execute 'ls -al $TMP_DIR'
```

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
278
### Group-level environment variables
Douwe Maan's avatar
Douwe Maan включено в состав коммита
279

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
280
> Introduced in GitLab 9.4.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
281

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
282
283
GitLab CI/CD allows you to define per-project or per-group variables
that are set in the pipeline environment. Group-level variables are stored out of
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
284
the repository (not in `.gitlab-ci.yml`) and are securely passed to GitLab Runner
Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
285
making them available during a pipeline run. It's the **recommended method** to
Evan Read's avatar
Evan Read включено в состав коммита
286
287
use for storing things like passwords, SSH keys, and credentials.

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
288
Group-level variables can be added by:
Grzegorz Bizon's avatar
Grzegorz Bizon включено в состав коммита
289

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
290
1. Navigating to your group's **Settings > CI/CD** page.
Evan Read's avatar
Evan Read включено в состав коммита
291
1. Inputing variable keys and values in the **Environment variables** section.
Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
292
Any variables of [subgroups](../../user/group/subgroups/index.md) will be inherited recursively.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
293

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
294
Once you set them, they will be available for all subsequent pipelines.
Douwe Maan's avatar
Douwe Maan включено в состав коммита
295

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
296
297
298
299
300
301
302
## Priority of environment variables

Variables of different types can take precedence over other
variables, depending on where they are defined.

The order of precedence for variables is (from highest to lowest):

Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
303
304
305
1. [Trigger variables](../triggers/README.md#making-use-of-trigger-variables) or [scheduled pipeline variables](../../user/project/pipelines/schedules.md#using-variables).
1. Project-level [variables](#creating-a-custom-environment-variable) or [protected variables](#protected-environment-variables).
1. Group-level [variables](#group-level-environment-variables) or [protected variables](#protected-environment-variables).
Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
306
307
1. YAML-defined [job-level variables](../yaml/README.md#variables).
1. YAML-defined [global variables](../yaml/README.md#variables).
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
308
1. [Deployment variables](#deployment-environment-variables).
Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
309
310
311
312
313
314
1. [Predefined environment variables](predefined_variables.md).

For example, if you define:

- `API_TOKEN=secure` as a project variable.
- `API_TOKEN=yaml` in your `.gitlab-ci.yml`.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
315

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
316
317
`API_TOKEN` will take the value `secure` as the project
variables take precedence over those defined in `.gitlab-ci.yml`.
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
318

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
319
320
321
322
323
324
325
326
327
## Unsupported variables

There are cases where some variables cannot be used in the context of a
`.gitlab-ci.yml` definition (for example under `script`). Read more
about which variables are [not supported](where_variables_can_be_used.md).

## Advanced use

### Protected environment variables
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
328

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
329
> Introduced in GitLab 9.3.
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
330

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
331
Variables can be protected. Whenever a variable is
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
332
protected, it would only be securely passed to pipelines running on the
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
333
[protected branches] or [protected tags]. The other pipelines would not get any
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
334
335
336
protected variables.

Protected variables can be added by going to your project's
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
337
**Settings > CI/CD**, then finding the section called
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
338
**Variables**, and check "Protected".
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
339
340

Once you set them, they will be available for all subsequent pipelines.
Nick Thomas's avatar
Nick Thomas включено в состав коммита
341

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
342
### Deployment environment variables
Adam Niedzielski's avatar
Adam Niedzielski включено в состав коммита
343

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
344
> Introduced in GitLab 8.15.
Adam Niedzielski's avatar
Adam Niedzielski включено в состав коммита
345

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
346
[Project services](../../user/project/integrations/project_services.md) that are
Adam Niedzielski's avatar
Adam Niedzielski включено в состав коммита
347
348
responsible for deployment configuration may define their own variables that
are set in the build environment. These variables are only defined for
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
349
[deployment jobs](../environments.md). Please consult the documentation of
Adam Niedzielski's avatar
Adam Niedzielski включено в состав коммита
350
351
the project services that you are using to learn which variables they define.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
352
353
An example project service that defines deployment variables is the
[Kubernetes integration](../../user/project/clusters/index.md#deployment-variables).
Adam Niedzielski's avatar
Adam Niedzielski включено в состав коммита
354

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
355
### Auto DevOps environment variables
Thong Kuah's avatar
Thong Kuah включено в состав коммита
356
357
358
359
360
361
362
363
364
365
366
367

> [Introduced](https://gitlab.com/gitlab-org/gitlab-ce/issues/49056) in GitLab 11.7.

You can configure [Auto DevOps](../../topics/autodevops/index.md) to
pass CI variables to the running application by prefixing the key of the
variable with `K8S_SECRET_`.

These [prefixed
variables](../../topics/autodevops/index.md#application-secret-variables) will
then be available as environment variables on the running application
container.

Thong Kuah's avatar
Thong Kuah включено в состав коммита
368
CAUTION: **Caution:**
Thong Kuah's avatar
Thong Kuah включено в состав коммита
369
Variables with multiline values are not currently supported due to
Thong Kuah's avatar
Thong Kuah включено в состав коммита
370
371
limitations with the current Auto DevOps scripting environment.

Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
372
373
374
375
376
377
378
### Environment variables triggered manually

> [Introduced](https://gitlab.com/gitlab-org/gitlab-ce/issues/44059) in GitLab 10.8.

[Manually triggered pipelines](../pipelines.md#manually-executing-pipelines) allow you to override the value of a current variable.

For instance, suppose you added a
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
379
[custom variable `$TEST`](#creating-a-custom-environment-variable)
Marcia Ramos's avatar
Marcia Ramos включено в состав коммита
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
as exemplified above and you want to override it in a manual pipeline.
Navigate to your project's **CI/CD > Pipelines** and click **Run pipeline**.
Choose the branch you want to run the pipeline for, then add a new variable
pair through the UI:

![Override variable value](img/override_variable_manual_pipeline.png)

The Runner will override the value previously set and use the custom
value you set for this specific pipeline:

![Manually overridden variable output](img/override_value_via_manual_pipeline_output.png)

## Environment variables expressions

> Introduced in GitLab 10.7.

It is possible to use variables expressions with only / except policies in
`.gitlab-ci.yml`. By using this approach you can limit what jobs are going to
be created within a pipeline after pushing a code to GitLab.

This is particularly useful in combination with variables and triggered
pipeline variables.

```yaml
deploy:
  script: cap staging deploy
  environment: staging
  only:
    variables:
      - $RELEASE == "staging"
      - $STAGING
```

Each expression provided is going to be evaluated before creating a pipeline.

If any of the conditions in `variables` evaluates to truth when using `only`,
a new job is going to be created. If any of the expressions evaluates to truth
when `except` is being used, a job is not going to be created.

This follows usual rules for [`only` / `except` policies](../yaml/README.md#onlyexcept-advanced).

### Supported syntax

Below you can find supported syntax reference:

1. Equality matching using a string

    > Example: `$VARIABLE == "some value"`

    You can use equality operator `==` to compare a variable content to a
    string. We support both, double quotes and single quotes to define a string
    value, so both `$VARIABLE == "some value"` and `$VARIABLE == 'some value'`
    are supported. `"some value" == $VARIABLE` is correct too.

1. Checking for an undefined value

    > Example: `$VARIABLE == null`

    It sometimes happens that you want to check whether a variable is defined
    or not. To do that, you can compare a variable to `null` keyword, like
    `$VARIABLE == null`. This expression is going to evaluate to truth if
    variable is not defined.

1. Checking for an empty variable

    > Example: `$VARIABLE == ""`

    If you want to check whether a variable is defined, but is empty, you can
    simply compare it against an empty string, like `$VAR == ''`.

1. Comparing two variables

    > Example: `$VARIABLE_1 == $VARIABLE_2`

    It is possible to compare two variables. This is going to compare values
    of these variables.

1. Variable presence check

    > Example: `$STAGING`

    If you only want to create a job when there is some variable present,
    which means that it is defined and non-empty, you can simply use
    variable name as an expression, like `$STAGING`. If `$STAGING` variable
    is defined, and is non empty, expression will evaluate to truth.
    `$STAGING` value needs to a string, with length higher than zero.
    Variable that contains only whitespace characters is not an empty variable.

1. Pattern matching  _(added in 11.0)_

    > Example: `$VARIABLE =~ /^content.*/`

    It is possible perform pattern matching against a variable and regular
    expression. Expression like this evaluates to truth if matches are found.

    Pattern matching is case-sensitive by default. Use `i` flag modifier, like
    `/pattern/i` to make a pattern case-insensitive.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
478
479
480
## Debug tracing

> Introduced in GitLab Runner 1.7.
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
481
482
483

CAUTION: **Warning:**
Enabling debug tracing can have severe security implications. The
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
484
output **will** contain the content of all your variables and any other
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
485
486
secrets! The output **will** be uploaded to the GitLab server and made visible
in job traces!
Nick Thomas's avatar
Nick Thomas включено в состав коммита
487
488

By default, GitLab Runner hides most of the details of what it is doing when
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
489
processing a job. This behavior keeps job traces short, and prevents secrets
Nick Thomas's avatar
Nick Thomas включено в состав коммита
490
491
492
493
494
from being leaked into the trace unless your script writes them to the screen.

If a job isn't working as expected, this can make the problem difficult to
investigate; in these cases, you can enable debug tracing in `.gitlab-ci.yml`.
Available on GitLab Runner v1.7+, this feature enables the shell's execution
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
495
trace, resulting in a verbose job trace listing all commands that were run,
Nick Thomas's avatar
Nick Thomas включено в состав коммита
496
497
variables that were set, etc.

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
498
Before enabling this, you should ensure jobs are visible to
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
499
[team members only](../../user/permissions.md#project-features). You should
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
500
also [erase](../pipelines.md#accessing-individual-jobs) all generated job traces
Nick Thomas's avatar
Nick Thomas включено в состав коммита
501
502
503
504
505
before making them visible again.

To enable debug traces, set the `CI_DEBUG_TRACE` variable to `true`:

```yaml
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
506
job_name:
Nick Thomas's avatar
Nick Thomas включено в состав коммита
507
508
509
510
  variables:
    CI_DEBUG_TRACE: "true"
```

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
Example truncated output with debug trace set to true:

```bash
...

export CI_SERVER_TLS_CA_FILE="/builds/gitlab-examples/ci-debug-trace.tmp/CI_SERVER_TLS_CA_FILE"
if [[ -d "/builds/gitlab-examples/ci-debug-trace/.git" ]]; then
  echo $'\''\x1b[32;1mFetching changes...\x1b[0;m'\''
  $'\''cd'\'' "/builds/gitlab-examples/ci-debug-trace"
  $'\''git'\'' "config" "fetch.recurseSubmodules" "false"
  $'\''rm'\'' "-f" ".git/index.lock"
  $'\''git'\'' "clean" "-ffdx"
  $'\''git'\'' "reset" "--hard"
  $'\''git'\'' "remote" "set-url" "origin" "https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@example.com/gitlab-examples/ci-debug-trace.git"
  $'\''git'\'' "fetch" "origin" "--prune" "+refs/heads/*:refs/remotes/origin/*" "+refs/tags/*:refs/tags/*"
else
  $'\''mkdir'\'' "-p" "/builds/gitlab-examples/ci-debug-trace.tmp/git-template"
  $'\''rm'\'' "-r" "-f" "/builds/gitlab-examples/ci-debug-trace"
  $'\''git'\'' "config" "-f" "/builds/gitlab-examples/ci-debug-trace.tmp/git-template/config" "fetch.recurseSubmodules" "false"
  echo $'\''\x1b[32;1mCloning repository...\x1b[0;m'\''
  $'\''git'\'' "clone" "--no-checkout" "https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@example.com/gitlab-examples/ci-debug-trace.git" "/builds/gitlab-examples/ci-debug-trace" "--template" "/builds/gitlab-examples/ci-debug-trace.tmp/git-template"
  $'\''cd'\'' "/builds/gitlab-examples/ci-debug-trace"
fi
echo $'\''\x1b[32;1mChecking out dd648b2e as master...\x1b[0;m'\''
$'\''git'\'' "checkout" "-f" "-q" "dd648b2e48ce6518303b0bb580b2ee32fadaf045"
'
+++ hostname
++ echo 'Running on runner-8a2f473d-project-1796893-concurrent-0 via runner-8a2f473d-machine-1480971377-317a7d0f-digital-ocean-4gb...'
Running on runner-8a2f473d-project-1796893-concurrent-0 via runner-8a2f473d-machine-1480971377-317a7d0f-digital-ocean-4gb...
++ export CI=true
++ CI=true
Olliver Schinagl's avatar
Olliver Schinagl включено в состав коммита
542
543
++ export CI_API_V4_API_URL=https://example.com:3000/api/v4
++ CI_API_V4_API_URL=https://example.com:3000/api/v4
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
544
545
++ export CI_DEBUG_TRACE=false
++ CI_DEBUG_TRACE=false
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg включено в состав коммита
546
547
++ export CI_COMMIT_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ CI_COMMIT_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
Jason Lenny's avatar
Jason Lenny включено в состав коммита
548
549
++ export CI_COMMIT_SHORT_SHA=dd648b2e
++ CI_COMMIT_SHORT_SHA=dd648b2e
Z.J. van de Weg's avatar
Z.J. van de Weg включено в состав коммита
550
551
552
553
554
555
556
557
558
559
++ export CI_COMMIT_BEFORE_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ CI_COMMIT_BEFORE_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ export CI_COMMIT_REF_NAME=master
++ CI_COMMIT_REF_NAME=master
++ export CI_JOB_ID=7046507
++ CI_JOB_ID=7046507
++ export CI_REPOSITORY_URL=https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@example.com/gitlab-examples/ci-debug-trace.git
++ CI_REPOSITORY_URL=https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@example.com/gitlab-examples/ci-debug-trace.git
++ export CI_JOB_TOKEN=xxxxxxxxxxxxxxxxxxxx
++ CI_JOB_TOKEN=xxxxxxxxxxxxxxxxxxxx
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
560
561
562
563
564
565
566
567
568
569
++ export CI_PROJECT_ID=1796893
++ CI_PROJECT_ID=1796893
++ export CI_PROJECT_DIR=/builds/gitlab-examples/ci-debug-trace
++ CI_PROJECT_DIR=/builds/gitlab-examples/ci-debug-trace
++ export CI_SERVER=yes
++ CI_SERVER=yes
++ export 'CI_SERVER_NAME=GitLab CI'
++ CI_SERVER_NAME='GitLab CI'
++ export CI_SERVER_VERSION=
++ CI_SERVER_VERSION=
Gilbert Roulot's avatar
Gilbert Roulot включено в состав коммита
570
571
572
573
574
575
++ export CI_SERVER_VERSION_MAJOR=
++ CI_SERVER_VERSION_MAJOR=
++ export CI_SERVER_VERSION_MINOR=
++ CI_SERVER_VERSION_MINOR=
++ export CI_SERVER_VERSION_PATCH=
++ CI_SERVER_VERSION_PATCH=
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
576
577
578
579
580
581
++ export CI_SERVER_REVISION=
++ CI_SERVER_REVISION=
++ export GITLAB_CI=true
++ GITLAB_CI=true
++ export CI=true
++ CI=true
Olliver Schinagl's avatar
Olliver Schinagl включено в состав коммита
582
583
++ export CI_API_V4_API_URL=https://example.com:3000/api/v4
++ CI_API_V4_API_URL=https://example.com:3000/api/v4
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
584
585
++ export GITLAB_CI=true
++ GITLAB_CI=true
Z.J. van de Weg's avatar
Z.J. van de Weg включено в состав коммита
586
587
588
589
590
591
592
593
594
595
596
597
598
599
++ export CI_JOB_ID=7046507
++ CI_JOB_ID=7046507
++ export CI_JOB_TOKEN=xxxxxxxxxxxxxxxxxxxx
++ CI_JOB_TOKEN=xxxxxxxxxxxxxxxxxxxx
++ export CI_COMMIT_REF=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ CI_COMMIT_REF=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ export CI_COMMIT_BEFORE_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ CI_COMMIT_BEFORE_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ export CI_COMMIT_REF_NAME=master
++ CI_COMMIT_REF_NAME=master
++ export CI_COMMIT_NAME=debug_trace
++ CI_JOB_NAME=debug_trace
++ export CI_JOB_STAGE=test
++ CI_JOB_STAGE=test
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
600
601
602
603
604
605
++ export CI_SERVER_NAME=GitLab
++ CI_SERVER_NAME=GitLab
++ export CI_SERVER_VERSION=8.14.3-ee
++ CI_SERVER_VERSION=8.14.3-ee
++ export CI_SERVER_REVISION=82823
++ CI_SERVER_REVISION=82823
Adrian Moisey's avatar
Adrian Moisey включено в состав коммита
606
607
608
609
++ export CI_PAGES_DOMAIN=gitlab.io
++ CI_PAGES_DOMAIN=gitlab.io
++ export CI_PAGES_URL=https://gitlab-examples.gitlab.io/ci-debug-trace
++ CI_PAGES_URL=https://gitlab-examples.gitlab.io/ci-debug-trace
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
610
611
612
613
614
615
616
617
618
619
620
621
++ export CI_PROJECT_ID=17893
++ CI_PROJECT_ID=17893
++ export CI_PROJECT_NAME=ci-debug-trace
++ CI_PROJECT_NAME=ci-debug-trace
++ export CI_PROJECT_PATH=gitlab-examples/ci-debug-trace
++ CI_PROJECT_PATH=gitlab-examples/ci-debug-trace
++ export CI_PROJECT_NAMESPACE=gitlab-examples
++ CI_PROJECT_NAMESPACE=gitlab-examples
++ export CI_PROJECT_URL=https://example.com/gitlab-examples/ci-debug-trace
++ CI_PROJECT_URL=https://example.com/gitlab-examples/ci-debug-trace
++ export CI_PIPELINE_ID=52666
++ CI_PIPELINE_ID=52666
Shinya Maeda's avatar
Shinya Maeda включено в состав коммита
622
623
++ export CI_PIPELINE_IID=123
++ CI_PIPELINE_IID=123
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
624
625
626
627
++ export CI_RUNNER_ID=1337
++ CI_RUNNER_ID=1337
++ export CI_RUNNER_DESCRIPTION=shared-runners-manager-1.example.com
++ CI_RUNNER_DESCRIPTION=shared-runners-manager-1.example.com
Pawel Chojnacki's avatar
Pawel Chojnacki включено в состав коммита
628
629
++ export 'CI_RUNNER_TAGS=shared, docker, linux, ruby, mysql, postgres, mongo'
++ CI_RUNNER_TAGS='shared, docker, linux, ruby, mysql, postgres, mongo'
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
630
631
632
633
634
635
636
++ export CI_REGISTRY=registry.example.com
++ CI_REGISTRY=registry.example.com
++ export CI_DEBUG_TRACE=true
++ CI_DEBUG_TRACE=true
++ export GITLAB_USER_ID=42
++ GITLAB_USER_ID=42
++ export GITLAB_USER_EMAIL=user@example.com
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
637
++ GITLAB_USER_EMAIL=user@example.com
Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
638
639
640
641
642
643
644
645
++ export VERY_SECURE_VARIABLE=imaverysecurevariable
++ VERY_SECURE_VARIABLE=imaverysecurevariable
++ mkdir -p /builds/gitlab-examples/ci-debug-trace.tmp
++ echo -n '-----BEGIN CERTIFICATE-----
MIIFQzCCBCugAwIBAgIRAL/ElDjuf15xwja1ZnCocWAwDQYJKoZIhvcNAQELBQAw'

...
```
Nick Thomas's avatar
Nick Thomas включено в состав коммита
646

Achilleas Pipinellis's avatar
Achilleas Pipinellis включено в состав коммита
647
[ce-13784]: https://gitlab.com/gitlab-org/gitlab-ce/issues/13784 "Simple protection of CI variables"
Marcel Amirault's avatar
Marcel Amirault включено в состав коммита
648
[eep]: https://about.gitlab.com/pricing/ "Available only in GitLab Premium"
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
649
[envs]: ../environments.md
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
650
651
[protected branches]: ../../user/project/protected_branches.md
[protected tags]: ../../user/project/protected_tags.md
John Spaetzel's avatar
John Spaetzel включено в состав коммита
652
[shellexecutors]: https://docs.gitlab.com/runner/executors/
Lin Jen-Shin's avatar
Lin Jen-Shin включено в состав коммита
653
[triggered]: ../triggers/README.md
Mayra Cabrera's avatar
Mayra Cabrera включено в состав коммита
654
[gitlab-deploy-token]: ../../user/project/deploy_tokens/index.md#gitlab-deploy-token
Steve Azzopardi's avatar
Steve Azzopardi включено в состав коммита
655
[registry]: ../../user/project/container_registry.md
Evan Read's avatar
Evan Read включено в состав коммита
656
[dependent-repositories]: ../../user/project/new_ci_build_permissions_model.md#dependent-repositories