GitLab CI/CD — Complete Pipeline Guide

Sanjeev SharmaSanjeev Sharma
2 min read

Advertisement

GitLab CI/CD — Complete Pipeline Guide

GitLab CI/CD provides powerful pipeline capabilities integrated directly into GitLab.

Introduction

GitLab CI/CD enables complete automation through .gitlab-ci.yml configuration in your repository.

Basic Pipeline

Create .gitlab-ci.yml:

image: node:18

stages:
  - build
  - test
  - deploy

build:
  stage: build
  script:
    - npm install
    - npm run build
  artifacts:
    paths:
      - dist/

test:
  stage: test
  script:
    - npm test

deploy:
  stage: deploy
  environment: production
  script:
    - npm run deploy
  only:
    - main

Jobs and Stages

stages:
  - build
  - test
  - deploy

build:job1:
  stage: build
  script: npm run build:service1

build:job2:
  stage: build
  script: npm run build:service2

test:unit:
  stage: test
  script: npm run test:unit

test:integration:
  stage: test
  script: npm run test:integration

deploy:
  stage: deploy
  script: npm run deploy

Services

image: node:18

services:
  - postgres:15
  - redis:7

variables:
  POSTGRES_DB: test_db
  POSTGRES_PASSWORD: password

test:
  script:
    - npm install
    - npm test

Artifacts and Caching

build:
  script: npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 week
  cache:
    paths:
      - node_modules/

FAQ

Q: How do I run jobs only on certain branches? A: Use only: or except: to control when jobs run based on branch, tags, or events.

Q: Can I run jobs in parallel? A: Yes, jobs in the same stage run in parallel by default.

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro