the-tip-top-backend/Jenkinsfile
soufiane caff01fc4b feat: add automatic deployment triggers to Jenkinsfile
Configure Jenkins pipeline to trigger automatically on git push:
- Add pollSCM trigger (checks every minute)
- Add Generic Webhook trigger for Gitea integration
- Add comprehensive webhook setup documentation

This enables CI/CD automation where the pipeline starts
automatically when code is pushed to the repository.

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

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

151 lines
5.1 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..."
sh '''
npm ci
npm run lint || echo "⚠️ Erreurs de lint détectées"
npm test || echo "⚠️ Tests échoués — ignorés pour le déploiement"
'''
}
}
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}."
}
}
}