| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- pipeline {
- agent any
- environment {
- NODEJS_HOME = "${tool 'NodeJS'}"
- PATH = "${env.NODEJS_HOME}/bin:${env.PATH}"
-
- }
- stages {
- stage('Select Environment') {
- steps {
- script {
- if (env.BRANCH_NAME == 'feature') {
- env.ENV = 'dev'
- env.COMPOSE_FILE = 'docker-compose.dev.yml'
- env.URL = "dev.foodgame.fr"
- env.BACKUP_CONTAINER = "mongodb-backup-dev"
- } else if (env.BRANCH_NAME == 'dev') {
- env.ENV = 'preprod'
- env.COMPOSE_FILE = 'docker-compose.preprod.yml'
- env.URL = "preprod.foodgame.fr"
- env.BACKUP_CONTAINER = "mongodb-backup-preprod"
- } else if (env.BRANCH_NAME == 'master') {
- env.ENV = 'prod'
- env.COMPOSE_FILE = 'docker-compose.prod.yml'
- env.URL = "fatboar.foodgame.fr"
- env.BACKUP_CONTAINER = "mongodb-backup-prod"
- } else {
- error "Branche non gérée : ${env.BRANCH_NAME}"
- }
- echo "Déploiement sur l'environnement ${env.ENV} (${env.URL})"
- }
- }
- }
- // stage('Install Dependencies & Test') {
- // steps {
- // withEnv(["PATH=${tool 'NodeJS'}/bin:${env.PATH}"]) {
- // sh 'npm install --legacy-peer-deps'
- // sh 'npm run test -- --no-watch --no-progress --browsers=ChromeHeadless'
- // }
- // }
- // }
- stage('SonarQube Analysis') {
- steps {
- script {
- def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
- withSonarQubeEnv('SonarQube') {
- sh """
- ${scannerHome}/bin/sonar-scanner \
- -Dsonar.projectKey=FatboarProject-${env.BRANCH_NAME} \
- -Dsonar.sources=. \
- -Dsonar.host.url=https://sonarqube.foodgame.fr \
- -Dsonar.login=sqa_9ec3588a80a0b8458d9273dbb6eb7f6ae91b446a
- """
- }
- }
- }
- }
- // stage('Stop & Clean Containers') {
- // steps {
- // sh """
- // docker ps | grep "workflow_" -v | awk '{if(NR>1) print \$1}' | xargs docker kill || true
- // docker-compose -f ${env.COMPOSE_FILE} down || true
- // docker system prune -f
- // """
- // }
- // }
- stage('Build & Deploy') {
- steps {
- sh """
- docker-compose -f ${env.COMPOSE_FILE} build --no-cache
- docker-compose -f ${env.COMPOSE_FILE} up -d --force-recreate --remove-orphans
- """
- script {
- echo "Lancement du backup sur le container ${env.BACKUP_CONTAINER}"
- def backupCmd = "docker exec ${env.BACKUP_CONTAINER} /backup.sh || echo '⚠️ Backup ${env.ENV.toUpperCase()} échoué'"
- sh backupCmd
- }
- }
- }
- stage('Push to Private Registry (Optional)') {
- when {
- anyOf {
- branch 'dev'
- branch 'master'
- }
- }
- steps {
- script {
- def registryUrl = 'nexus.foodgame.fr:8123'
- def imageName = 'grafana/tns-db'
- def imageVersion = 'latest'
-
- docker.withRegistry("https://${registryUrl}", 'nexus') {
- try {
- //Try pulling the image from the registry
- echo "Trying to pull image: ${registryUrl}/${imageName}:${imageVersion}"
- docker.image("${registryUrl}/${imageName}:${imageVersion}").pull()
- } catch (Exception e) {
- echo "Image pull failed. Attempting to build and push."
- //Pull base image from Docker Hub
- echo "Pulling base image: ${imageName}:${imageVersion}"
- sh "docker pull ${imageName}:${imageVersion}"
-
- //Tag the image for the private registry
- echo "Tagging image for private registry"
- sh "docker tag ${imageName}:${imageVersion} ${registryUrl}/${imageName}:${imageVersion}"
-
- //Push the tagged image to the private registry
- echo "Pushing image to private registry"
- sh "docker push ${registryUrl}/${imageName}:${imageVersion}"
- }
- }
- }
- }
- }
- }
- post {
- success {
- echo "✅ Déploiement réussi sur ${env.ENV} !"
- }
- failure {
- echo "❌ Échec du pipeline sur ${env.ENV}."
- }
- }
- }
- // pipeline {
- // agent any
- // environment {
- // NODEJS_HOME = "${tool 'NodeJS'}"
- // PATH = "${env.NODEJS_HOME}/bin:${env.PATH}"
- // DEV_URL = "dev.foodgame.fr"
- // dev_URL = "dev.foodgame.fr"
- // PROD_URL = "prod.foodgame.fr"
- // }
- // stages {
- // stage('Setup Environment') {
- // steps {
- // script {
- // echo "Environnement détecté : ${env.BRANCH_NAME}"
- // if (env.BRANCH_NAME == 'test') {
- // echo "Déploiement sur DEV (${DEV_URL})"
- // } else if (env.BRANCH_NAME == 'dev') {
- // echo "Déploiement sur dev (${dev_URL})"
- // } else if (env.BRANCH_NAME == 'master') {
- // echo "Déploiement sur PROD (${PROD_URL})"
- // } else {
- // error "Branche non prise en charge : ${env.BRANCH_NAME}"
- // }
- // }
- // sh 'npm --version'
- // }
- // }
- // stage('Checkout Code') {
- // steps {
- // deleteDir()
- // checkout scm
- // }
- // }
-
- // stage('Stop Containers') {
- // steps {
- // sh '''
- // docker ps | grep "workflow_" -v | awk -F " " '{ if(NR>1) print $1}' | xargs docker kill || true
- // docker system prune -f
- // '''
- // }
- // }
- // stage('Build & Deploy') {
- // steps {
- // sh '''
- // docker-compose stop
- // docker-compose build
- // docker-compose up -d
- // '''
- // }
- // }
- // // stage('Push Docker Image (Nexus)') {
- // // when {
- // // branch 'master'
- // // }
- // // steps {
- // // withDockerRegistry([credentialsId: 'nexus', url: 'https://nexus.foodgame.fr']) {
- // // script {
- // // def dockerImageName = 'workflow_jenkins_1'
- // // def dockerImageTag = 'latest'
- // // def nexusRepository = 'fatboar_repo'
- // // sh """
- // // docker tag ${dockerImageName}:${dockerImageTag} ${nexusRepository}/${dockerImageName}:${dockerImageTag}
- // // docker push ${nexusRepository}/${dockerImageName}:${dockerImageTag}
- // // """
- // // }
- // // }
- // // }
- // // }
-
- // stage('Docker Registry Login, Pull, and Push') {
- // when {
- // branch 'dev'
- // }
- // steps {
- // script {
- // def registryUrl = 'nexus.foodgame.fr:8123'
- // def imageName = 'grafana/tns-db'
- // def imageVersion = 'latest'
-
- // docker.withRegistry("https://${registryUrl}", 'nexus') {
- // try {
- // Try pulling the image from the registry
- // echo "Trying to pull image: ${registryUrl}/${imageName}:${imageVersion}"
- // docker.image("${registryUrl}/${imageName}:${imageVersion}").pull()
- // } catch (Exception e) {
- // echo "Image pull failed. Attempting to build and push."
- // Pull base image from Docker Hub
- // echo "Pulling base image: ${imageName}:${imageVersion}"
- // sh "docker pull ${imageName}:${imageVersion}"
-
- // Tag the image for the private registry
- // echo "Tagging image for private registry"
- // sh "docker tag ${imageName}:${imageVersion} ${registryUrl}/${imageName}:${imageVersion}"
-
- // Push the tagged image to the private registry
- // echo "Pushing image to private registry"
- // sh "docker push ${registryUrl}/${imageName}:${imageVersion}"
- // }
- // }
- // }
- // }
- // }
-
- // stage('Cleanup') {
- // steps {
- // echo "Nettoyage terminé pour la branche ${env.BRANCH_NAME}"
- // }
- // }
- // stage('Fin du Pipeline') {
- // steps {
- // sh 'echo "Félicitations, le pipeline s\'est terminé avec succès !"'
- // }
- // }
- // }
- // post {
- // success {
- // echo "Pipeline exécuté avec succès pour la branche ${env.BRANCH_NAME}."
- // }
- // failure {
- // echo "Échec du pipeline pour la branche ${env.BRANCH_NAME}."
- // }
- // }
- // }
- // // node{
- // // env.NODEJS_HOME = "${tool 'NodeJS'}"
- // // // on linux / mac
- // // env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
- // // // on windows
- // // //env.PATH="${env.NODEJS_HOME};${env.PATH}"
- // // sh 'npm --version'
- // // stage('checkout')
- // // {
- // // deleteDir()
- // // checkout scm
- // // }
-
- // // stage('Stop Containers')
- // // {
- // // sh 'docker ps | grep "workflow_" -v | awk -F " " \'{ if(NR>1) print $1}\' |xargs docker kill |xargs docker rm || true'
- // // sh 'docker system prune -f'
- // // }
-
-
- // // // stage("Push dev images to nexus")
- // // // {
- // // // docker.withRegistry('http://localhost:8083','885ef60c-9352-489a-bd1c-e4b695747c21')
- // // // {
- // // // imageApache.push('latest')
- // // // imageExpress.push('latest')
- // // // }*/
- // // // }
- // // // stage('SonarQube analysis')
- // // // {
- // // // def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
- // // // withSonarQubeEnv('SonarQube')
- // // // {
- // // // // If you have configured more than one global server connection, you can specify its name
- // // // sh "${scannerHome}/bin/sonar-scanner \
- // // // -Dsonar.projectKey=FatboarProject \
- // // // -Dsonar.sources=. \
- // // // -Dsonar.host.url=https://sonarqube.foodgame.fr \
- // // // -Dsonar.login=sqp_09ee9072c917af8212864baf0f75c950afc14c64"
- // // // }
- // // // }
-
- // // stage('Build Docker MEAN Stack(Test Deployment)')
- // // {
- // // sh 'docker-compose -v'
- // // sh 'docker-compose build'
- // // sh 'docker-compose up -d'
- // // }
-
- // // stage('Fin du Pipeline')
- // // {
- // // sh 'echo "Félicitation tout c\'est bien déroulé!"'
- // // }
- // // }
|