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.

They are part of the environment in which a process runs. For example, a running process can query the value of the USER environment variable to get a name of the current user, 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 the following:

echo $DOCKER_REGISTRY

where DOCKER_REGISTRY is the name of the variable to print.

Project & Repository environment variables

Repository environment variables inherit Project environment variables.

The Variables settings page can be accessed by the following path:

  • ProjectProject SettingsSNAKE CIVariables
  • RepositoryRepository SettingsSNAKE CIVariables

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

screenshot

Variables may 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 & Job 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"

Environment Variable Attributes

An environment variable can have several attributes:

  • Masked — the value will be hidden in jobs logs.
  • Secret — implies the Masked attribute, but also hides the value on the Variables page.
  • Protected: will be available in the next versions: see the issue in our roadmap

Masked

A masked environment variable means that its value will be hidden in jobs logs.
Snake CI makes its best to hide the value in jobs logs, but it’s highly recommended to avoid printing the value in jobs logs.

Secret

A secret environment variable implies the Masked attribute, so it hides the value in jobs logs but it also hides the value on the Variables page.

A user can change its value by specifying a new value in the Edit Variable dialog.

Protected

A protected environment variables are not yet implemented: see the issue in our roadmap

A protected environment variables are available only on specific set of protected branches such as the master branch.

Built-in environment variables CI_*

Variable Description
CI Always equal to true. Can be used to determine if the process is running in the CI system.
CI_PIPELINE_ID An unique pipeline ID, can be used as an identifier of the build.
CI_JOB_ID An 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. Equal to the 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 running.


Last modified July 27, 2020