the-tip-top-backend/Jenkinsfile

176 lines
7.4 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
// 🔁 Vérifie automatiquement toutes les 2 minutes sil y a des commits sur Gitea
triggers {
pollSCM('H/2 * * * *')
}
// ⚙️ Choix de lenvironnement de déploiement
parameters {
choice(
name: 'ENV',
choices: ['dev', 'preprod', 'prod'],
description: 'Choisir lenvironnement de déploiement'
)
}
// 🌍 Variables globales
environment {
REGISTRY_URL = "registry.wk-archi-o24a-15m-g3.fr"
IMAGE_NAME = "the-tip-top-backend"
TAG = "${params.ENV}-latest"
DEPLOY_PATH = "/srv/devops/the-tip-top/${params.ENV}"
}
stages {
/* ───────────────────────────────
* 1⃣ Récupération du code source
* ─────────────────────────────── */
stage('Checkout') {
steps {
echo "📦 Récupération du code source depuis Gitea..."
checkout scm
}
}
/* ───────────────────────────────
* 2⃣ Tests de qualité et unitaires
* ─────────────────────────────── */
stage('Tests & Qualité') {
steps {
echo "🧪 Lancement des tests et analyse de code..."
sh '''
npm ci
npm run lint || echo "⚠️ Erreurs de lint détectées"
npm test || echo "⚠️ Tests échoués — vérifier les logs"
'''
}
}
/* ───────────────────────────────
* 3⃣ Informations de contexte
* ─────────────────────────────── */
stage('Show Environment Info') {
steps {
echo "🌍 Environnement : ${params.ENV}"
echo "📦 Image : ${REGISTRY_URL}/${IMAGE_NAME}:${TAG}"
echo "📂 Dossier de déploiement : ${DEPLOY_PATH}"
}
}
/* ───────────────────────────────
* 4⃣ Construction de limage Docker
* ─────────────────────────────── */
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
"""
}
}
/* ───────────────────────────────
* 5⃣ Envoi au registre Docker privé
* ─────────────────────────────── */
stage('Push to Registry') {
steps {
echo "📤 Envoi de limage vers le registre Docker privé..."
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
"""
}
}
}
/* ───────────────────────────────
* 6⃣ Sauvegarde avant déploiement
* ─────────────────────────────── */
stage('Backup Before Deploy') {
steps {
echo "💾 Exécution du script de sauvegarde avant déploiement..."
sh '''
if [ -f /srv/devops/the-tip-top/backup.sh ]; then
bash /srv/devops/the-tip-top/backup.sh
else
echo "⚠️ Aucun script backup.sh trouvé, déploiement sans sauvegarde."
fi
'''
}
}
/* ───────────────────────────────
* 7⃣ Déploiement Docker Compose
* ─────────────────────────────── */
stage('Deploy') {
steps {
echo "🚀 Déploiement du backend sur ${params.ENV}..."
sh '''
if [ ! -f ${DEPLOY_PATH}/docker-compose.yml ]; then
echo "❌ Fichier docker-compose.yml introuvable dans ${DEPLOY_PATH}"
exit 1
fi
cd ${DEPLOY_PATH}
docker compose pull backend
docker compose up -d --force-recreate backend
'''
}
}
/* ───────────────────────────────
* 8⃣ Vérification du déploiement
* ─────────────────────────────── */
stage('Health Check') {
steps {
echo "🩺 Vérification du backend après déploiement..."
script {
def domain = (params.ENV == 'dev') ? "api.dev.dsp5-archi-o24a-15m-g3.fr" :
(params.ENV == 'preprod') ? "api.preprod.dsp5-archi-o24a-15m-g3.fr" :
"api.dsp5-archi-o24a-15m-g3.fr"
def maxRetries = 10
def statusCode = "000"
for (int i = 1; i <= maxRetries; i++) {
statusCode = sh(
script: "curl -k -s -o /dev/null -w '%{http_code}' https://${domain}/health || echo 000",
returnStdout: true
).trim()
if (statusCode in ['200', '301', '302']) {
echo "✅ Backend ${params.ENV} opérationnel (HTTP ${statusCode}) après ${i} essai(s)"
break
} else {
echo "⏳ Tentative ${i}/${maxRetries} → HTTP ${statusCode}"
sleep 5
}
}
if (!(statusCode in ['200', '301', '302'])) {
error("❌ Health check échoué sur ${params.ENV} - code HTTP ${statusCode}")
}
}
}
}
}
/* ───────────────────────────────
* 🔚 Post actions
* ─────────────────────────────── */
post {
success {
echo "✅ Pipeline backend ${params.ENV} terminé avec succès !"
}
failure {
echo "❌ Échec du pipeline backend pour ${params.ENV}."
}
}
}