the-tip-top-backend/Jenkinsfile
soufiane 9330c68e5c fix: improve CI/CD quality checks and fix test/lint configuration
Backend fixes:
- Add eslint.config.js with proper ES6 module configuration
- Add jest.config.js to support ES modules
- Update package.json with @eslint/js dependency
- Configure npm test script with NODE_OPTIONS for ES modules
- Update Jenkinsfile to block deployments on failed lint/tests

This ensures:
1. ESLint works correctly with ES6 modules
2. Jest can run tests with ES6 imports
3. Deployments are blocked if quality checks fail

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 15:40:04 +01:00

160 lines
5.5 KiB
Groovy
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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('* * * * *')
// Ou utiliser un webhook Gitea (recommandé)
// Le webhook doit être configuré dans Gitea pour pointer vers:
// http://jenkins-url/generic-webhook-trigger/invoke?token=the-tip-top-backend-token
GenericTrigger(
genericVariables: [
[key: 'ref', value: '$.ref']
],
causeString: 'Triggered by Gitea push',
token: 'the-tip-top-backend-token',
printContributedVariables: true,
printPostContent: true
)
}
parameters {
choice(
name: 'ENV',
choices: ['dev', 'preprod', 'prod'],
description: 'Choisir l'environnement de déploiement'
)
}
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('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}."
}
}
}