Published on: May 27, 2025
5 min read
Learn what CI/CD variables are, why they are important in DevSecOps, and best practices for utilizing them.
Welcome to our "Getting started with GitLab" series, where we help newcomers get familiar with the GitLab DevSecOps platform.
In an earlier article, we explored GitLab CI/CD. Now, let's dive deeper into the world of CI/CD variables and unlock their full potential.
CI/CD variables are dynamic key-value pairs that you can define at different levels within your GitLab environment (e.g., project, group, or instance). These variables act as placeholders for values that you can use in your .gitlab-ci.yml
file to customize your pipelines, securely store sensitive information, and make your CI/CD configuration more maintainable.
CI/CD variables offer numerous benefits:
GitLab allows you to define CI/CD variables with different scopes, controlling their visibility and accessibility:
Project-level variables - These variables are specific to a single project and are ideal for storing project-specific settings, such as:
DPROD_DEPLOY_URL
with the value https://mywebapp.com
.Group-level variables - These variables are shared across all projects within a GitLab group. They are useful for settings that are common to multiple projects, such as:
DOCKER_HUB_API_KEY
with the corresponding API key value.Instance-level variables - These variables are available to all projects on a GitLab instance. They are typically used for global settings that apply across an entire organization such as:
DEFAULT_DOCKER_IMAGE
with the value ubuntu:latest
.To define a CI/CD variable:
API_KEY
) and value.To use a CI/CD variable in your .gitlab-ci.yml
file, simply prefix the variable name with $
:
deploy_job:
script:
- echo "Deploying to production..."
- curl -H "Authorization: Bearer $API_KEY" https://api.example.com/deploy
GitLab provides a set of predefined CI/CD variables that you can use in your pipelines. These variables provide information about the current pipeline, job, project, and more.
Some commonly used predefined variables include:
$CI_COMMIT_SHA
: The commit SHA of the current pipeline.$CI_PROJECT_DIR
: The directory where the project is cloned.$CI_PIPELINE_ID
: The ID of the current pipeline.$CI_ENVIRONMENT_NAME
: The name of the environment being deployed to (if applicable).CI/CD variables are a powerful tool for customizing and securing your GitLab pipelines. By mastering variables and understanding their different scopes, you can create more flexible, maintainable, and efficient workflows.
We hope you found it helpful and are now well-equipped to leverage the power of GitLab for your development projects.
Get started with CI/CD variables today with a free, 60-day trial of GitLab Ultimate with Duo Enterprise.
Read more articles in our "Getting Started with GitLab" series: