121 lines
3.9 KiB
Groovy
121 lines
3.9 KiB
Groovy
pipeline {
|
|
agent any
|
|
|
|
parameters {
|
|
choice(
|
|
name: 'ENV',
|
|
choices: ['dev', 'preprod', 'prod'],
|
|
description: 'Choisir lenvironnement de déploiement'
|
|
)
|
|
}
|
|
|
|
environment {
|
|
REGISTRY_URL = "registry.wk-archi-o24a-15m-g3.fr"
|
|
IMAGE_NAME = "the-tip-top-backend"
|
|
}
|
|
|
|
stages {
|
|
|
|
// Stage 1: Initialisation
|
|
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}"
|
|
}
|
|
}
|
|
}
|
|
|
|
// Stage 2: Checkout du code
|
|
stage('Checkout') {
|
|
steps {
|
|
checkout scm
|
|
}
|
|
}
|
|
|
|
// Stage 3: Tests et Qualité
|
|
stage('Tests & Qualité') {
|
|
agent {
|
|
docker {
|
|
image 'node:18-alpine'
|
|
args '-u root'
|
|
}
|
|
}
|
|
steps {
|
|
sh '''
|
|
npm ci
|
|
npm run lint || echo "Erreurs de lint détectées"
|
|
npm test || echo "Tests échoués"
|
|
'''
|
|
}
|
|
}
|
|
|
|
// Stage 4: Build Docker image
|
|
stage('Build Docker image') {
|
|
steps {
|
|
dir('the-tip-top-backend') {
|
|
sh """
|
|
docker build -t ${REGISTRY_URL}/${IMAGE_NAME}:${TAG} .
|
|
docker tag ${REGISTRY_URL}/${IMAGE_NAME}:${TAG} ${REGISTRY_URL}/${IMAGE_NAME}:latest
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
|
|
// Stage 5: Push vers le registre privé
|
|
stage('Push to Registry') {
|
|
steps {
|
|
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 6: Déploiement
|
|
stage('Deploy') {
|
|
steps {
|
|
sh """
|
|
cd "${DEPLOY_PATH}"
|
|
docker compose pull backend
|
|
docker compose up -d --force-recreate backend
|
|
"""
|
|
}
|
|
}
|
|
|
|
// Stage 7: Health Check
|
|
stage('Health Check') {
|
|
steps {
|
|
script {
|
|
def domain = (env.ENV == 'dev') ? "api.dev.dsp5-archi-o24a-15m-g3.fr" :
|
|
(env.ENV == 'preprod') ? "api.preprod.dsp5-archi-o24a-15m-g3.fr" :
|
|
"api.dsp5-archi-o24a-15m-g3.fr"
|
|
|
|
def statusCode = "000"
|
|
for (int i = 1; i <= 10; 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 ${env.ENV} OK (${statusCode})"
|
|
return
|
|
}
|
|
sleep 5
|
|
}
|
|
error("Health check échoué (${statusCode})")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|