Day 83 โ GitLab Variables & Secret


In CI/CD pipelines, variables are essential for passing dynamic values like environment names, credentials, and configurations. GitLab provides both regular variables and secret (protected/masked) variables to handle sensitive data securely. Today, I explored how GitLab manages variables and how to set secret variables for projects and groups.
๐ Types of GitLab Variables
Project Variables โ specific to a single project.
Group Variables โ inherited by all projects under a group.
Pipeline/Job Variables โ defined directly inside the
.gitlab-ci.yml
file.Secret Variables โ masked/protected variables (like API keys, passwords).
โ๏ธ Setting Variables in GitLab
Navigate to your project โ Settings โ CI/CD โ Variables.
Click Add Variable.
Define:
Key (e.g.,
DB_USER
)Value (e.g.,
admin
)Scope (environment-specific if needed)
๐ Secret Variables
GitLab allows you to mask and protect variables to ensure sensitive data is never exposed in logs.
Examples:
AWS Keys
Database Passwords
Docker Registry Tokens
๐ Example: Storing a database password
variables:
DB_HOST: "db.prod.internal"
DB_USER: "admin"
DB_PASS: "${DB_PASS}"
Here, DB_PASS
is stored as a masked secret variable in GitLab โ Settings โ CI/CD.
โ Best Practices
Never hardcode secrets in
.gitlab-ci.yml
.Use protected variables for production-only pipelines.
Limit variable scopes to environments (e.g., staging, prod).
Rotate secrets regularly.
Conclusion:
Managing variables properly in GitLab CI/CD keeps your pipelines flexible while ensuring security. Tomorrow, Iโll dive into GitLab CI/CD environments & deployments ๐.
Subscribe to my newsletter
Read articles from Shaharyar Shakir directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
