perf: optimize pipeline with npm cache and parallel stages

This commit is contained in:
soufiane 2025-11-25 15:36:19 +01:00
parent 63096f22e4
commit 9017313bf7

112
Jenkinsfile vendored
View File

@ -1,9 +1,7 @@
pipeline {
agent any
// Déclenchement automatique sur push Git
triggers {
// Vérifie les changements toutes les minutes (polling SCM)
pollSCM('* * * * *')
}
@ -18,6 +16,7 @@ pipeline {
environment {
REGISTRY_URL = "registry.wk-archi-o24a-15m-g3.fr"
IMAGE_NAME = "the-tip-top-backend"
NPM_CACHE = "/var/jenkins_home/npm-cache"
}
stages {
@ -42,8 +41,6 @@ pipeline {
🏷️ Tag Docker = ${env.TAG}
📂 Chemin de déploiement = ${env.DEPLOY_PATH}
"""
sh "ls -l ${env.DEPLOY_PATH} || echo '⚠️ Dossier non accessible depuis Jenkins'"
}
}
}
@ -55,57 +52,73 @@ pipeline {
}
}
stage('Tests & Qualité') {
stage('Install Dependencies') {
agent {
docker {
image 'node:18-alpine'
args '-u root'
args "-u root -v ${NPM_CACHE}:/root/.npm"
}
}
steps {
echo "🧪 Lancement des tests et analyse..."
script {
def lintStatus = sh(script: 'npm ci && npm run lint', returnStatus: true)
def testStatus = sh(script: 'npm test', returnStatus: true)
if (lintStatus != 0) {
error "❌ ESLint a échoué - Le déploiement est bloqué"
}
if (testStatus != 0) {
error "❌ Les tests ont échoué - Le déploiement est bloqué"
}
echo "✅ Tests et lint passés avec succès"
}
echo "📦 Installation des dépendances..."
sh 'npm ci --prefer-offline'
stash includes: 'node_modules/**', name: 'node_modules'
}
}
stage('SonarQube Analysis') {
agent {
docker {
image 'sonarsource/sonar-scanner-cli:latest'
args '-u root'
stage('Quality Checks') {
parallel {
stage('Lint & Tests') {
agent {
docker {
image 'node:18-alpine'
args "-u root -v ${NPM_CACHE}:/root/.npm"
}
}
steps {
unstash 'node_modules'
echo "🧪 Lancement des tests et lint..."
script {
def lintStatus = sh(script: 'npm run lint', returnStatus: true)
def testStatus = sh(script: 'npm test', returnStatus: true)
if (lintStatus != 0) {
error "❌ ESLint a échoué"
}
if (testStatus != 0) {
error "❌ Les tests ont échoué"
}
echo "✅ Tests et lint passés"
}
}
}
}
steps {
echo "🔍 Analyse SonarQube..."
withSonarQubeEnv('SonarQube') {
sh """
sonar-scanner \
-Dsonar.projectKey=Th-Tip-Top-Backend \
-Dsonar.projectName='Thé Tip Top Backend' \
-Dsonar.sources=. \
-Dsonar.exclusions=node_modules/**,coverage/**,dist/** \
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
"""
stage('SonarQube') {
agent {
docker {
image 'sonarsource/sonar-scanner-cli:latest'
args '-u root'
}
}
steps {
echo "🔍 Analyse SonarQube..."
withSonarQubeEnv('SonarQube') {
sh """
sonar-scanner \
-Dsonar.projectKey=Th-Tip-Top-Backend \
-Dsonar.projectName='Thé Tip Top Backend' \
-Dsonar.sources=. \
-Dsonar.exclusions=node_modules/**,coverage/**,dist/**
"""
}
}
}
}
}
stage('Build Docker image') {
steps {
echo "🐳 Construction de limage Docker backend..."
echo "🐳 Construction de l'image Docker backend..."
sh """
docker build -t ${REGISTRY_URL}/${IMAGE_NAME}:${TAG} .
docker tag ${REGISTRY_URL}/${IMAGE_NAME}:${TAG} ${REGISTRY_URL}/${IMAGE_NAME}:latest
@ -115,7 +128,7 @@ pipeline {
stage('Push to Registry') {
steps {
echo "📤 Envoi de limage vers le registre..."
echo "📤 Envoi de l'image vers le registre..."
withCredentials([usernamePassword(credentialsId: 'registry-credentials', usernameVariable: 'REG_USER', passwordVariable: 'REG_PASS')]) {
sh """
echo "$REG_PASS" | docker login ${REGISTRY_URL} -u "$REG_USER" --password-stdin
@ -126,25 +139,6 @@ pipeline {
}
}
stage('Backup before deploy') {
steps {
echo "💾 Sauvegarde de lenvironnement ${env.ENV} avant déploiement..."
sh '''
BACKUP_DIR="/srv/backups/the-tip-top/${ENV}/$(date +%F_%H-%M-%S)"
mkdir -p $BACKUP_DIR
echo "📦 Sauvegarde du docker-compose et logs..."
cp -r ${DEPLOY_PATH}/docker-compose.yml $BACKUP_DIR/ 2>/dev/null || true
cp -r ${DEPLOY_PATH}/logs $BACKUP_DIR/ 2>/dev/null || true
tar -czf ${BACKUP_DIR}.tar.gz -C $(dirname $BACKUP_DIR) $(basename $BACKUP_DIR)
rm -rf $BACKUP_DIR
echo "✅ Sauvegarde compressée : ${BACKUP_DIR}.tar.gz"
# (Optionnel) Transfert distant :
# scp ${BACKUP_DIR}.tar.gz backup@backup-server:/backups/the-tip-top/${ENV}/
'''
}
}
stage('Deploy') {
steps {
echo "🚀 Déploiement sur ${env.ENV}..."