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:
Flexibility - Easily adapt your pipelines to different environments, configurations, or deployment targets without modifying your core CI/CD script.
Security - Securely store sensitive information like API keys, passwords, and tokens, preventing them from being exposed directly in your code.
Maintainability - Keep your CI/CD configuration clean and organized by centralizing values in variables, making updates and modifications easier.
Reusability - Define variables once and reuse them across multiple projects, promoting consistency and reducing duplication.
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:
Click on the Settings > CI/CD buttons for your project, group, or instance.
Go to the Variables section.
Click Add variable.
Enter the key (e.g., API_KEY
) and value.
Optionally, check the Protect variable box for sensitive information. This ensures that the variable is only available to pipelines running on protected branches or tags.
Optionally, check the Mask variable box to hide the variable's value from job logs, preventing accidental exposure.
Click Save variable.
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).
Securely manage sensitive variables: Use protected and masked variables for API keys, passwords, and other sensitive information.
Avoid hardcoding values: Use variables to store configuration values, making your pipelines more flexible and maintainable.
Organize your variables: Use descriptive names and group related variables together for better organization.
Use the appropriate scope: Choose the correct scope (project, group, or instance) for your variables based on their intended use and visibility.
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 trial of GitLab Ultimate with Duo Enterprise.
Read more articles in our "Getting Started with GitLab" series: