DEV Community

DevOps Man
DevOps Man

Posted on

๐Ÿ”ง GitLab CI/CD โ€” The Complete Guide

๐Ÿš€ Why GitLab CI/CD Over Jenkins and GitHub Actions?

Feature GitLab CI/CD Jenkins GitHub Actions
Integrated UI โœ… Built-in โŒ Plugin-based โœ… Built-in
Auto DevOps โœ… Native support โŒ Manual โŒ
Kubernetes Deployment โœ… First-class โœ… With plugins โœ… Beta level
Security (Secrets, SAST, DAST) โœ… Built-in โŒ Manual config โš ๏ธ External
Runners Management โœ… Docker/Machine/K8s โœ… Nodes โœ… Hosted/self-hosted

Why choose GitLab CI/CD?

  • Everything in one place (version control + CI/CD)

  • Great for GitOps and K8s-based workflows

  • Easy syntax, strong security model

  • No plugin hell like Jenkins


๐Ÿ“ Pipeline Basics

'# Comments'
Use # to write inline documentation.

# This job builds the project
build:
  script:
    - echo "Compiling..."
Enter fullscreen mode Exit fullscreen mode

stages
Defines the execution order of jobs.

stages:
  - build
  - test
  - deploy
Enter fullscreen mode Exit fullscreen mode

stage
Assigns a job to a defined stage.

test:
  stage: test
  script: echo "Running tests"

Enter fullscreen mode Exit fullscreen mode

script
Commands that the job will execute.

build:
  stage: build
  script:
    - make build

Enter fullscreen mode Exit fullscreen mode

๐Ÿ”€ Multi-Jobs in a Stage

build_app:
  stage: build
  script: make app

build_api:
  stage: build
  script: make api

Enter fullscreen mode Exit fullscreen mode

โณ timeout
Job-level timeout.

test:
  script: run_tests
  timeout: 30 minutes

Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ needs โ€“ Optimizing Pipeline Dependency

deploy:
  stage: deploy
  script: deploy.sh
  needs: [build]

Enter fullscreen mode Exit fullscreen mode

Use when job depends on output from another but not the full stage.


๐Ÿงน before_script and after_script

before_script:
  - echo "Setup"

after_script:
  - echo "Cleanup"

Enter fullscreen mode Exit fullscreen mode

Used globally or at job level for pre/post commands.


๐Ÿ“ฆ artifacts โ€“ Store Build Outputs

build:
  script: make build
  artifacts:
    paths:
      - dist/
    expire_in: 1 week

Enter fullscreen mode Exit fullscreen mode

๐Ÿงฌ variables โ€“ 5 Levels

  • Project-level: via GitLab UI โ†’ Settings โ†’ CI/CD โ†’ Variables
    Used across all jobs.

  • Global level:

variables:
  ENV: production

Enter fullscreen mode Exit fullscreen mode
  • Job-level:
test:
  variables:
    ENV: test
  script: echo $ENV

Enter fullscreen mode Exit fullscreen mode
  • Built-in:

$CI_PIPELINE_ID and $CI_COMMIT_BRANCH..............

  • From file:
variables:
  CONFIG: config.yml
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›‚ only and except (Deprecated โ†’ Use rules)

build:
  script: build.sh
  only:
    - main

Enter fullscreen mode Exit fullscreen mode

๐Ÿ“œ rules, if, and when

test:
  script: test.sh
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
      when: always

Enter fullscreen mode Exit fullscreen mode

๐Ÿƒ Run File Inside Script

script:
  - chmod +x ./deploy.sh
  - ./deploy.sh
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” workflow โ€“ Pipeline Start Conditions

workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
      when: always
    - when: never
Enter fullscreen mode Exit fullscreen mode

๐Ÿณ image โ€“ Docker Environment

image: python:3.9

script:
  - pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ข Matrix/Loops (via parallel:matrix)

job:
  stage: test
  parallel:
    matrix:
      - VARIANT: [A, B, C]
  script:
    - echo $VARIANT

Enter fullscreen mode Exit fullscreen mode

๐ŸŒ Multi-Project Pipelines

trigger-downstream:
  stage: trigger
  trigger:
    project: other-group/other-project
    branch: main
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฆ Downloading/Packaging in a Pipeline

build:
  script:
    - wget https://example.com/app.tar.gz
    - tar -xzf app.tar.gz
Enter fullscreen mode Exit fullscreen mode

โฐ Pipeline Scheduling
Go to GitLab UI โ†’ CI/CD โ†’ Schedules

workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: always
Enter fullscreen mode Exit fullscreen mode

๐Ÿƒ GitLab Runner Types

Runner Type Description Use Case
Shared Runners Maintained by GitLab Quick use, limited config
Group Runners Shared by a group Org-level control
Project Runners Bound to a project Isolation, security
Docker Executor Containers Microservices, clean env
Shell Executor Host machine Full access needed
Kubernetes Executor Pods on K8s Cloud-native CI/CD

Top comments (2)

Collapse
 
shifa_2 profile image
Shifa

such amzing article on gitlab and the table is just amazing

Collapse
 
vishnutejas profile image
DevOps Man

Thank you.