Environment Variables

A way to control jobs for various environments.

An environment variable is a dynamically-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 can be invoked in your pipelines by adding $ in front of the variable name like this:

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. This is 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 them into Base64 and decode them in the container. Just put them in as-is.

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 job logs.
  • Secret — implies the Masked attribute, but also hides the value on the Variables page.
  • Protected: will be available in a future version: see the issue in our roadmap.

Masked

A masked environment variable means that its value will be hidden in job logs.
Snake CI does its best to hide the value in jobs logs, but it’s still possible to accidentally print it from within your code.

Secret

A secret environment variable implies the Masked attribute, so it hides the value in job logs and on the Variables page. No one can see its value after creating the variable.

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

Protected

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

A protected environment variable is available only on a specific set of protected branches such as the master branch.

Built-in environment variables CI_*

Variable Min. Version Description
CI - Always equal to true. Can be used to determine if the process is running in the CI system.
CI_PIPELINE_ID - The unique pipeline ID. Can be used as an identifier of the build.
CI_PIPELINE_DIR - The absolute path of the directory that contains the repository. Equal to the current working directory.
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 the commit that triggered the pipeline.
CI_TAG - The tag of the commit that triggered the pipeline. Available only when using tags.
CI_COMMIT_HASH - The unique hash of a commit that triggered the pipeline.
CI_COMMIT_SHORT_HASH - The short representation of CI_COMMIT_HASH
CI_FROM_COMMIT_HASH 0.8 The unique hash of a previous commit that triggered the pipeline.
CI_FROM_COMMIT_SHORT_HASH 0.8 The short representation of CI_FROM_COMMIT_HASH
CI_PULL_REQUEST_ID - The ID of the pull request that triggered the pipeline (scoped to the repository).
CI_PULL_REQUEST_TITLE 0.8 The title of the pull request.
CI_PULL_REQUEST_STATE 0.8 The state of the pull request.
CI_PULL_REQUEST_TO_REF 0.8 The target ref of the pull request (base branch).
CI_PULL_REQUEST_TO_HASH 0.8 The target ref’s commit of the pull request.
CI_PULL_REQUEST_TO_FORK 0.8 Whether the target repository is a fork. Value: true or false.
CI_PULL_REQUEST_FROM_REF 0.8 The source ref of the pull request (to be merged).
CI_PULL_REQUEST_FROM_HASH 0.8 The source ref’s commit of the pull request.
CI_PULL_REQUEST_FROM_FORK 0.8 Whether the source repository is fork. Value: true or false.
CI_PULL_REQUEST_CROSS_REPO 0.8 Whether the merge is cross-repository. Value: true or false.
CI_PROJECT_KEY - The key of the project which the repository belongs to.
CI_PROJECT_NAME - The name of the project which the repository belongs to.
CI_PROJECT_ID - The unique ID of the project which the 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.
CI_SECRET_ACCESS_TOKEN 0.8.4 The secret access token that can be used for cloning repositories via HTTP(S).
CI_SSH_KNOWN_HOSTS_FILE 1.0.0 The path to the known_hosts file which may be used with the ssh to verify host keys.

Last modified October 21, 2020