CI/CD pipeline with GitLab CI
Ryan Nakamura
Feb 2026
1 tab
stages:
- lint
- test
- build
- deploy
variables:
NODE_VERSION: "20"
DOCKER_DRIVER: overlay2
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
default:
image: node:${NODE_VERSION}-alpine
cache:
key:
files:
- package-lock.json
paths:
- node_modules/
# Templates
.deploy_template: &deploy
image: bitnami/kubectl:latest
before_script:
- kubectl config use-context $KUBE_CONTEXT
# Lint
lint:
stage: lint
script:
- npm ci
- npm run lint
- npm run format:check
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
# Test with coverage
test:
stage: test
services:
- name: postgres:16-alpine
alias: db
variables:
POSTGRES_DB: test
POSTGRES_PASSWORD: test
DATABASE_URL: postgres://postgres:test@db:5432/test
script:
- npm ci
- npm test -- --coverage
coverage: '/All files[^|]*|[^|]* +([d.]+)/'
artifacts:
when: always
reports:
junit: junit.xml
coverage_report:
coverage_format: cobertura
path: coverage/cobertura-coverage.xml
paths:
- coverage/
expire_in: 7 days
# Build Docker image
build:
stage: build
image: docker:24
services:
- docker:24-dind
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker build -t $IMAGE_TAG .
- docker push $IMAGE_TAG
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
# Deploy to staging
deploy_staging:
<<: *deploy
stage: deploy
script:
- kubectl set image deployment/web-app web-app=$IMAGE_TAG -n staging
- kubectl rollout status deployment/web-app -n staging --timeout=300s
environment:
name: staging
url: https://staging.example.com
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
# Deploy to production (manual gate)
deploy_production:
<<: *deploy
stage: deploy
script:
- kubectl set image deployment/web-app web-app=$IMAGE_TAG -n production
- kubectl rollout status deployment/web-app -n production --timeout=300s
environment:
name: production
url: https://app.example.com
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manual
needs:
- deploy_staging
1 file · yaml
Explain with highlit
GitLab CI/CD uses .gitlab-ci.yml for pipeline configuration. Pipelines consist of stages that run sequentially. Jobs within the same stage run in parallel. The image key sets the Docker image for each job. variables define global or job-level environment settings. cache persists files between pipeline runs. artifacts pass files between jobs. rules (replacing only/except) control when jobs run with flexible conditions. needs creates a directed acyclic graph (DAG) for non-linear pipelines. environments track deployments. include imports templates for reusable configurations. services attach sidecar containers like databases.