GitLab CI 풀스택 파이프라인

린트, 테스트, 빌드, 배포 단계를 포함하는 종합적인 GitLab CI 파이프라인 설정입니다. 캐싱 및 아티팩트 관리를 통해 효율적인 빌드 프로세스를 제공합니다.

Gist
stages:
  - lint
  - test
  - build
  - deploy

# 글로벌 캐시 설정
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/

variables:
  DOCKER_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG

# 린트 작업
lint:
  stage: lint
  image: node:20-alpine
  script:
    - npm ci
    - npm run lint
  only:
    - merge_requests
    - main

# 유닛 테스트 작업
test:
  stage: test
  image: node:20-alpine
  script:
    - npm ci
    - npm test
  artifacts:
    when: always
    reports:
      junit: junit.xml
      coverage_report:
        coverage_format: cobertura
        path: coverage/cobertura-coverage.xml
  only:
    - merge_requests
    - main

# 도커 이미지 빌드
build:
  stage: build
  image: docker:24.0.5
  services:
    - docker:24.0.5-dind
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build --cache-from $DOCKER_IMAGE -t $DOCKER_IMAGE .
    - docker push $DOCKER_IMAGE
  only:
    - main

# 프로덕션 배포 (수동 승인)
deploy_prod:
  stage: deploy
  image: bitnami/kubectl:latest
  script:
    - kubectl config use-context my-cluster-context
    - sed -i "s|__IMAGE__|$DOCKER_IMAGE|g" k8s/deployment.yaml
    - kubectl apply -f k8s/
  environment:
    name: production
  when: manual
  only:
    - main