2016-11-25 13:41:16 +00:00
# Variables
2016-04-17 18:08:02 +00:00
2015-08-26 01:42:46 +00:00
When receiving a build from GitLab CI, the runner prepares the build environment.
2016-11-25 13:41:16 +00:00
It starts by setting a list of **predefined variables** (Environment variables)
and a list of **user-defined variables** .
2015-08-26 01:42:46 +00:00
2016-11-25 13:41:16 +00:00
The variables can be overwritten. They take precedence over each other in this
order:
1. Trigger variables (take precedence over all)
2015-08-26 01:42:46 +00:00
1. Secure variables
2016-04-18 10:29:36 +00:00
1. YAML-defined job-level variables
2016-04-17 18:08:02 +00:00
1. YAML-defined global variables
2016-11-25 13:41:16 +00:00
1. Predefined variables (are the lowest in the chain)
2015-08-26 01:42:46 +00:00
2016-11-25 13:41:16 +00:00
For example, if you define `API_TOKEN=secure` as a secure variable and
`API_TOKEN=yaml` as YAML-defined variable, the `API_TOKEN` will take the value
`secure` as the secure variables are higher in the chain.
2015-08-26 01:42:46 +00:00
2016-11-25 13:41:16 +00:00
## Predefined variables (Environment variables)
2015-08-26 01:42:46 +00:00
2016-11-25 13:41:16 +00:00
>**Note:**
Some of the variables are available only if a minimum version of [GitLab Runner]
is used.
2015-08-26 01:42:46 +00:00
2016-06-21 11:55:33 +00:00
| Variable | GitLab | Runner | Description |
|-------------------------|--------|--------|-------------|
| **CI** | all | 0.4 | Mark that build is executed in CI environment |
| **GITLAB_CI** | all | all | Mark that build is executed in GitLab CI environment |
| **CI_SERVER** | all | all | Mark that build is executed in CI environment |
| **CI_SERVER_NAME** | all | all | The name of CI server that is used to coordinate builds |
| **CI_SERVER_VERSION** | all | all | GitLab version that is used to schedule builds |
| **CI_SERVER_REVISION** | all | all | GitLab revision that is used to schedule builds |
| **CI_BUILD_ID** | all | all | The unique id of the current build that GitLab CI uses internally |
| **CI_BUILD_REF** | all | all | The commit revision for which project is built |
| **CI_BUILD_TAG** | all | 0.5 | The commit tag name. Present only when building tags. |
| **CI_BUILD_NAME** | all | 0.5 | The name of the build as defined in `.gitlab-ci.yml` |
| **CI_BUILD_STAGE** | all | 0.5 | The name of the stage as defined in `.gitlab-ci.yml` |
| **CI_BUILD_REF_NAME** | all | all | The branch or tag name for which project is built |
| **CI_BUILD_REPO** | all | all | The URL to clone the Git repository |
| **CI_BUILD_TRIGGERED** | all | 0.5 | The flag to indicate that build was [triggered] |
2016-09-05 09:42:59 +00:00
| **CI_BUILD_MANUAL** | 8.12 | all | The flag to indicate that build was manually started |
2016-06-21 11:55:33 +00:00
| **CI_BUILD_TOKEN** | all | 1.2 | Token used for authenticating with the GitLab Container Registry |
2016-07-19 22:08:54 +00:00
| **CI_PIPELINE_ID** | 8.10 | 0.5 | The unique id of the current pipeline that GitLab CI uses internally |
2016-06-21 11:55:33 +00:00
| **CI_PROJECT_ID** | all | all | The unique id of the current project that GitLab CI uses internally |
2016-07-20 11:17:21 +00:00
| **CI_PROJECT_NAME** | 8.10 | 0.5 | The project name that is currently being built |
2016-07-21 18:33:07 +00:00
| **CI_PROJECT_NAMESPACE** | 8.10 | 0.5 | The project namespace (username or groupname) that is currently being built |
2016-07-19 22:08:54 +00:00
| **CI_PROJECT_PATH** | 8.10 | 0.5 | The namespace with project name |
| **CI_PROJECT_URL** | 8.10 | 0.5 | The HTTP address to access project |
2016-07-20 11:17:21 +00:00
| **CI_PROJECT_DIR** | all | all | The full path where the repository is cloned and where the build is run |
| **CI_REGISTRY** | 8.10 | 0.5 | If the Container Registry is enabled it returns the address of GitLab's Container Registry |
2016-09-14 10:11:38 +00:00
| **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 |
2016-07-20 12:00:25 +00:00
| **CI_RUNNER_ID** | 8.10 | 0.5 | The unique id of runner being used |
2016-07-20 11:17:21 +00:00
| **CI_RUNNER_DESCRIPTION** | 8.10 | 0.5 | The description of the runner as saved in GitLab |
2016-07-19 22:08:54 +00:00
| **CI_RUNNER_TAGS** | 8.10 | 0.5 | The defined runner tags |
2016-10-05 16:40:13 +00:00
| **CI_DEBUG_TRACE** | all | 1.7 | Whether [debug tracing ](#debug-tracing ) is enabled |
2016-09-05 09:42:59 +00:00
| **GITLAB_USER_ID** | 8.12 | all | The id of the user who started the build |
| **GITLAB_USER_EMAIL** | 8.12 | all | The email of the user who started the build |
2015-10-12 15:06:53 +00:00
2015-08-26 01:42:46 +00:00
Example values:
```bash
export CI_BUILD_ID="50"
export CI_BUILD_REF="1ecfd275763eff1d6b4844ea3168962458c9f27a"
export CI_BUILD_REF_NAME="master"
2016-06-21 11:55:33 +00:00
export CI_BUILD_REPO="https://gitab-ci-token:abcde-1234ABCD5678ef@gitlab.com/gitlab-org/gitlab-ce.git"
2015-10-12 15:06:53 +00:00
export CI_BUILD_TAG="1.0.0"
export CI_BUILD_NAME="spec:other"
export CI_BUILD_STAGE="test"
2016-09-05 09:42:59 +00:00
export CI_BUILD_MANUAL="true"
2015-10-12 15:06:53 +00:00
export CI_BUILD_TRIGGERED="true"
2016-06-15 04:26:38 +00:00
export CI_BUILD_TOKEN="abcde-1234ABCD5678ef"
2016-06-21 11:55:33 +00:00
export CI_PIPELINE_ID="1000"
2015-08-26 01:42:46 +00:00
export CI_PROJECT_ID="34"
2016-06-21 11:55:33 +00:00
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"
2016-07-20 11:17:21 +00:00
export CI_PROJECT_URL="https://gitlab.com/gitlab-org/gitlab-ce"
2016-06-21 11:55:33 +00:00
export CI_REGISTRY="registry.gitlab.com"
export CI_REGISTRY_IMAGE="registry.gitlab.com/gitlab-org/gitlab-ce"
2016-07-19 22:17:19 +00:00
export CI_RUNNER_ID="10"
export CI_RUNNER_DESCRIPTION="my runner"
export CI_RUNNER_TAGS="docker, linux"
2015-08-26 01:42:46 +00:00
export CI_SERVER="yes"
2016-06-21 11:55:33 +00:00
export CI_SERVER_NAME="GitLab"
2016-09-07 12:56:50 +00:00
export CI_SERVER_REVISION="70606bf"
export CI_SERVER_VERSION="8.9.0"
2016-09-05 09:42:59 +00:00
export GITLAB_USER_ID="42"
export GITLAB_USER_EMAIL="alexzander@sporer.com"
2015-08-26 01:42:46 +00:00
```
2016-11-25 13:41:16 +00:00
## YAML-defined variables
>**Note:**
This feature requires GitLab Runner 0.5.0 or higher and GitLab CI 7.14 or higher.
2015-08-26 01:42:46 +00:00
2016-11-25 13:41:16 +00:00
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:
2015-08-26 01:42:46 +00:00
```yaml
variables:
DATABASE_URL: "postgres://postgres@postgres/my_database"
```
2016-11-25 13:41:16 +00:00
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: []
```
## User-defined variables (secure variables)
2015-08-26 01:42:46 +00:00
2016-11-25 13:41:16 +00:00
>**Notes:**
- This feature requires GitLab Runner 0.4.0 or higher.
- Be aware that secure variables are not masked, and their values can be shown
in the build 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. Follow the discussion in issue [#13784][ce-13784] for masking the
secure variables.
2015-08-26 01:42:46 +00:00
2016-11-25 13:41:16 +00:00
GitLab CI allows you to define per-project **Secure variables** that are set in
the build environment. The secure variables are stored out of the repository
(`.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.
2016-04-16 19:17:05 +00:00
2016-11-25 13:41:16 +00:00
Secure Variables can added by going to your project's
**Settings ➔ Variables ➔ Add variable**.
2015-08-26 01:42:46 +00:00
2016-11-25 13:41:16 +00:00
Once you set them, they will be available for all subsequent builds.
2016-10-05 16:40:13 +00:00
2016-11-25 13:41:16 +00:00
## Debug tracing
> Introduced in GitLab Runner 1.7.
>
2016-10-05 16:40:13 +00:00
> **WARNING:** Enabling debug tracing can have severe security implications. The
output **will** contain the content of all your secure variables and any other
secrets! The output **will** be uploaded to the GitLab server and made visible
in build traces!
By default, GitLab Runner hides most of the details of what it is doing when
processing a job. This behaviour keeps build traces short, and prevents secrets
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
trace, resulting in a verbose build trace listing all commands that were run,
variables that were set, etc.
Before enabling this, you should ensure builds are visible to
[team members only ](../../../user/permissions.md#project-features ). You should
also [erase ](../pipelines.md#seeing-build-traces ) all generated build traces
before making them visible again.
To enable debug traces, set the `CI_DEBUG_TRACE` variable to `true` :
```yaml
2016-11-25 13:41:16 +00:00
job_name:
2016-10-05 16:40:13 +00:00
variables:
CI_DEBUG_TRACE: "true"
```
The [example project ](https://gitlab.com/gitlab-examples/ci-debug-trace )
demonstrates a working configuration, including build trace examples.
2016-11-25 13:41:16 +00:00
## Using the CI variables in your job scripts
2015-08-26 01:42:46 +00:00
2016-11-25 13:41:16 +00:00
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 build script.
2015-08-26 01:42:46 +00:00
2016-11-25 13:41:16 +00:00
To access the variables (predefined and user-defined) in a `bash` /`sh` environment,
prefix the variable name with the dollar sign (`$`):
2016-02-09 00:08:49 +00:00
2015-08-26 01:42:46 +00:00
```
job_name:
script:
- echo $CI_BUILD_ID
```
2016-11-25 13:41:16 +00:00
You can also list all environment variables with the `export` command,
but be aware that this will also expose the values of all the secure variables
you set, in the build log:
2015-08-26 01:42:46 +00:00
```
job_name:
script:
- export
```
2016-03-01 07:57:09 +00:00
2016-11-25 13:41:16 +00:00
[ce-13784]: https://gitlab.com/gitlab-org/gitlab-ce/issues/13784
[gitlab runner]: https://docs.gitlab.com/runner/
2016-03-01 07:57:09 +00:00
[triggered]: ../triggers/README.md