retour
01/04
section 01

GitLab CI (.gitlab-ci.yml)

Connectez-vous pour sauvegarder votre progression et acceder aux quiz
Se connecter

GitLab CI/CD est la solution CI/CD integree a GitLab. Elle se configure via un fichier .gitlab-ci.yml a la racine du repository.

Structure de base

yaml# .gitlab-ci.yml
stages:
  - build
  - test
  - deploy

variables:
  NODE_VERSION: "20"

# Job de build
build:
  stage: build
  image: node:20-alpine
  script:
    - npm ci
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 hour

# Job de test
test:
  stage: test
  image: node:20-alpine
  script:
    - npm ci
    - npm test
  coverage: '/Statements\s*:\s*(\d+\.?\d*)%/'

# Job de deploiement
deploy_staging:
  stage: deploy
  script:
    - echo "Deploying to staging"
    - rsync -avz dist/ user@staging-server:/var/www/
  environment:
    name: staging
    url: https://staging.example.com
  only:
    - develop

deploy_production:
  stage: deploy
  script:
    - echo "Deploying to production"
  environment:
    name: production
    url: https://example.com
  when: manual          # Declenchement manuel
  only:
    - main

Concepts cles GitLab CI

ConceptDescription
StagesPhases du pipeline (build, test, deploy). Les jobs d'un meme stage s'executent en parallele
JobsUnite d'execution. Chaque job a un script, un stage et un runner
RunnersMachines qui executent les jobs (shared ou project-specific)
ArtifactsFichiers produits par un job et passes aux jobs suivants
CacheFichiers caches entre les pipelines (node_modules, etc.)
VariablesVariables d'environnement (projet, groupe ou instance)

Cache vs Artifacts

yaml# Cache : persiste entre les pipelines (meme branche)
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/

# Artifacts : passes entre les jobs d'un meme pipeline
build:
  artifacts:
    paths:
      - dist/
    expire_in: 1 week

Rules (remplace only/except)

yamldeploy:
  stage: deploy
  script: ./deploy.sh
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: manual
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - when: on_success
Point fort GitLab CI : L'integration native avec GitLab (issues, merge requests, registry, monitoring) est son plus grand avantage. Le fichier .gitlab-ci.yml est plus intuitif que les workflows GitHub Actions pour les pipelines lineaires. Les environnements avec historique de deploiement sont particulierement bien faits.