GitLab CI/CD comes with numerous built-in variables that are automatically available in your pipeline jobs, eliminating the need to define them yourself. These predefined variables provide essential context about your project, commits, branches, and pipeline execution.
Commonly Used Predefined Variables
Here are some of the most useful built-in GitLab pipeline variables:
- CI_COMMIT_SHA: The full SHA hash of the commit that triggered the pipeline[3][5]
- CI_COMMIT_SHORT_SHA: Shortened version of the commit SHA, useful for tagging[3]
- CI_COMMIT_REF_SLUG: A "slugified" version of the branch or tag name that triggered the pipeline[3]
- CI_PROJECT_NAME: The name of your GitLab project[3]
- CI_PIPELINE_ID: The unique identifier for the current pipeline run[3]
- CI_JOB_STAGE: The current stage that the job is running in[3]
- CI_REGISTRY_IMAGE: The address of the project's Container Registry[5]
Example Usage
These variables are particularly useful for dynamic configuration in your pipelines. For example, you can use them to tag Docker images with meaningful information:
build:
image: docker:latest
services:
- docker:dind
variables:
IMAGE_TAG: $CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA
script:
- docker build -t my-image:$IMAGE_TAG .
- docker push my-image:$IMAGE_TAG
This automatically creates a unique tag using the branch name and short commit SHA without requiring any additional scripting[3].
Variable Scope and Precedence
GitLab's predefined variables are always set first in the precedence order, meaning they serve as defaults that can be overridden by other variable definitions if needed[5]. They're available within the job's environment and can be used in:
- Job scripts
- Before and after scripts
- As parameters in CI/CD configuration
- For interpolation in other fields of your
.gitlab-ci.yml file[5]
These built-in variables help make your pipelines more dynamic and informative, allowing you to track which code version is running where and create more reliable deployments without hardcoded values.