Environment Variables

A way to control jobs for various environments.

An environment variable is a dynamic-named value that can affect how running processes behave on a host.

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 the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.

The built-in and user-defined variables are passed to the build containers as environment variables. They invoked in your pipelines by adding $ in front of the variable name like as following:

echo $DOCKER_REGISTRY

where DOCKER_REGISTRY is the name of the variable to print.

Built-in environment variables

Variable Description
CI_PIPELINE_ID The unique pipeline ID, can be used as an identifier of the build.
CI_JOB_ID The unique job ID.
CI_JOB_STAGE The stage of the job as defined in the pipeline config.
CI_JOB_NAME The name of the job as defined in the pipeline config.
CI_BRANCH The branch of a commit that triggered the pipeline.
CI_TAG The tag of a commit that triggered the pipeline. Available only on tags.
CI_COMMIT_HASH The unique commit has of a commit that triggered the pipeline.
CI_COMMIT_SHORT_HASH The short representation of CI_COMMIT_HASH
CI_PIPELINE_DIR The absolute path of the directory that contains the repository. It is also set to current working directory.
CI_PULL_REQUEST_ID The ID of the Pull Request triggered the pipeline.
CI_PROJECT_KEY The key of the project which repository belongs to.
CI_PROJECT_NAME The name of the project which repository belongs to.
CI_PROJECT_ID The unique ID of the project which repository belongs to.
CI_REPO_SLUG The repository slug (can be useful for URLs)
CI_REPO_NAME The repository name.
CI_REPO_ID The unique ID of the repository.
CI_REPO_CLONE_URL_SSH The URL used to clone the repository.
CI_RUNNER_ID The unique ID of the runner where the pipeline is running.
CI_RUNNER_NAME The name of the runner where the pipeline is running
CI_RUNNER_VERSION The version of the runner where the pipeline is runner

Repository environment variables

Environment Variables settings can be accessed by the following path: RepositoryRepository SettingsCI/CDVariables

You can add, delete or modify environment variables on a repository level, it can is useful for teams because only a user with admin privileges can access the Repository Settings page.

screenshot

Variables can contain multi-line breaks, you don’t need to encode it into Base64 and decode it in the container. Just put it as is, just like that.

Pipeline environment variables (YAML)

You can also specify variables using the variables section in the pipeline. There are two levels:

  • pipeline variables — available for all jobs
  • job variables — available only for the specified job

Example

image: alpine

variables:
    PIPELINE_WIDE: "available for all jobs"

stages:
    - test
    - build

run tests:
    stage: test
    variables:
        TEST: "available only for run tests"
    commands:
        - "echo PIPELINE_WIDE: $PIPELINE_WIDE"
        - "echo TEST: $TEST"

compile code:
    stage: build
    variables:
        BUILD_FOO: "foo; only for build"
    commands:
        - "echo PIPELINE_WIDE: $PIPELINE_WIDE"
        - "echo TEST: $TEST" # will be empty
        - "echo BUILD_FOO: $BUILD_FOO"