Understanding Variables in GitLab CI/CD

 In this blog, we will explore variables in GitLab CI/CD. Variables help manage configurations, credentials, and settings dynamically within your GitLab pipelines. There are three types of variables in GitLab:

  1. Built-in Variables
  2. User-defined Variables
  3. Secret Variables

Let’s go through each type step by step.

1. Built-in Variables

GitLab provides pre-defined variables that you can use anywhere in your pipeline. These variables store essential information related to your project, commit, and pipeline execution. Some commonly used built-in variables are:

  • $CI_PROJECT_NAME → Displays the project name.
  • $CI_COMMIT_AUTHOR → Shows the author of the commit.
  • $CI_RUNNER_ID → Identifies the runner executing the pipeline.

For example, using $CI_PROJECT_NAME in a script will print the project’s name dynamically.

2. User-defined Variables

User-defined variables allow you to store custom values and reuse them in the pipeline. You can define these variables in your .gitlab-ci.yml file.

Example:

variables:
DEPLOY_ENV: "production"
GITLAB_USER_KEY: "this-is-my-secret-key"

These variables can be accessed using $DEPLOY_ENV or $GITLAB_USER_KEY within your scripts.

3. Secret Variables

Secret variables store sensitive information such as API keys, passwords, and tokens. Unlike user-defined variables, secret variables are securely stored and can be masked to prevent exposure in logs.

Steps to Add Secret Variables in GitLab:

  1. Go to Settings → CI/CD.

2. Expand the Variables section.

3. Click Add Variable and enter the following details:

  • Key: DOCKERHUB_USER
  • Value: brillertechnologies (your username)
  • Type: Variable
  • Environment: All (default)
  • Visibility: Masked (to hide the value in logs)
  • Flags: Protected
  1. Similarly, add another variable:
  • Key: DOCKERHUB_PASS
  • Value: Paste your personal access token (PAT)

These secret variables are now securely stored and can be used in the pipeline without exposing sensitive details.

Understanding Artifacts in GitLab

Artifacts store files generated during a pipeline run, such as test reports and logs. These files can be downloaded and shared with team members.

To enable artifacts:

  1. Go to Settings → CI/CD.
  2. Click on Artifacts and enable it if disabled.

GitLab CI/CD Pipeline Example

Below is a sample .gitlab-ci.yml file using built-in, user-defined, and secret variables.

stages:
- build
- test
- push
- deploy
variables:
DEPLOY_ENV: "production"
GITLAB_USER_KEY: "this-is-my-secret-key"
build_job:
stage: build
script:
- echo "Building $CI_PROJECT_NAME the application"
test_job:
stage: test
script:
- echo "Testing in general build done by $CI_COMMIT_AUTHOR"
- mkdir -p logs
- echo "These are my test results" >> logs/app.log
artifacts:
paths:
- logs
expire_in: 1 week
dev_test_job:
stage: test
script:
- echo "Testing in development using $GITLAB_USER_KEY"
prd_test_job:
stage: test
script:
- echo "Testing in production using $GITLAB_USER_KEY"
push_job:
stage: push
script:
- echo "Pushing to Docker Hub for $DEPLOY_ENV"
deploy_job:
stage: deploy
script:
- echo "Deploying to EC2 for $DOCKERHUB_USER"
- echo "This log is from $CI_JOB_STAGE" >> logs/app.log
artifacts:
paths:
- logs
expire_in: 1 week

After running this pipeline, go to Artifacts in the GitLab UI to download the logs and reports.

Final Thoughts

Variables in GitLab help manage configurations, improve security, and streamline workflows. Use built-in variables for project metadata, user-defined variables for custom settings, and secret variables to securely store sensitive information.

Stay focused, consistent, and disciplined. Keep learning and evolving in DevOps!

Share this blog with your peers and stay connected!

Comments

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator