the-tip-top-backend/Jenkinsfile
soufiane 0e1cd727c4 feat: add SonarQube integration for code quality analysis
- Add SonarQube configuration files
  - sonar-project.properties with Node.js/Express settings
  - .sonarignore to exclude test files, database, and build artifacts
  - Configure source paths (src, index.js, db.js)
  - Set up test coverage paths

- Add SonarQube npm script
  - npm run sonar command for manual analysis

- Integrate SonarQube into Jenkins pipeline
  - Add SonarQube Analysis stage with sonar-scanner-cli
  - Add Quality Gate verification stage
  - Block deployment if quality gate fails
  - 5-minute timeout for quality gate check

This enables continuous code quality monitoring and ensures
code meets quality standards before deployment.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-21 01:24:59 +01:00

173 lines
5.7 KiB
Groovy
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

pipeline {
agent any
// Déclenchement automatique sur push Git
triggers {
// Vérifie les changements toutes les minutes (polling SCM)
pollSCM('* * * * *')
}
parameters {
choice(
name: 'ENV',
choices: ['dev', 'preprod', 'prod'],
description: 'Choisir l environnement de deploiement'
)
}
environment {
REGISTRY_URL = "registry.wk-archi-o24a-15m-g3.fr"
IMAGE_NAME = "the-tip-top-backend"
}
stages {
stage('Init') {
steps {
script {
def currentBranch = sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
echo "🧭 Branche détectée : ${currentBranch}"
if (["dev", "preprod", "main"].contains(currentBranch)) {
env.ENV = (currentBranch == "main") ? "prod" : currentBranch
} else {
env.ENV = params.ENV ?: "dev"
}
env.TAG = "${env.ENV}-latest"
env.DEPLOY_PATH = "/srv/devops/the-tip-top/${env.ENV}"
echo """
🌍 Environnement = ${env.ENV}
🏷️ Tag Docker = ${env.TAG}
📂 Chemin de déploiement = ${env.DEPLOY_PATH}
"""
sh "ls -l ${env.DEPLOY_PATH} || echo '⚠️ Dossier non accessible depuis Jenkins'"
}
}
}
stage('Checkout') {
steps {
echo "📦 Récupération du code source..."
checkout scm
}
}
stage('Tests & Qualité') {
agent {
docker {
image 'node:18-alpine'
args '-u root'
}
}
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"
}
}
}
stage('SonarQube Analysis') {
agent {
docker {
image 'sonarsource/sonar-scanner-cli:latest'
args '-u root'
}
}
steps {
echo "🔍 Analyse de la qualité du code avec SonarQube..."
script {
withSonarQubeEnv('SonarQube') {
sh 'sonar-scanner'
}
}
}
}
stage('Quality Gate') {
steps {
echo "🚦 Vérification du Quality Gate SonarQube..."
timeout(time: 5, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
stage('Build Docker image') {
steps {
echo "🐳 Construction de limage Docker backend..."
sh """
docker build -t ${REGISTRY_URL}/${IMAGE_NAME}:${TAG} .
docker tag ${REGISTRY_URL}/${IMAGE_NAME}:${TAG} ${REGISTRY_URL}/${IMAGE_NAME}:latest
"""
}
}
stage('Push to Registry') {
steps {
echo "📤 Envoi de limage 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
docker push ${REGISTRY_URL}/${IMAGE_NAME}:${TAG}
docker push ${REGISTRY_URL}/${IMAGE_NAME}:latest
"""
}
}
}
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}..."
sh """
cd "${DEPLOY_PATH}"
docker compose pull backend
docker compose up -d --force-recreate backend
"""
}
}
}
post {
success {
echo "✅ Pipeline ${env.ENV} terminé avec succès !"
}
failure {
echo "❌ Échec du pipeline ${env.ENV}."
}
}
}