Aller au contenu principal

Cache et artifacts


Table des matières

  1. Différence cache vs artifacts
  2. Utiliser le cache
  3. Utiliser les artifacts
  4. Stratégies avancées
  5. Optimisation
  6. Exercices pratiques


1 - Différence cache vs artifacts

Comparaison

AspectCacheArtifacts
ObjectifAccélérer les buildsPartager des résultats
PersistanceTemporaire (~7 jours)Configurable (1-90 jours)
ScopeMême workflow/brancheEntre jobs/workflows
RestaurationPar clé (hash)Par nom
Taille max10 GB total500 MB par artifact

Cas d'utilisation

🔝 Retour à la table des matières



2 - Utiliser le cache

actions/cache

- uses: actions/cache@v4
with:
path: |
~/.npm
node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

Paramètres

ParamètreDescription
pathChemins à cacher
keyClé unique pour ce cache
restore-keysClés de fallback

Cache Node.js

# Méthode 1: actions/cache
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}

- run: npm ci

# Méthode 2: Intégré dans setup-node (recommandé)
- uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm'

- run: npm ci

Cache Python

- uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'

- run: pip install -r requirements.txt

# Ou manuellement
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}

Cache Docker

- uses: docker/build-push-action@v5
with:
context: .
push: true
tags: user/app:latest
cache-from: type=gha
cache-to: type=gha,mode=max

Vérifier le cache hit

- uses: actions/cache@v4
id: cache
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}

- name: Install if no cache
if: steps.cache.outputs.cache-hit != 'true'
run: npm ci

🔝 Retour à la table des matières



3 - Utiliser les artifacts

Upload artifact

- uses: actions/upload-artifact@v4
with:
name: my-artifact # Nom de l'artifact
path: | # Fichiers/dossiers
dist/
build/output.zip
retention-days: 5 # Durée de rétention
if-no-files-found: error # error, warn, ignore

Download artifact

- uses: actions/download-artifact@v4
with:
name: my-artifact
path: ./downloaded # Destination

Partager entre jobs

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm run build

- uses: actions/upload-artifact@v4
with:
name: build-output
path: dist/

test:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: build-output
path: ./dist

- run: npm test

deploy:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: build-output
path: ./dist

- run: ./deploy.sh

Multiple artifacts

# Upload plusieurs artifacts
- uses: actions/upload-artifact@v4
with:
name: test-report
path: test-results/

- uses: actions/upload-artifact@v4
with:
name: coverage
path: coverage/

# Download tous les artifacts
- uses: actions/download-artifact@v4
# Sans 'name', télécharge tous les artifacts

🔝 Retour à la table des matières



4 - Stratégies avancées

Clés de cache intelligentes

# Clé avec version + lock file
key: v1-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}

# Inclure la version de Node
key: ${{ runner.os }}-node${{ matrix.node }}-${{ hashFiles('**/package-lock.json') }}

# Fallback progressif
restore-keys: |
${{ runner.os }}-node${{ matrix.node }}-
${{ runner.os }}-node-
${{ runner.os }}-

Cache conditionnel

- uses: actions/cache@v4
if: github.event_name == 'push'
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}

Artifacts avec pattern

- uses: actions/upload-artifact@v4
with:
name: logs
path: |
**/*.log
!**/node_modules/**

Compression artifacts

- name: Compress before upload
run: tar -czf build.tar.gz dist/

- uses: actions/upload-artifact@v4
with:
name: build
path: build.tar.gz

🔝 Retour à la table des matières



5 - Optimisation

Mesurer l'impact

- name: Build without cache
run: |
START=$(date +%s)
npm ci
END=$(date +%s)
echo "Install time: $((END-START))s"

Bonnes pratiques cache

# ✅ Bon : Hash du lockfile
key: npm-${{ hashFiles('**/package-lock.json') }}

# ✅ Bon : Inclure OS et version
key: ${{ runner.os }}-node18-${{ hashFiles('**/package-lock.json') }}

# ❌ Mauvais : Clé statique
key: npm-dependencies

# ❌ Mauvais : Hash trop large
key: ${{ hashFiles('**/*') }}

Limites à connaître

LimiteValeur
Cache total par repo10 GB
Rétention cache7 jours (inactif)
Artifact max size500 MB
Artifact retention1-90 jours

Nettoyer les caches

# Via GitHub CLI
gh cache list
gh cache delete KEY

🔝 Retour à la table des matières



6 - Exercices pratiques

Exercice 1 : Cache npm

Créez un workflow avec cache npm :

Solution
name: Build with Cache

on: push

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: '18'
cache: 'npm'

- run: npm ci
- run: npm run build

Exercice 2 : Partager build entre jobs

Partagez le résultat du build avec le job de déploiement :

Solution
name: Build and Deploy

on: push

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm run build

- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/

deploy:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: dist
path: ./dist

- run: ls -la dist/

Quiz

Q1. Quelle est la durée de rétention par défaut du cache ?

Réponse

7 jours d'inactivité.

Q2. Comment vérifier si le cache a été restauré ?

Réponse

steps.cache.outputs.cache-hit == 'true'

🔝 Retour à la table des matières



Points clés à retenir

  • Cache : accélère les builds (dépendances)
  • Artifacts : partage les résultats entre jobs
  • hashFiles() pour des clés de cache uniques
  • restore-keys pour fallback progressif
  • setup-node/python : cache intégré
  • Limite de 10 GB de cache par repo
  • Artifacts téléchargeables depuis l'UI GitHub

🔝 Retour à la table des matières


← Chapitre précédent | Chapitre suivant : Matrix builds →