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.