AB
A comprehensive guide to understanding GitLab CI/CD fundamentals, including what it is, key features, setup instructions, and essential CI/CD concepts for building efficient pipelines.
GitLab serves as an all-in-one DevOps platform with robust CI/CD capabilities. This guide will help you understand what GitLab is, why it’s ideal for CI/CD, its key features, and how its CI/CD pipelines work. In this first part of our GitLab CI/CD Ultimate Guide, we’ll cover the fundamentals you need to get started with GitLab CI/CD.
GitLab is an all-in-one DevOps platform that integrates tools for version control, CI/CD pipelines, project management, and monitoring into a single interface. It enables teams to collaborate efficiently, manage their codebase, and automate workflows seamlessly.
Key Highlights of GitLab:
GitLab stands out as a CI/CD tool because of its seamless integration, powerful automation capabilities, and ease of use.
Built-in CI/CD: Unlike other tools requiring plugins, GitLab CI/CD is integrated directly into the platform.
Why does built-in CI/CD matter? It simplifies setup and reduces dependency on third-party tools. This integration means you don’t need to configure and maintain separate systems for source control and CI/CD, leading to a more streamlined workflow.
Single Platform for DevOps: GitLab eliminates the need for separate tools, as it combines source code management, CI/CD, and project management.
Flexibility: Supports a wide range of environments, including Kubernetes, Docker, and multi-cloud setups.
Scalability: GitLab CI/CD can handle everything from small personal projects to large enterprise applications with ease.
GitLab offers a robust feature set, making it ideal for modern software development and delivery workflows.
GitLab’s Git-based version control system helps teams collaborate efficiently, track changes, and manage branches.
Example:
git clone https://gitlab.com/<username>/<project>.git
Merge Requests streamline code reviews, enabling teams to collaborate on changes before merging them into the main branch.
GitLab provides a powerful issue tracker to manage tasks, bugs, and features. Its Kanban-style boards make workflow visualization easy.
The CI/CD system automates testing, building, and deployment.
What are pipelines?
Pipelines consist of stages (like build
, test
, and deploy
) that execute defined tasks in a sequential or parallel manner. They automate the process of taking code from version control to production.
Built-in tools like SAST (Static Application Security Testing) and DAST (Dynamic Application Security Testing) help identify vulnerabilities early.
GitLab provides a built-in Docker Container Registry, enabling seamless containerization workflows.
GitLab integrates with monitoring tools like Prometheus and Grafana to track performance and logs.
GitLab CI/CD automates the software delivery pipeline, enabling teams to ship code faster and more reliably. The core workflow involves the following steps:
.gitlab-ci.yml
FileThe .gitlab-ci.yml
file is at the heart of GitLab CI/CD. It defines the pipeline’s stages, jobs, and scripts.
Example .gitlab-ci.yml
:
stages:
- build
- test
- deploy
build-job:
stage: build
script:
- echo "Building the application"
- npm install
test-job:
stage: test
script:
- echo "Running tests"
- npm test
deploy-job:
stage: deploy
script:
- echo "Deploying the application"
- npm run deploy
What does this file do?
build
, test
, and deploy
..gitlab-ci.yml
automatically triggers.Command:
git commit -m "Add new feature"
git push origin main
main
branch on GitLab..gitlab-ci.yml
.What is a GitLab Runner?
Command:
gitlab-runner run
.gitlab-ci.yml
pipeline triggers automatically.This section walks you through the initial steps to get started with GitLab CI/CD, from creating an account to setting up a GitLab Runner for pipeline execution.
To begin using GitLab CI/CD, you need a GitLab account. Here’s how to create one:
Pro Tip: If you’re working in an organization, sign up with your company email to access group-specific features and repositories.
Why do you need a GitLab account?
A GitLab Project is where your code, issues, CI/CD configuration, and pipeline results live.
Command to Push Local Code to GitLab:
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://gitlab.com/<username>/<project>.git
git push -u origin main
main
branch.To enable GitLab CI/CD, you must link your code repository to GitLab.
Set up SSH to securely connect your local machine with GitLab:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
A GitLab Runner is an agent that executes CI/CD jobs. Runners can be hosted on your local machine, a cloud provider, or GitLab’s shared runners.
Download and install the GitLab Runner:
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt-get install gitlab-runner
Register the Runner:
sudo gitlab-runner register
https://gitlab.com/<username>
).my-runner
).shell
, docker
, kubernetes
, etc.). Docker is recommended for most cases.Test the Runner:
gitlab-runner verify
Create a .gitlab-ci.yml
File:
Add the following to your project to define a basic pipeline:
stages:
- test
test-job:
stage: test
script:
- echo "Running tests..."
test
stage and a job (test-job
) that runs echo "Running tests..."
.echo "Running tests..."
command whenever you push code to GitLab.Push Your Code:
git add .gitlab-ci.yml
git commit -m "Add CI/CD pipeline"
git push origin main
test-job
.Monitor the Pipeline:
Pipeline Not Triggering?
.gitlab-ci.yml
file is in the root directory of your repository.Runner Offline?
sudo gitlab-runner restart
Understanding GitLab CI/CD concepts is crucial for leveraging its full potential. In this section, we’ll break down the building blocks of GitLab CI/CD, from pipelines and configuration files to artifacts and secrets.
A pipeline in GitLab is a series of stages and jobs that automate tasks like building, testing, and deploying code.
build
, test
, and deploy
.Example Pipeline Visualization:
Pipeline: [ Build Stage ] → [ Test Stage ] → [ Deploy Stage ]
| job-1 | | job-2 | | job-3 |
Why Use Pipelines?
The .gitlab-ci.yml
file is the backbone of GitLab CI/CD. It defines your pipeline’s structure, including stages, jobs, and execution rules.
.gitlab-ci.yml
:build
, test
, deploy
).stages:
- build
- test
- deploy
build-job:
stage: build
script:
- echo "Building the application..."
test-job:
stage: test
script:
- echo "Running tests..."
deploy-job:
stage: deploy
script:
- echo "Deploying the application..."
Use the GitLab CI Lint Tool to validate your .gitlab-ci.yml
.
Stages are the logical steps of your CI/CD process. Each stage contains one or more jobs.
Note: You can give any name to the stage
Example:
stages:
- build
- test
- deploy
test-job:
stage: test
script:
- echo "Running parallel tests..."
What if a stage fails?
If a stage fails, subsequent stages are skipped unless specified otherwise. This helps prevent deploying broken code to production environments.
A job is a single task in a pipeline, and a runner is the agent that executes these jobs.
Jobs include:
Example Job:
build-job:
stage: build
script:
- echo "Building the app..."
tags:
- docker
What if we don’t specify the stage?
If we don’t specify the stage, the job will run in the test
stage by default.
shell
, docker
, and kubernetes
.Example Command to List Runners:
gitlab-runner list
Artifacts: Temporary files created by a job that can be passed to subsequent jobs.
Caching: Reuses dependencies or build outputs to speed up pipelines.
build-job:
stage: build
script:
- mkdir build
- echo "Build files" > build/output.txt
artifacts:
paths:
- build/
build/
folder for later stages.build/
folder.cache:
paths:
- node_modules/
node_modules/
directory to speed up future pipeline runs.How are artifacts and caches different?
Sensitive data like API keys, passwords, or configurations should not be hardcoded. Use environment variables for secure management.
Environment variables are a way to store sensitive data, such as API keys, passwords, and configuration settings, outside of your codebase. This approach enhances security by preventing sensitive information from being hardcoded in your repositories.
To set up environment variables for your GitLab CI/CD pipelines, follow these steps:
Navigate to the Settings:
Access Variables Section:
Add Variables:
API_KEY
) in the Key field.your-secret-api-key
) in the Value field.Configure Options:
When creating a variable, you have several options to choose from:
Protected:
Masked:
echo $API_KEY
), it won’t show the actual value in the output logs, providing an additional layer of security.Environment Scope:
development
, staging
, production
). By default, the variable will be available to all environments if this option is not specified.Value Type:
Save the Variable:
Once you have defined your environment variables, you can easily use them in your .gitlab-ci.yml
file.
Example of Using an Environment Variable:
test-job:
script:
- echo "API_KEY is $API_KEY"
$API_KEY
with the value of the API_KEY
environment variable you defined in the GitLab CI/CD settings.API_KEY is ********
).Why use environment variables?
API_KEY
).your-secret-api-key
).Not all pipelines should run automatically. GitLab allows manual and triggered pipelines for more control.
Use the when: manual
keyword to specify jobs that require manual approval.
deploy-job:
stage: deploy
script:
- echo "Deploying..."
when: manual
Trigger pipelines from external systems or another pipeline.
trigger-job:
stage: trigger
trigger:
project: path/to/project
branch: main
In this first part of our GitLab CI/CD Ultimate Guide, we’ve covered the fundamentals of GitLab CI/CD including:
In the second part of this guide, we’ll explore advanced GitLab CI/CD features, integrations with other tools, best practices, and real-world examples to help you take your CI/CD pipelines to the next level.
By understanding these fundamentals, you’re now equipped to start building basic CI/CD pipelines with GitLab and take advantage of its powerful automation capabilities.