2017-11-01 11:56:40 -04:00
# GitLab CI/CD Variables
2016-04-17 14:08:02 -04:00
2017-02-13 11:59:57 -05:00
When receiving a job from GitLab CI, the [Runner] prepares the build environment.
2016-12-05 16:46:13 -05:00
It starts by setting a list of **predefined variables** (environment variables)
2016-11-25 08:41:16 -05:00
and a list of **user-defined variables** .
2015-08-25 21:42:46 -04:00
2016-12-05 16:46:13 -05:00
## Priority of variables
2016-11-25 08:41:16 -05:00
2016-12-05 16:46:13 -05:00
The variables can be overwritten and they take precedence over each other in
this order:
2017-07-01 06:23:41 -04:00
1. [Trigger variables][triggers] or [scheduled pipeline variables ](../../user/project/pipelines/schedules.md#making-use-of-scheduled-pipeline-variables ) (take precedence over all)
2018-05-31 12:41:37 -04:00
1. Project-level [variables ](#variables ) or [protected variables ](#protected-variables )
1. Group-level [variables ](#variables ) or [protected variables ](#protected-variables )
2018-03-07 05:29:20 -05:00
1. YAML-defined [job-level variables ](../yaml/README.md#variables )
2016-12-05 16:46:13 -05:00
1. YAML-defined [global variables ](../yaml/README.md#variables )
2016-12-16 07:24:03 -05:00
1. [Deployment variables ](#deployment-variables )
2016-12-05 16:46:13 -05:00
1. [Predefined variables ](#predefined-variables-environment-variables ) (are the
lowest in the chain)
2015-08-25 21:42:46 -04:00
2018-05-31 12:41:37 -04:00
For example, if you define `API_TOKEN=secure` as a project variable and
2016-12-05 16:46:13 -05:00
`API_TOKEN=yaml` in your `.gitlab-ci.yml` , the `API_TOKEN` will take the value
2018-05-31 12:41:37 -04:00
`secure` as the project variables are higher in the chain.
2015-08-25 21:42:46 -04:00
2018-05-29 13:33:48 -04:00
## 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 ).
2016-11-25 08:41:16 -05:00
## Predefined variables (Environment variables)
2015-08-25 21:42:46 -04:00
2016-12-05 16:46:13 -05:00
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.
2015-08-25 21:42:46 -04:00
2017-03-09 04:19:37 -05:00
>**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 |
|-------------------------------- |--------|--------|-------------|
2018-05-29 13:33:48 -04:00
| **ARTIFACT_DOWNLOAD_ATTEMPTS** | 8.15 | 1.9 | Number of attempts to download artifacts running a job |
2017-03-09 04:19:37 -05:00
| **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 |
2017-07-05 12:21:19 -04:00
| **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 `-` . No leading / trailing `-` . Use in URLs, host names and domain names. |
2017-03-09 04:19:37 -05:00
| **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. |
2018-05-07 11:25:28 -04:00
| **CI_COMMIT_MESSAGE** | 10.8 | all | The full commit message. |
| **CI_COMMIT_TITLE** | 10.8 | all | The title of the commit - the full first line of the message |
| **CI_COMMIT_DESCRIPTION** | 10.8 | all | The description of the commit: the message without first line, if the title is shorter than 100 characters; full message in other case. |
2017-07-03 14:00:50 -04:00
| **CI_CONFIG_PATH** | 9.4 | 0.5 | The path to CI config file. Defaults to `.gitlab-ci.yml` |
2017-03-09 04:19:37 -05:00
| **CI_DEBUG_TRACE** | all | 1.7 | Whether [debug tracing ](#debug-tracing ) is enabled |
2018-05-29 13:33:48 -04:00
| **CI_DEPLOY_USER** | 10.8 | all | Authentication username of the [GitLab Deploy Token][gitlab-deploy-token], only present if the Project has one related.|
| **CI_DEPLOY_PASSWORD** | 10.8 | all | Authentication password of the [GitLab Deploy Token][gitlab-deploy-token], only present if the Project has one related.|
2017-10-21 14:05:32 -04:00
| **CI_DISPOSABLE_ENVIRONMENT** | all | 10.1 | Marks that the job is executed in a disposable environment (something that is created only for this job and disposed of/destroyed after the execution - all executors except `shell` and `ssh` ). If the environment is disposable, it is set to true, otherwise it is not defined at all. |
2017-03-09 04:19:37 -05:00
| **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. |
2017-05-25 10:58:51 -04:00
| **CI_ENVIRONMENT_URL** | 9.3 | all | The URL of the environment for this job |
2017-03-09 04:19:37 -05:00
| **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 |
2018-05-20 08:18:50 -04:00
| **CI_JOB_URL** | 11.0 | 0.5 | Job details URL |
2017-03-09 04:19:37 -05:00
| **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 |
2018-01-31 13:05:35 -05:00
| **CI_RUNNER_VERSION** | all | 10.6 | GitLab Runner version that is executing the current job |
| **CI_RUNNER_REVISION** | all | 10.6 | GitLab Runner revision that is executing the current job |
| **CI_RUNNER_EXECUTABLE_ARCH** | all | 10.6 | The OS/architecture of the GitLab Runner executable (note that this is not necessarily the same as the environment of the executor) |
2017-03-09 04:19:37 -05:00
| **CI_PIPELINE_ID** | 8.10 | 0.5 | The unique id of the current pipeline that GitLab CI uses internally |
2018-05-08 03:01:18 -04:00
| **CI_PIPELINE_IID** | 11.0 | all | The unique id of the current pipeline scoped to project |
2017-03-09 04:19:37 -05:00
| **CI_PIPELINE_TRIGGERED** | all | all | The flag to indicate that job was [triggered] |
2018-03-30 10:45:57 -04:00
| **CI_PIPELINE_SOURCE** | 10.0 | all | Indicates how the pipeline was triggered. Possible options are: `push` , `web` , `trigger` , `schedule` , `api` , and `pipeline` . For pipelines created before GitLab 9.5, this will show as `unknown` |
2017-03-09 04:19:37 -05:00
| **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 |
2017-06-01 01:04:16 -04:00
| **CI_PROJECT_NAME** | 8.10 | 0.5 | The project name that is currently being built (actually it is project folder name) |
2017-03-09 04:19:37 -05:00
| **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 |
2017-06-01 01:04:16 -04:00
| **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. |
2018-05-20 08:18:50 -04:00
| **CI_PIPELINE_URL** | 11.0 | 0.5 | Pipeline details URL |
2017-03-09 04:19:37 -05:00
| **CI_PROJECT_URL** | 8.10 | 0.5 | The HTTP address to access project |
2017-11-03 21:42:16 -04:00
| **CI_PROJECT_VISIBILITY** | 10.3 | all | The project visibility (internal, private, public) |
2017-03-09 04:19:37 -05:00
| **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 |
2017-10-21 14:05:32 -04:00
| **CI_SHARED_ENVIRONMENT** | all | 10.1 | Marks that the job is executed in a shared environment (something that is persisted across CI invocations like `shell` or `ssh` executor). If the environment is shared, it is set to true, otherwise it is not defined at all. |
2017-03-09 04:19:37 -05:00
| **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_EMAIL** | 8.12 | all | The email of the user who started the job |
2018-05-29 13:33:48 -04:00
| **GITLAB_USER_ID** | 8.12 | all | The id of the user who started the job |
2017-08-25 00:01:55 -04:00
| **GITLAB_USER_LOGIN** | 10.0 | all | The login username of the user who started the job |
2017-08-25 00:01:01 -04:00
| **GITLAB_USER_NAME** | 10.0 | all | The real name of the user who started the job |
2017-03-09 04:19:37 -05:00
| **RESTORE_CACHE_ATTEMPTS** | 8.15 | 1.9 | Number of attempts to restore the cache running a job |
2015-08-25 21:42:46 -04:00
2017-03-07 04:06:53 -05:00
## 9.0 Renaming
2018-01-19 05:09:59 -05:00
To follow conventions of naming across GitLab, and to further move away from the
2017-03-07 04:06:53 -05:00
`build` term and toward `job` CI variables have been renamed for the 9.0
release.
2017-08-24 10:40:38 -04:00
>**Note:**
Starting with GitLab 9.0, we have deprecated the `$CI_BUILD_*` variables. **You are
strongly advised to use the new variables as we will remove the old ones in
future GitLab releases.**
2017-03-09 04:19:37 -05:00
| 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` |
2017-03-09 07:06:11 -05:00
| `CI_BUILD_REPO` | `CI_REPOSITORY_URL` |
2017-03-09 04:19:37 -05:00
| `CI_BUILD_TRIGGERED` | `CI_PIPELINE_TRIGGERED` |
| `CI_BUILD_MANUAL` | `CI_JOB_MANUAL` |
| `CI_BUILD_TOKEN` | `CI_JOB_TOKEN` |
2017-03-07 04:06:53 -05:00
2018-01-24 07:03:12 -05:00
## `.gitlab-ci.yml` defined variables
2016-11-25 08:41:16 -05:00
>**Note:**
This feature requires GitLab Runner 0.5.0 or higher and GitLab CI 7.14 or higher.
2015-08-25 21:42:46 -04:00
2016-11-25 08:41:16 -05: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-25 21:42:46 -04:00
```yaml
variables:
DATABASE_URL: "postgres://postgres@postgres/my_database"
```
2016-11-25 08:41:16 -05: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
2017-06-06 03:55:24 -04:00
global defined variables in your job, define an empty hash:
2016-11-25 08:41:16 -05:00
```yaml
job_name:
2017-06-04 02:14:42 -04:00
variables: {}
2016-11-25 08:41:16 -05:00
```
2017-02-28 18:49:28 -05:00
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'
```
2018-05-31 12:41:37 -04:00
## Variables
2015-08-25 21:42:46 -04:00
2017-10-03 09:11:49 -04:00
NOTE: **Note:**
2018-05-31 12:41:37 -04:00
Group-level variables were added in GitLab 9.4.
2017-10-03 09:11:49 -04:00
CAUTION: **Important:**
2018-05-31 12:41:37 -04:00
Be aware that variables are not masked, and their values can be shown
2017-10-03 09:11:49 -04:00
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).
2018-05-31 12:41:37 -04:00
Follow the discussion in issue [#13784][ce-13784] for masking the variables.
2015-08-25 21:42:46 -04:00
2018-05-31 12:41:37 -04:00
GitLab CI allows you to define per-project or per-group variables
that are set in the pipeline environment. The variables are stored out of
2017-09-14 11:59:35 -04:00
the repository (not in `.gitlab-ci.yml` ) and are securely passed to GitLab Runner
making them available during a pipeline run. It's the recommended method to
use for storing things like passwords, SSH keys and credentials.
2016-04-16 15:17:05 -04:00
2018-05-31 12:41:37 -04:00
Project-level variables can be added by going to your project's
**Settings > CI/CD**, then finding the section called **Variables** .
2015-08-25 21:42:46 -04:00
2018-05-31 12:41:37 -04:00
Likewise, group-level variables can be added by going to your group's
**Settings > CI/CD**, then finding the section called **Variables** .
2017-07-03 10:43:06 -04:00
Any variables of [subgroups] will be inherited recursively.
2015-08-25 21:42:46 -04:00
2018-05-31 12:41:37 -04:00
![Variables ](img/secret_variables.png )
2017-10-03 09:11:49 -04:00
2017-05-03 14:51:55 -04:00
Once you set them, they will be available for all subsequent pipelines. You can also
2018-05-31 12:41:37 -04:00
[protect your variables ](#protected-variables ).
2017-05-25 08:29:12 -04:00
2018-05-31 12:41:37 -04:00
### Protected variables
2017-05-25 08:29:12 -04:00
>**Notes:**
2017-06-01 09:22:38 -04:00
This feature requires GitLab 9.3 or higher.
2017-05-25 08:29:12 -04:00
2018-05-31 12:41:37 -04:00
Variables could be protected. Whenever a variable is
2017-05-25 08:29:12 -04:00
protected, it would only be securely passed to pipelines running on the
2017-05-31 10:45:51 -04:00
[protected branches] or [protected tags]. The other pipelines would not get any
2017-05-25 08:29:12 -04:00
protected variables.
Protected variables can be added by going to your project's
2017-09-14 11:59:35 -04:00
**Settings > CI/CD**, then finding the section called
2018-05-31 12:41:37 -04:00
**Variables**, and check "Protected".
2017-05-25 08:29:12 -04:00
Once you set them, they will be available for all subsequent pipelines.
2016-10-05 12:40:13 -04:00
2016-12-16 07:24:03 -05:00
## Deployment variables
>**Note:**
This feature requires GitLab CI 8.15 or higher.
2017-02-03 13:41:35 -05:00
[Project services ](../../user/project/integrations/project_services.md ) that are
2016-12-16 07:24:03 -05:00
responsible for deployment configuration may define their own variables that
are set in the build environment. These variables are only defined for
2017-02-13 11:59:57 -05:00
[deployment jobs ](../environments.md ). Please consult the documentation of
2016-12-16 07:24:03 -05:00
the project services that you are using to learn which variables they define.
2018-05-09 03:04:06 -04:00
An example project service that defines deployment variables is the
[Kubernetes integration ](../../user/project/clusters/index.md#deployment-variables ).
2016-12-16 07:24:03 -05:00
2016-11-25 08:41:16 -05:00
## Debug tracing
> Introduced in GitLab Runner 1.7.
2017-12-07 09:29:40 -05:00
CAUTION: **Warning:**
Enabling debug tracing can have severe security implications. The
2018-05-31 12:41:37 -04:00
output **will** contain the content of all your variables and any other
2017-12-07 09:29:40 -05:00
secrets! The output **will** be uploaded to the GitLab server and made visible
in job traces!
2016-10-05 12:40:13 -04:00
By default, GitLab Runner hides most of the details of what it is doing when
2017-12-07 09:29:40 -05:00
processing a job. This behavior keeps job traces short, and prevents secrets
2016-10-05 12:40:13 -04:00
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
2017-02-13 11:59:57 -05:00
trace, resulting in a verbose job trace listing all commands that were run,
2016-10-05 12:40:13 -04:00
variables that were set, etc.
2017-02-13 11:59:57 -05:00
Before enabling this, you should ensure jobs are visible to
2016-12-05 16:46:13 -05:00
[team members only ](../../user/permissions.md#project-features ). You should
2017-02-13 11:59:57 -05:00
also [erase ](../pipelines.md#seeing-build-status ) all generated job traces
2016-10-05 12:40:13 -04:00
before making them visible again.
To enable debug traces, set the `CI_DEBUG_TRACE` variable to `true` :
```yaml
2016-11-25 08:41:16 -05:00
job_name:
2016-10-05 12:40:13 -04:00
variables:
CI_DEBUG_TRACE: "true"
```
2016-12-05 16:46:13 -05:00
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
2017-03-16 19:27:42 -04:00
++ export CI_COMMIT_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ CI_COMMIT_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
2017-03-10 05:20:08 -05:00
++ 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
2016-12-05 16:46:13 -05:00
++ 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
2017-03-10 05:20:08 -05:00
++ 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
2016-12-05 16:46:13 -05:00
++ 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
2018-06-03 23:12:02 -04:00
++ export CI_PIPELINE_IID=123
++ CI_PIPELINE_IID=123
2016-12-05 16:46:13 -05:00
++ 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
2017-02-24 07:18:07 -05:00
++ export 'CI_RUNNER_TAGS=shared, docker, linux, ruby, mysql, postgres, mongo'
++ CI_RUNNER_TAGS='shared, docker, linux, ruby, mysql, postgres, mongo'
2016-12-05 16:46:13 -05:00
++ 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
2017-04-07 14:51:38 -04:00
++ GITLAB_USER_EMAIL=user@example.com
2016-12-05 16:46:13 -05:00
++ export VERY_SECURE_VARIABLE=imaverysecurevariable
++ VERY_SECURE_VARIABLE=imaverysecurevariable
++ mkdir -p /builds/gitlab-examples/ci-debug-trace.tmp
++ echo -n '-----BEGIN CERTIFICATE-----
MIIFQzCCBCugAwIBAgIRAL/ElDjuf15xwja1ZnCocWAwDQYJKoZIhvcNAQELBQAw'
...
```
2016-10-05 12:40:13 -04:00
2016-11-25 08:41:16 -05:00
## Using the CI variables in your job scripts
2015-08-25 21:42:46 -04:00
2016-11-25 08:41:16 -05: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.
2017-02-13 11:59:57 -05:00
In most cases `bash` or `sh` is used to execute the job script.
2015-08-25 21:42:46 -04:00
2017-04-14 19:01:52 -04:00
To access environment variables, use the syntax for your Runner's [shell][shellexecutors].
2016-02-08 19:08:49 -05:00
2017-04-14 19:01:52 -04:00
| Shell | Usage |
|----------------------|-----------------|
| bash/sh | `$variable` |
| windows batch | `%variable%` |
| PowerShell | `$env:variable` |
To access environment variables in bash, prefix the variable name with (`$`):
```yaml
2015-08-25 21:42:46 -04:00
job_name:
script:
2017-04-27 10:12:29 -04:00
- echo $CI_JOB_ID
2015-08-25 21:42:46 -04:00
```
2017-04-14 19:01:52 -04:00
To access environment variables in **Windows Batch** , surround the variable
with (`%`):
```yaml
job_name:
script:
- echo %CI_JOB_ID%
```
To access environment variables in a **Windows PowerShell** environment, prefix
the variable name with (`$env:`):
```yaml
job_name:
script:
- echo $env:CI_JOB_ID
```
2016-11-25 08:41:16 -05:00
You can also list all environment variables with the `export` command,
2018-05-31 12:41:37 -04:00
but be aware that this will also expose the values of all the variables
2017-02-13 11:59:57 -05:00
you set, in the job log:
2016-11-25 08:41:16 -05:00
2017-04-14 19:01:52 -04:00
```yaml
2015-08-25 21:42:46 -04:00
job_name:
script:
- export
```
2016-03-01 02:57:09 -05:00
2017-03-09 07:06:11 -05:00
Example values:
```bash
export CI_JOB_ID="50"
export CI_COMMIT_SHA="1ecfd275763eff1d6b4844ea3168962458c9f27a"
export CI_COMMIT_REF_NAME="master"
2017-04-03 08:19:11 -04:00
export CI_REPOSITORY_URL="https://gitlab-ci-token:abcde-1234ABCD5678ef@example.com/gitlab-org/gitlab-ce.git"
2017-03-09 07:06:11 -05:00
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"
2018-06-03 23:12:02 -04:00
export CI_PIPELINE_IID="10"
2017-03-09 07:06:11 -05:00
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"
```
2018-03-28 08:00:53 -04:00
## Variables expressions
> Variables expressions were added in GitLab 10.7.
It is possible to use variables expressions with only / except policies in
2018-04-05 05:30:02 -04:00
`.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.
2018-03-28 08:00:53 -04:00
2018-05-31 12:41:37 -04:00
This is particularly useful in combination with variables and triggered
2018-03-28 08:00:53 -04:00
pipeline variables.
```yaml
deploy:
script: cap staging deploy
environment: staging
only:
variables:
- $RELEASE == "staging"
- $STAGING
```
2018-04-05 05:30:02 -04:00
Each expression provided is going to be evaluated before creating a pipeline.
2018-03-28 08:00:53 -04:00
If any of the conditions in `variables` evaluates to truth when using `only` ,
2018-03-29 05:25:49 -04:00
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.
2018-03-28 08:00:53 -04:00
2018-04-05 05:30:02 -04:00
This follows usual rules for [`only` / `except` policies][builds-policies].
2018-03-28 08:00:53 -04:00
### Supported syntax
2018-04-05 05:30:02 -04:00
Below you can find supported syntax reference:
2018-03-28 08:00:53 -04:00
2018-03-29 05:25:49 -04:00
1. Equality matching using a string
2018-03-28 08:00:53 -04:00
2018-04-05 05:30:02 -04:00
> Example: `$VARIABLE == "some value"`
2018-03-28 08:00:53 -04:00
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.
2018-03-29 05:25:49 -04:00
1. Checking for an undefined value
2018-03-28 08:00:53 -04:00
2018-04-05 05:30:02 -04:00
> 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
2018-03-28 08:00:53 -04:00
`$VARIABLE == null` . This expression is going to evaluate to truth if
2018-04-05 05:30:02 -04:00
variable is not defined.
2018-03-28 08:00:53 -04:00
2018-03-29 05:25:49 -04:00
1. Checking for an empty variable
2018-04-05 05:30:02 -04:00
> Example: `$VARIABLE == ""`
2018-03-29 05:25:49 -04:00
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
2018-03-28 08:00:53 -04:00
2018-04-05 05:30:02 -04:00
> Example: `$VARIABLE_1 == $VARIABLE_2`
It is possible to compare two variables. This is going to compare values
of these variables.
2018-03-28 08:00:53 -04:00
2018-03-29 05:25:49 -04:00
1. Variable presence check
2018-03-28 08:00:53 -04:00
2018-04-05 05:30:02 -04:00
> Example: `$STAGING`
2018-03-28 08:00:53 -04:00
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.
2018-04-05 05:30:02 -04:00
`$STAGING` value needs to a string, with length higher than zero.
Variable that contains only whitespace characters is not an empty variable.
2018-05-15 08:41:20 -04:00
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.
2018-05-17 06:47:41 -04:00
Pattern matching is case-sensitive by default. Use `i` flag modifier, like
`/pattern/i` to make a pattern case-insensitive.
2018-05-15 09:18:18 -04:00
2018-05-31 12:41:37 -04:00
[ce-13784]: https://gitlab.com/gitlab-org/gitlab-ce/issues/13784 "Simple protection of CI variables"
2018-02-07 05:48:29 -05:00
[eep]: https://about.gitlab.com/products/ "Available only in GitLab Premium"
2017-07-06 03:45:38 -04:00
[envs]: ../environments.md
2017-05-31 10:45:51 -04:00
[protected branches]: ../../user/project/protected_branches.md
[protected tags]: ../../user/project/protected_tags.md
2017-07-06 03:45:38 -04:00
[runner]: https://docs.gitlab.com/runner/
2017-04-14 19:01:52 -04:00
[shellexecutors]: https://docs.gitlab.com/runner/executors/
2017-07-06 03:45:38 -04:00
[triggered]: ../triggers/README.md
[triggers]: ../triggers/README.md#pass-job-variables-to-a-trigger
2017-07-03 10:43:06 -04:00
[subgroups]: ../../user/group/subgroups/index.md
2018-04-05 05:30:02 -04:00
[builds-policies]: ../yaml/README.md#only-and-except-complex
2018-04-18 13:25:57 -04:00
[gitlab-deploy-token]: ../../user/project/deploy_tokens/index.md#gitlab-deploy-token