Skip to main content

Command Palette

Search for a command to run...

Day 6 : Variables, Secrets, Artifacts & Complete CI/CD Workflow

Updated
β€’4 min readβ€’View as Markdown
Day  6 : Variables, Secrets, Artifacts & Complete CI/CD Workflow
G
Gujjar Apurv is a passionate DevOps Engineer in the making, dedicated to automating infrastructure, streamlining software delivery, and building scalable cloud-native systems. With hands-on experience in tools like AWS, Docker, Kubernetes, Jenkins, Git, and Linux, he thrives at the intersection of development and operations. Driven by curiosity and continuous learning, Apurv shares insights, tutorials, and real-world solutions from his journeyβ€”making complex tech simple and accessible. Whether it's writing YAML, scripting in Python, or deploying on the cloud, he believes in doing it the right way. "Infrastructure is code, but reliability is art."

Master GitLab CI/CD Variables, Secrets Management, Artifacts & Best Practices

πŸ“– Introduction

In the previous article, we learned how GitLab Runners execute CI/CD pipelines and how to configure Self-Hosted Runners.

Now it's time to learn one of the most important topics in GitLab CI/CDβ€”Variables, Secrets, and Artifacts.

Instead of hardcoding sensitive information like API keys, passwords, or cloud credentials, GitLab provides a secure way to manage them using CI/CD Variables. You'll also learn how to store build outputs using Artifacts and follow best practices for building secure and maintainable pipelines.

By the end of this article, you'll be able to create more secure, production-ready GitLab CI/CD pipelines.

🎯 What You'll Learn

After completing this article, you'll understand:

  • βœ… What are CI/CD Variables?

  • βœ… Protected Variables

  • βœ… Masked Variables

  • βœ… Expanded Variables

  • βœ… Secrets Management

  • βœ… Artifacts

  • βœ… Artifact Expiry

  • βœ… Common CI/CD Errors

  • βœ… CI/CD Best Practices

πŸ“š Prerequisites

Before continuing, make sure you have:

  • A GitLab account

  • A GitLab repository

  • A working GitLab CI/CD pipeline

  • Basic knowledge of GitLab CI/CD

πŸ” What are CI/CD Variables?

CI/CD Variables are key-value pairs used to store configuration values and sensitive information securely. Instead of hardcoding values inside the .gitlab-ci.yml file, you can store them as variables and use them during pipeline execution.

For example, instead of writing an API key directly in your pipeline, you can reference it as a variable.

variables:
  APP_NAME: "GitLab Practice"

build:
  script:
    - echo $APP_NAME

πŸ›‘οΈ Protected Variables

Protected Variables are only available to pipelines running on protected branches or protected tags.

They are commonly used for production credentials such as:

  • Production API Keys

  • Database Passwords

  • Cloud Credentials

This prevents sensitive data from being exposed in feature branches.

πŸ™ˆ Masked Variables

Masked Variables hide sensitive values from pipeline logs.

For example, if a variable contains an API key or password, GitLab replaces the actual value with **** in the job logs.

This helps prevent accidental exposure of secrets.

πŸ”„ Expanded Variables

Expanded Variables allow one variable to reference another.

Example:

variables:
  APP_NAME: "gitlab-app"
  IMAGE_NAME: "$APP_NAME:v1"

GitLab automatically expands the referenced value during pipeline execution.

πŸ”‘ Secrets Management

Sensitive information such as passwords, API keys, AWS credentials, and database URLs should never be stored directly in your repository.

Instead, save them in Settings β†’ CI/CD β†’ Variables and reference them inside your pipeline.

deploy:
  script:
    - echo "$AWS_ACCESS_KEY_ID"

πŸ“¦ Artifacts

Artifacts are files generated during a pipeline that can be stored and downloaded after a job completes.

Common examples include:

  • Build output

  • Reports

  • Log files

  • Test results

Example:

build:
  script:
    - mkdir dist
    - echo "Build Complete" > dist/output.txt

  artifacts:
    paths:
      - dist/

⏳ Artifact Expiry

Artifacts can be configured to expire automatically after a specific period.

artifacts:
  paths:
    - dist/
  expire_in: 7 days

⚠️ Common CI/CD Errors

Some common issues you may encounter include:

  • Invalid .gitlab-ci.yml syntax

  • Runner Offline

  • Missing CI/CD Variables

  • Permission Denied

  • Pipeline Failed

Review the pipeline logs to identify and resolve these errors quickly.

βœ… Best Practices

  • Store secrets using CI/CD Variables.

  • Never hardcode passwords or tokens.

  • Use Protected Variables for production.

  • Mask sensitive credentials.

  • Configure artifact expiry.

  • Validate your .gitlab-ci.yml before deployment.

🎯 Summary

Congratulations! πŸŽ‰ You have completed Day 6 of the GitLab learning series.

In this article, you learned how to securely manage CI/CD Variables, protect sensitive information using Protected and Masked Variables, use Artifacts to store build outputs, configure Artifact Expiry, and follow CI/CD best practices for production-ready pipelines.

πŸŽ‰ Congratulations!

You have successfully completed the GitLab DevOps Learning Series.

You now have a solid understanding of GitLab fundamentals, repositories, CI/CD pipelines, GitLab Runners, Variables, Secrets, and Artifacts. These concepts provide a strong foundation for building secure and production-ready DevOps workflows using GitLab.

Complete GitLab DevOps Guide: From Beginner to Advanced

Part 6 of 6

Master GitLab from scratch with this complete step-by-step DevOps series. Learn GitLab fundamentals, repositories, branching, merge requests, CI/CD pipelines, GitLab Runner, Docker, Kubernetes integration, security, deployments, and real-world DevOps workflows. Whether you're a beginner or preparing for DevOps interviews, this series will help you gain practical GitLab skills through hands-on examples and projects.

Start from the beginning

Day 1: What is GitLab? The Complete Beginner's Guide for DevOps Engineers

Learn GitLab fundamentals, architecture, GitHub vs GitLab, Bitbucket comparison, CI/CD pipelines, and why enterprises choose GitLab for modern DevOps..