the-tip-top-backend/Jenkinsfile

101 lines
3.4 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
triggers {
pollSCM('* * * * *') // Vérifie les changements toutes les minutes, webhook Gitea force un scan immédiat
}
environment {
REGISTRY_URL = "registry.wk-archi-o24a-15m-g3.fr"
IMAGE_NAME = "the-tip-top-backend"
DEPLOY_PATH = "/srv/devops/the-tip-top"
DOMAIN = "api.dsp5-archi-o24a-15m-g3.fr"
}
stages {
stage('Checkout') {
steps {
echo "📦 Récupération du code source depuis Gitea..."
checkout scm
}
}
stage('Build Docker image') {
steps {
echo "🐳 Construction de limage Docker backend..."
sh '''
docker build -t ${REGISTRY_URL}/${IMAGE_NAME}:${BUILD_NUMBER} .
docker tag ${REGISTRY_URL}/${IMAGE_NAME}:${BUILD_NUMBER} ${REGISTRY_URL}/${IMAGE_NAME}:latest
'''
}
}
stage('Push to Registry') {
steps {
echo "📤 Envoi de limage vers le registre 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}:${BUILD_NUMBER}
docker push ${REGISTRY_URL}/${IMAGE_NAME}:latest
'''
}
}
}
stage('Deploy') {
steps {
echo "🚀 Déploiement du backend..."
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
'''
}
}
stage('Health Check') {
steps {
echo "🩺 Vérification du backend après déploiement..."
script {
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}/ || echo 000",
returnStdout: true
).trim()
if (statusCode == '200') {
echo "✅ Backend opérationnel (HTTP ${statusCode}) après ${i} essai(s)"
break
} else {
echo "⏳ Tentative ${i}/${maxRetries} → HTTP ${statusCode}"
sleep 5
}
}
if (statusCode != '200') {
error("❌ Health check échoué - code HTTP ${statusCode}")
}
}
}
}
}
post {
success {
echo "✅ Pipeline backend terminé avec succès !"
}
failure {
echo "❌ Échec du pipeline backend."
}
}
}