CI/CD : tests et build 22 min de lecture

Tests automatises et build dans le pipeline

Executer des tests dans le pipeline

L'integration continue consiste a executer les tests automatiquement a chaque push.

Exemple avec Node.js

# .gitlab-ci.yml
image: node:18

stages:
  - install
  - test
  - build

install-deps:
  stage: install
  script:
    - npm ci
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
      - node_modules/

unit-tests:
  stage: test
  script:
    - npm test
  coverage: '/Lines\s*:\s*(\d+\.?\d*)%/'
  artifacts:
    reports:
      junit: junit.xml

lint:
  stage: test
  script:
    - npm run lint

Exemple avec Python

image: python:3.11

stages:
  - test
  - build

test-python:
  stage: test
  script:
    - pip install -r requirements.txt
    - pytest --junitxml=report.xml --cov=app
  artifacts:
    reports:
      junit: report.xml

Build et artifacts

build-app:
  stage: build
  script:
    - npm run build
  artifacts:
    paths:
      - dist/
    expire_in: 1 week

Cache vs Artifacts

  • Cache : accelerer les jobs (ex : node_modules). Peut etre perdu.
  • Artifacts : resultat d'un job transmis au suivant. Fiable et telechargeable.
# Cache des dependances
cache:
  key:
    files:
      - package-lock.json
  paths:
    - node_modules/
Performance : Utilisez npm ci au lieu de npm install dans le CI pour des installations plus rapides et reproductibles.