How To Build CI/CD Pipeline in GitLab

Introduction

In modern software development, automation is crucial for efficiency and reliability. CI/CD (Continuous Integration/Continuous Deployment) pipelines help automate the entire process of building, testing, and deploying applications. GitLab CI/CD provides a powerful built-in pipeline mechanism to streamline this workflow.

This guide will walk you through creating a CI/CD pipeline using GitLab, covering setup, writing a .gitlab-ci.yml configuration file, defining build, test, and deployment stages, and best practices.


Table of Contents

  1. What is GitLab?
  2. Understanding CI/CD Pipelines
  3. Prerequisites
  4. Setting Up a CI/CD Pipeline in GitLab
    • Creating a GitLab Account
    • Checking for GitLab Runners
    • Creating or Importing a Project
  5. Writing a .gitlab-ci.yml File
    • Build Stage
    • Test Stage
    • Deploy Stage
  6. Optimizing the Pipeline
  7. Monitoring Pipelines in GitLab
  8. Conclusion

🔗 Official Documentation:


1. What is GitLab?

GitLab is an all-in-one DevOps platform that enables developers to store, manage, and collaborate on code efficiently. It includes built-in CI/CD tools to automate the software development lifecycle, from code integration to deployment.

Key Features of GitLab CI/CD:
✔ Automated testing and deployment
✔ Supports Docker-based builds
✔ Integration with Kubernetes and cloud providers
✔ Customizable pipelines with .gitlab-ci.yml


2. Understanding CI/CD Pipelines

A CI/CD pipeline is a series of automated steps that manage the software development lifecycle, including:

  • Continuous Integration (CI): Automatically testing and integrating code changes.
  • Continuous Deployment (CD): Deploying software updates to production.

CI/CD Pipeline Workflow in GitLab:

1️⃣ Code Changes → Developer pushes code to GitLab.
2️⃣ CI/CD Pipeline Triggers → GitLab starts build, test, and deployment stages.
3️⃣ Build Stage → Compiles code and prepares artifacts.
4️⃣ Test Stage → Runs automated tests to ensure quality.
5️⃣ Deploy Stage → Deploys changes to production.


3. How To Build CI/CD Pipeline in GitLab – Prerequisites

Before creating a GitLab CI/CD pipeline, ensure you have:

A GitLab AccountSign up at GitLab.com
Basic Git KnowledgeGit Documentation
A GitLab Project – Either create a new project or import an existing one.
GitLab Runner Installed – If using GitLab locally, install a GitLab Runner.


4. Setting Up a CI/CD Pipeline in GitLab

Step 1: Creating a GitLab Account

If you haven’t already, create a GitLab account at GitLab.com.

Step 2: Checking for Runners

Runners execute CI/CD jobs. To check available runners:

  • Navigate to Settings → CI/CD → Runners
  • If using GitLab’s hosted service, a runner should be active.
  • If self-hosting, install a GitLab Runner (Installation Guide).

Step 3: Creating or Importing a Project

  • Go to GitLab Dashboard
  • Click New Project → Import Project (for GitHub imports)

5. Writing a .gitlab-ci.yml File

The .gitlab-ci.yml file defines the CI/CD pipeline and should be placed in the root directory of your project.

Step 1: Creating the File

In your GitLab project:
1️⃣ Click the + button → New File
2️⃣ Name it .gitlab-ci.yml


Step 2: Defining the Pipeline Stages

stages:
  - build
  - test
  - deploy

This ensures GitLab runs tasks in the order of build → test → deploy.


Step 3: Build Stage

build-job:
  image: node
  stage: build
  script:
    - npm install
    - npm run build
  artifacts:
    paths:
      - "dist/"

🔹 Uses the Node.js image to install dependencies and build the project.
🔹 Stores the dist/ folder for later stages.


Step 4: Test Stage

test-html:
  stage: test
  dependencies: []
  script:
    - npm install --save-dev htmlhint
    - npx htmlhint "dist/**/*.html"

🔹 Runs HTML linting to check for errors.
🔹 Defines dependencies: [] so this job runs independently.


Step 5: Deploy Stage

deploy:
  stage: deploy
  image: busybox
  dependencies:
    - build-job
  script:
    - mv dist/ public/
  artifacts:
    paths:
      - "public/"
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

🔹 Deploys the built files to GitLab Pages or another hosting provider.
🔹 Uses a lightweight BusyBox image for efficiency.
🔹 Ensures deployment only happens on the main branch.


6. Optimizing the Pipeline

To avoid duplication, we define reusable jobs:

default:
  image: node

.standard-rules:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

🔹 default: sets a global Node.js image for all jobs.
🔹 .standard-rules: defines conditions for triggering jobs.


7. Monitoring Pipelines in GitLab

To track your CI/CD pipeline:
1️⃣ Go to Project Dashboard
2️⃣ Click Build → Pipelines
3️⃣ View the status of each job (Success ✅ or Failure ❌)

🔗 Monitoring CI/CD Pipelines


8. Conclusion

Using GitLab’s CI/CD pipeline, we can automate the build, test, and deploy workflow efficiently. This approach reduces manual errors, saves time, and ensures software is always in a deployable state.

Key Takeaways:

✔ GitLab CI/CD automates building, testing, and deployment.
✔ The .gitlab-ci.yml file defines pipeline stages and jobs.
GitLab Runners execute CI/CD jobs efficiently.
Monitoring pipelines helps track build status and errors.

Further Reading:

🔗 GitLab CI/CD Overview
🔗 GitLab Runners Setup
🔗 Writing .gitlab-ci.yml

Learn about CI/CD Pipeline Automation

Related articles

How to Create Cloud Storage Bucket in Google Cloud Platform

 How to Create Cloud Storage Bucket in Google Cloud Platform ✨ Introduction to GCP Cloud Storage Google Cloud Platform (GCP)...

Create Custom Role in Azure

Create Custom Role in Azure A detailed guide on creating and managing custom roles in Microsoft Azure to enhance...

Azure Cost Optimization

Azure Cost Optimization Introduction Azure Virtual Machines (VMs) provide scalable and flexible compute resources in the cloud, enabling businesses to...

Microsoft Defender for Cloud

Microsoft Defender for Cloud Introduction Microsoft Defender for Cloud (formerly known as Azure Security Center) is a comprehensive, cloud-native security...