πŸš€ Exploring GitLab CI/CD – Stages, Jobs & Runners (Day 33)πŸ› οΈ

πŸ”§ GitLab CI/CD Basics – Understanding Stages & Runners 🚒

In IT, automation is the key to ensuring smooth software development and deployment. GitLab CI/CD provides a powerful way to automate building, testing, and deploying applications using pipelines.

πŸ’‘ Think of GitLab CI/CD as an airport security system 🏒✈️. Every passenger (code update) must go through multiple security checks (build & test stages) before boarding the plane (deployment).

βœ… What You’ll Learn Today:

βœ” What GitLab CI/CD is and why it’s important
βœ” Understanding stages, jobs, and runners
βœ” How to create a CI/CD pipeline in GitLab
βœ” Running a real-world example pipeline


πŸ“Œ What is GitLab CI/CD?

GitLab CI/CD is an automation tool that helps developers:
πŸ”Ή Automatically test and build applications when code is pushed
πŸ”Ή Ensure software quality before deployment
πŸ”Ή Deploy applications efficiently with fewer errors


❌ Challenges Without CI/CD:

  • Manual testing & deployments lead to delays and errors

  • Bugs go unnoticed until production, affecting users

  • Inconsistent environments make debugging difficult

βœ… With GitLab CI/CD:
βœ” Every code update is automatically tested
βœ” Builds and deployments are consistent across environments
βœ” Developers get faster feedback on issues

πŸ“Œ Real-World Analogy: CI/CD as a Fast-Food Kitchen πŸ”πŸš€

  • The kitchen (CI/CD pipeline) follows a structured process

  • Orders (code updates) go through preparation (build) and quality checks (test)

  • If everything is good, they are delivered to customers (deployment)


πŸ— 1️⃣ Understanding GitLab CI/CD Components

GitLab CI/CD uses a .gitlab-ci.yml file to define automation workflows. Let’s break down its key components:

ComponentWhat It DoesExample πŸ—
PipelineThe complete CI/CD processThe entire fast-food preparation process
StagesDifferent phases in the pipelineCooking, Packing, Delivery
JobsTasks inside each stageGrilling, Assembling, Packing
RunnersServers that execute jobsKitchen staff working on orders

πŸ”„ 2️⃣ Understanding GitLab CI/CD Stages

A stage in GitLab CI/CD is a step in the pipeline, like build, test, or deploy.

πŸ“œ Example: CI/CD Stages for a Node.js App

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 to production..."
    - ./deploy.sh

🎯 What Happens in Each Stage?

StagePurpose
BuildInstalls project dependencies
TestRuns unit and integration tests
DeployPushes the application to production

πŸ“Œ Real-World Analogy: Stages as an Automated Factory Process 🏭

  • Build Stage = Raw materials are collected and assembled

  • Test Stage = Quality checks are performed

  • Deploy Stage = Finished products are sent to customers


βš™οΈ 3️⃣ What Are GitLab Runners?

Runners are servers that execute CI/CD pipeline jobs.

Runner TypeDescriptionExample
Shared RunnerManaged by GitLab, used by multiple projectsTemporary contract workers in a factory
Specific RunnerDedicated to a single projectFull-time employees for a single department
Group RunnerShared across multiple projects in a GitLab groupTeam members working across multiple departments

πŸ“Œ Real-World Analogy: Runners as Kitchen Staff 🍳

  • Shared Runners = Freelance chefs working on multiple restaurant orders

  • Specific Runners = Chefs dedicated to a single restaurant

  • Group Runners = Chefs preparing meals for multiple branches of a restaurant


πŸš€ 4️⃣ Setting Up a GitLab CI/CD Pipeline

πŸ”Ή Step 1: Create a .gitlab-ci.yml File

Inside your GitLab repository, create a new file:

.gitlab-ci.yml

πŸ”Ή Step 2: Define the Pipeline Stages

Copy and paste the following YAML configuration:

stages:
  - build
  - test
  - deploy

build_app:
  stage: build
  script:
    - echo "Building the application..."
    - npm install

test_app:
  stage: test
  script:
    - echo "Running tests..."
    - npm test

deploy_app:
  stage: deploy
  script:
    - echo "Deploying to production..."
    - ./deploy.sh

πŸ“Œ How It Works:
βœ” Build Stage – Installs dependencies
βœ” Test Stage – Runs unit tests
βœ” Deploy Stage – Pushes the application to production


πŸ”§ 5️⃣ Setting Up a GitLab Runner for Your Project

πŸ”Ή Step 1: Install GitLab Runner

sudo apt update
sudo apt install gitlab-runner

πŸ”Ή Step 2: Register a New Runner

gitlab-runner register

πŸ“Œ You’ll be prompted to enter:
βœ” GitLab URL – Your GitLab instance URL
βœ” Runner Token – Found under Settings > CI/CD > Runners
βœ” Runner Type – Choose shell, docker, or Kubernetes

πŸ“Œ Now, your GitLab Runner is ready to execute CI/CD jobs!


🌍 6️⃣ Deploying Applications with GitLab CI/CD

πŸ”Ή Deploying to AWS S3 (Static Website Hosting)

GitLab CI/CD can deploy static websites to AWS S3 automatically.

πŸ“Œ Example Deployment Job:

deploy:
  stage: deploy
  script:
    - aws s3 sync ./build s3://my-s3-bucket --delete
  environment:
    name: production

πŸ”Ή Deploying Docker Containers with GitLab CI/CD

Many IT teams deploy applications using Docker for better scalability.

πŸ“Œ Example Deployment Job for Docker:

deploy:
  stage: deploy
  script:
    - docker build -t my-app:latest .
    - docker push my-dockerhub-user/my-app:latest

πŸ† 7️⃣ Best Practices for GitLab CI/CD

βœ… Use Caching – Speed up pipelines by caching dependencies
βœ… Run Tests Before Deployment – Catch bugs early
βœ… Use Secrets for Credentials – Never hardcode API keys
βœ… Monitor Pipeline Performance – Optimize execution time
βœ… Enable Rollbacks – Rollback strategies for failed deployments


🏁 Final Thoughts – Why GitLab CI/CD Matters?

πŸ”Ή Automates software testing, building, and deployment
πŸ”Ή Reduces manual errors and speeds up releases
πŸ”Ή Improves software quality through standardized workflows

πŸ“Œ Next Up: Advanced GitLab CI/CD – Multi-Stage Pipelines & Security Best Practices! πŸš€

πŸ’¬ Got questions? Drop them below! Let’s discuss! 😊

0
Subscribe to my newsletter

Read articles from SRITESH SURANJAN directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

SRITESH SURANJAN
SRITESH SURANJAN

πŸš€ Passionate DevOps Engineer with expertise in cloud computing, CI/CD, and automation. Skilled in Linux, Docker, Kubernetes, Terraform, Ansible, and Jenkins. I specialize in building scalable, secure, and automated infrastructures, optimizing software delivery pipelines, and integrating DevSecOps practices. Always exploring new ways to enhance deployment workflows and bridge the gap between development and operations.