README.md 18.7 KB
Newer Older
1
# Variables
2

3
When receiving a job from GitLab CI, the [Runner] prepares the build environment.
4
It starts by setting a list of **predefined variables** (environment variables)
5
and a list of **user-defined variables**.
Douwe Maan's avatar
Douwe Maan committed
6

7
## Priority of variables
8

9
10
11
12
The variables can be overwritten and they take precedence over each other in
this order:

1. [Trigger variables][triggers] (take precedence over all)
13
1. [Secret variables](#secret-variables) or [protected secret variables](#protected-secret-variables)
14
15
1. YAML-defined [job-level variables](../yaml/README.md#job-variables)
1. YAML-defined [global variables](../yaml/README.md#variables)
16
1. [Deployment variables](#deployment-variables)
17
18
1. [Predefined variables](#predefined-variables-environment-variables) (are the
   lowest in the chain)
Douwe Maan's avatar
Douwe Maan committed
19

20
For example, if you define `API_TOKEN=secure` as a secret variable and
21
`API_TOKEN=yaml` in your `.gitlab-ci.yml`, the `API_TOKEN` will take the value
22
`secure` as the secret variables are higher in the chain.
Douwe Maan's avatar
Douwe Maan committed
23

24
## Predefined variables (Environment variables)
Douwe Maan's avatar
Douwe Maan committed
25

26
27
28
Some of the predefined environment variables are available only if a minimum
version of [GitLab Runner][runner] is used. Consult the table below to find the
version of Runner required.
Douwe Maan's avatar
Douwe Maan committed
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
>**Note:**
Starting with GitLab 9.0, we have deprecated some variables. Read the
[9.0 Renaming](#9-0-renaming) section to find out their replacements. **You are
strongly advised to use the new variables as we will remove the old ones in
future GitLab releases.**

| Variable                        | GitLab | Runner | Description |
|-------------------------------- |--------|--------|-------------|
| **CI**                          | all    | 0.4    | Mark that job is executed in CI environment |
| **CI_COMMIT_REF_NAME**          | 9.0    | all    | The branch or tag name for which project is built |
| **CI_COMMIT_REF_SLUG**          | 9.0    | all    | `$CI_COMMIT_REF_NAME` lowercased, shortened to 63 bytes, and with everything except `0-9` and `a-z` replaced with `-`. Use in URLs and domain names. |
| **CI_COMMIT_SHA**               | 9.0    | all    | The commit revision for which project is built |
| **CI_COMMIT_TAG**               | 9.0    | 0.5    | The commit tag name. Present only when building tags. |
| **CI_DEBUG_TRACE**              | all    | 1.7    | Whether [debug tracing](#debug-tracing) is enabled |
| **CI_ENVIRONMENT_NAME**         | 8.15   | all    | The name of the environment for this job |
| **CI_ENVIRONMENT_SLUG**         | 8.15   | all    | A simplified version of the environment name, suitable for inclusion in DNS, URLs, Kubernetes labels, etc. |
| **CI_JOB_ID**                   | 9.0    | all    | The unique id of the current job that GitLab CI uses internally |
| **CI_JOB_MANUAL**               | 8.12   | all    | The flag to indicate that job was manually started |
| **CI_JOB_NAME**                 | 9.0    | 0.5    | The name of the job as defined in `.gitlab-ci.yml` |
| **CI_JOB_STAGE**                | 9.0    | 0.5    | The name of the stage as defined in `.gitlab-ci.yml` |
| **CI_JOB_TOKEN**                | 9.0    | 1.2    | Token used for authenticating with the GitLab Container Registry |
| **CI_REPOSITORY_URL**           | 9.0    | all    | The URL to clone the Git repository |
| **CI_RUNNER_DESCRIPTION**       | 8.10   | 0.5    | The description of the runner as saved in GitLab |
| **CI_RUNNER_ID**                | 8.10   | 0.5    | The unique id of runner being used |
| **CI_RUNNER_TAGS**              | 8.10   | 0.5    | The defined runner tags |
| **CI_PIPELINE_ID**              | 8.10   | 0.5    | The unique id of the current pipeline that GitLab CI uses internally |
| **CI_PIPELINE_TRIGGERED**       | all    | all    | The flag to indicate that job was [triggered] |
| **CI_PROJECT_DIR**              | all    | all    | The full path where the repository is cloned and where the job is run |
| **CI_PROJECT_ID**               | all    | all    | The unique id of the current project that GitLab CI uses internally |
59
| **CI_PROJECT_NAME**             | 8.10   | 0.5    | The project name that is currently being built (actually it is project folder name) |
60
61
| **CI_PROJECT_NAMESPACE**        | 8.10   | 0.5    | The project namespace (username or groupname) that is currently being built |
| **CI_PROJECT_PATH**             | 8.10   | 0.5    | The namespace with project name |
62
| **CI_PROJECT_PATH_SLUG**        | 9.3    | all    | `$CI_PROJECT_PATH` lowercased and with everything except `0-9` and `a-z` replaced with `-`. Use in URLs and domain names. |
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
| **CI_PROJECT_URL**              | 8.10   | 0.5    | The HTTP address to access project |
| **CI_REGISTRY**                 | 8.10   | 0.5    | If the Container Registry is enabled it returns the address of GitLab's Container Registry |
| **CI_REGISTRY_IMAGE**           | 8.10   | 0.5    | If the Container Registry is enabled for the project it returns the address of the registry tied to the specific project |
| **CI_REGISTRY_PASSWORD**        | 9.0    | all    | The password to use to push containers to the GitLab Container Registry |
| **CI_REGISTRY_USER**            | 9.0    | all    | The username to use to push containers to the GitLab Container Registry |
| **CI_SERVER**                   | all    | all    | Mark that job is executed in CI environment |
| **CI_SERVER_NAME**              | all    | all    | The name of CI server that is used to coordinate jobs |
| **CI_SERVER_REVISION**          | all    | all    | GitLab revision that is used to schedule jobs |
| **CI_SERVER_VERSION**           | all    | all    | GitLab version that is used to schedule jobs |
| **ARTIFACT_DOWNLOAD_ATTEMPTS**  | 8.15   | 1.9    | Number of attempts to download artifacts running a job |
| **GET_SOURCES_ATTEMPTS**        | 8.15   | 1.9    | Number of attempts to fetch sources running a job |
| **GITLAB_CI**                   | all    | all    | Mark that job is executed in GitLab CI environment |
| **GITLAB_USER_ID**              | 8.12   | all    | The id of the user who started the job |
| **GITLAB_USER_EMAIL**           | 8.12   | all    | The email of the user who started the job |
| **RESTORE_CACHE_ATTEMPTS**      | 8.15   | 1.9    | Number of attempts to restore the cache running a job |
Douwe Maan's avatar
Douwe Maan committed
78

79
80
81
82
83
84
## 9.0 Renaming

To follow conventions of naming across GitLab, and to futher move away from the
`build` term and toward `job` CI variables have been renamed for the 9.0
release.

85
86
87
88
89
90
91
92
93
| 8.x name              | 9.0+ name               |
| --------------------- |------------------------ |
| `CI_BUILD_ID`         | `CI_JOB_ID`             |
| `CI_BUILD_REF`        | `CI_COMMIT_SHA`         |
| `CI_BUILD_TAG`        | `CI_COMMIT_TAG`         |
| `CI_BUILD_REF_NAME`   | `CI_COMMIT_REF_NAME`    |
| `CI_BUILD_REF_SLUG`   | `CI_COMMIT_REF_SLUG`    |
| `CI_BUILD_NAME`       | `CI_JOB_NAME`           |
| `CI_BUILD_STAGE`      | `CI_JOB_STAGE`          |
94
| `CI_BUILD_REPO`       | `CI_REPOSITORY_URL`     |
95
96
97
| `CI_BUILD_TRIGGERED`  | `CI_PIPELINE_TRIGGERED` |
| `CI_BUILD_MANUAL`     | `CI_JOB_MANUAL`         |
| `CI_BUILD_TOKEN`      | `CI_JOB_TOKEN`          |
98

99
## `.gitlab-ci.yaml` defined variables
100
101
102

>**Note:**
This feature requires GitLab Runner 0.5.0 or higher and GitLab CI 7.14 or higher.
Douwe Maan's avatar
Douwe Maan committed
103

104
105
106
107
108
109
110
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
are meant to store non-sensitive project configuration, e.g., `RAILS_ENV` or
`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 committed
111
112
113
114
115
116

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

117
118
119
120
121
122
123
124
125
126
127
128
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
global defined variables in your job, define an empty array:

```yaml
job_name:
  variables: []
```

129
130
131
132
133
134
135
136
137
138
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'
```

139
## Secret variables
Douwe Maan's avatar
Douwe Maan committed
140

141
142
>**Notes:**
- This feature requires GitLab Runner 0.4.0 or higher.
143
- Be aware that secret variables are not masked, and their values can be shown
144
  in the job logs if explicitly asked to do so. If your project is public or
145
146
  internal, you can set the pipelines private from your project's Pipelines
  settings. Follow the discussion in issue [#13784][ce-13784] for masking the
147
  secret variables.
Douwe Maan's avatar
Douwe Maan committed
148

149
150
GitLab CI allows you to define per-project **secret variables** that are set in
the build environment. The secret variables are stored out of the repository
151
152
153
(`.gitlab-ci.yml`) and are securely passed to GitLab Runner making them
available in the build environment. It's the recommended method to use for
storing things like passwords, secret keys and credentials.
154

155
Secret variables can be added by going to your project's
156
**Settings ➔ Pipelines**, then finding the section called
157
**Secret variables**.
Douwe Maan's avatar
Douwe Maan committed
158

159
160
Once you set them, they will be available for all subsequent pipelines.

161
## Protected secret variables
162
163

>**Notes:**
164
This feature requires GitLab 9.3 or higher.
165

166
Secret variables could be protected. Whenever a secret variable is
167
protected, it would only be securely passed to pipelines running on the
168
[protected branches] or [protected tags]. The other pipelines would not get any
169
170
171
172
protected variables.

Protected variables can be added by going to your project's
**Settings ➔ Pipelines**, then finding the section called
173
**Secret variables**, and check *Protected*.
174
175

Once you set them, they will be available for all subsequent pipelines.
176

177
178
179
180
181
## Deployment variables

>**Note:**
This feature requires GitLab CI 8.15 or higher.

182
[Project services](../../user/project/integrations/project_services.md) that are
183
184
responsible for deployment configuration may define their own variables that
are set in the build environment. These variables are only defined for
185
[deployment jobs](../environments.md). Please consult the documentation of
186
187
188
the project services that you are using to learn which variables they define.

An example project service that defines deployment variables is
189
[Kubernetes Service](../../user/project/integrations/kubernetes.md).
190

191
192
193
194
## Debug tracing

> Introduced in GitLab Runner 1.7.
>
195
> **WARNING:** Enabling debug tracing can have severe security implications. The
196
  output **will** contain the content of all your secret variables and any other
197
  secrets! The output **will** be uploaded to the GitLab server and made visible
198
  in job traces!
199
200

By default, GitLab Runner hides most of the details of what it is doing when
201
processing a job. This behaviour keeps job traces short, and prevents secrets
202
203
204
205
206
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
207
trace, resulting in a verbose job trace listing all commands that were run,
208
209
variables that were set, etc.

210
Before enabling this, you should ensure jobs are visible to
211
[team members only](../../user/permissions.md#project-features). You should
212
also [erase](../pipelines.md#seeing-build-status) all generated job traces
213
214
215
216
217
before making them visible again.

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

```yaml
218
job_name:
219
220
221
222
  variables:
    CI_DEBUG_TRACE: "true"
```

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
++ export CI_DEBUG_TRACE=false
++ CI_DEBUG_TRACE=false
256
257
++ export CI_COMMIT_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ CI_COMMIT_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
258
259
260
261
262
263
264
265
266
267
++ 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
++ 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=
++ export CI_SERVER_REVISION=
++ CI_SERVER_REVISION=
++ export GITLAB_CI=true
++ GITLAB_CI=true
++ export CI=true
++ CI=true
++ export GITLAB_CI=true
++ GITLAB_CI=true
286
287
288
289
290
291
292
293
294
295
296
297
298
299
++ 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
++ 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
++ 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
++ 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 committed
322
323
++ export 'CI_RUNNER_TAGS=shared, docker, linux, ruby, mysql, postgres, mongo'
++ CI_RUNNER_TAGS='shared, docker, linux, ruby, mysql, postgres, mongo'
324
325
326
327
328
329
330
++ 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
331
++ GITLAB_USER_EMAIL=user@example.com
332
333
334
335
336
337
338
339
++ export VERY_SECURE_VARIABLE=imaverysecurevariable
++ VERY_SECURE_VARIABLE=imaverysecurevariable
++ mkdir -p /builds/gitlab-examples/ci-debug-trace.tmp
++ echo -n '-----BEGIN CERTIFICATE-----
MIIFQzCCBCugAwIBAgIRAL/ElDjuf15xwja1ZnCocWAwDQYJKoZIhvcNAQELBQAw'

...
```
340

341
## Using the CI variables in your job scripts
Douwe Maan's avatar
Douwe Maan committed
342

343
344
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.
345
In most cases `bash` or `sh` is used to execute the job script.
Douwe Maan's avatar
Douwe Maan committed
346

347
348
To access the variables (predefined and user-defined) in a `bash`/`sh` environment,
prefix the variable name with the dollar sign (`$`):
349

Douwe Maan's avatar
Douwe Maan committed
350
351
352
```
job_name:
  script:
Elan Ruusamäe's avatar
Elan Ruusamäe committed
353
    - echo $CI_JOB_ID
Douwe Maan's avatar
Douwe Maan committed
354
355
```

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

Douwe Maan's avatar
Douwe Maan committed
360
361
362
363
364
```
job_name:
  script:
    - export
```
365

366
367
368
369
370
371
Example values:

```bash
export CI_JOB_ID="50"
export CI_COMMIT_SHA="1ecfd275763eff1d6b4844ea3168962458c9f27a"
export CI_COMMIT_REF_NAME="master"
372
export CI_REPOSITORY_URL="https://gitlab-ci-token:abcde-1234ABCD5678ef@example.com/gitlab-org/gitlab-ce.git"
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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_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 GITLAB_USER_ID="42"
export GITLAB_USER_EMAIL="user@example.com"
export CI_REGISTRY_USER="gitlab-ci-token"
export CI_REGISTRY_PASSWORD="longalfanumstring"
```

401
[ce-13784]: https://gitlab.com/gitlab-org/gitlab-ce/issues/13784
402
[runner]: https://docs.gitlab.com/runner/
403
[triggered]: ../triggers/README.md
404
[triggers]: ../triggers/README.md#pass-job-variables-to-a-trigger
405
406
[protected branches]: ../../user/project/protected_branches.md
[protected tags]: ../../user/project/protected_tags.md