Jenkinsfile 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. pipeline {
  2. agent any
  3. environment {
  4. NODEJS_HOME = "${tool 'NodeJS'}"
  5. PATH = "${env.NODEJS_HOME}/bin:${env.PATH}"
  6. DEV_URL = "dev.foodgame.fr"
  7. PREPROD_URL = "preprod.foodgame.fr"
  8. PROD_URL = "prod.foodgame.fr"
  9. }
  10. stages {
  11. stage('Setup Environment') {
  12. steps {
  13. script {
  14. echo "Environnement détecté : ${env.BRANCH_NAME}"
  15. if (env.BRANCH_NAME == 'test') {
  16. echo "Déploiement sur DEV (${DEV_URL})"
  17. } else if (env.BRANCH_NAME == 'dev') {
  18. echo "Déploiement sur PREPROD (${PREPROD_URL})"
  19. } else if (env.BRANCH_NAME == 'master') {
  20. echo "Déploiement sur PROD (${PROD_URL})"
  21. } else {
  22. error "Branche non prise en charge : ${env.BRANCH_NAME}"
  23. }
  24. }
  25. sh 'npm --version'
  26. }
  27. }
  28. // stage('Checkout Code') {
  29. // steps {
  30. // deleteDir()
  31. // checkout scm
  32. // }
  33. // }
  34. // stage('SonarQube Analysis') {
  35. // steps {
  36. // script {
  37. // def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
  38. // withSonarQubeEnv('SonarQube') { // Nom du serveur SonarQube configuré dans Jenkins
  39. // sh """
  40. // ${scannerHome}/bin/sonar-scanner \
  41. // -Dsonar.projectKey=FatboarProject-${env.BRANCH_NAME} \
  42. // -Dsonar.sources=. \
  43. // -Dsonar.host.url=https://sonarqube.foodgame.fr \
  44. // -Dsonar.login=sqa_d81445ef2d3961cddf7e29e31c1aeb2505aacdbd
  45. // """
  46. // }
  47. // }
  48. // }
  49. // }
  50. // stage('Stop Containers') {
  51. // steps {
  52. // sh '''
  53. // docker ps | grep "workflow_" -v | awk -F " " '{ if(NR>1) print $1}' | xargs docker kill || true
  54. // docker system prune -f
  55. // '''
  56. // }
  57. // }
  58. // stage('Build & Deploy') {
  59. // steps {
  60. // sh '''
  61. // docker-compose stop
  62. // docker-compose build
  63. // docker-compose up -d
  64. // '''
  65. // }
  66. // }
  67. // stage('Push Docker Image (Nexus)') {
  68. // when {
  69. // branch 'master'
  70. // }
  71. // steps {
  72. // withDockerRegistry([credentialsId: 'nexus', url: 'https://nexus.foodgame.fr']) {
  73. // script {
  74. // def dockerImageName = 'workflow_jenkins_1'
  75. // def dockerImageTag = 'latest'
  76. // def nexusRepository = 'fatboar_repo'
  77. // sh """
  78. // docker tag ${dockerImageName}:${dockerImageTag} ${nexusRepository}/${dockerImageName}:${dockerImageTag}
  79. // docker push ${nexusRepository}/${dockerImageName}:${dockerImageTag}
  80. // """
  81. // }
  82. // }
  83. // }
  84. // }
  85. stage('Docker Registry Login, Pull, and Push') {
  86. when {
  87. branch 'master'
  88. }
  89. steps {
  90. script {
  91. def imageName = 'fatboar_master_express_preprod'
  92. def registryUrl = 'nexus.foodgame.fr:8123'
  93. docker.withRegistry("https://${registryUrl}", 'nexus') {
  94. try {
  95. docker.image("${registryUrl}/${imageName}:latest").pull()
  96. } catch (Exception e) {
  97. echo "Image pull failed. Attempting to build and push."
  98. // Build image
  99. def customImage = docker.build("${imageName}")
  100. // Tag and push
  101. customImage.tag("${registryUrl}/${imageName}:latest")
  102. customImage.push()
  103. }
  104. }
  105. }
  106. }
  107. }
  108. stage('Cleanup') {
  109. steps {
  110. echo "Nettoyage terminé pour la branche ${env.BRANCH_NAME}"
  111. }
  112. }
  113. stage('Fin du Pipeline') {
  114. steps {
  115. sh 'echo "Félicitations, le pipeline s\'est terminé avec succès !"'
  116. }
  117. }
  118. }
  119. post {
  120. success {
  121. echo "Pipeline exécuté avec succès pour la branche ${env.BRANCH_NAME}."
  122. }
  123. failure {
  124. echo "Échec du pipeline pour la branche ${env.BRANCH_NAME}."
  125. }
  126. }
  127. }
  128. // node{
  129. // env.NODEJS_HOME = "${tool 'NodeJS'}"
  130. // // on linux / mac
  131. // env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
  132. // // on windows
  133. // //env.PATH="${env.NODEJS_HOME};${env.PATH}"
  134. // sh 'npm --version'
  135. // stage('checkout')
  136. // {
  137. // deleteDir()
  138. // checkout scm
  139. // }
  140. // stage('Stop Containers')
  141. // {
  142. // sh 'docker ps | grep "workflow_" -v | awk -F " " \'{ if(NR>1) print $1}\' |xargs docker kill |xargs docker rm || true'
  143. // sh 'docker system prune -f'
  144. // }
  145. // // stage("Push preprod images to nexus")
  146. // // {
  147. // // docker.withRegistry('http://localhost:8083','885ef60c-9352-489a-bd1c-e4b695747c21')
  148. // // {
  149. // // imageApache.push('latest')
  150. // // imageExpress.push('latest')
  151. // // }*/
  152. // // }
  153. // // stage('SonarQube analysis')
  154. // // {
  155. // // def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
  156. // // withSonarQubeEnv('SonarQube')
  157. // // {
  158. // // // If you have configured more than one global server connection, you can specify its name
  159. // // sh "${scannerHome}/bin/sonar-scanner \
  160. // // -Dsonar.projectKey=FatboarProject \
  161. // // -Dsonar.sources=. \
  162. // // -Dsonar.host.url=https://sonarqube.foodgame.fr \
  163. // // -Dsonar.login=sqp_09ee9072c917af8212864baf0f75c950afc14c64"
  164. // // }
  165. // // }
  166. // stage('Build Docker MEAN Stack(Test Deployment)')
  167. // {
  168. // sh 'docker-compose -v'
  169. // sh 'docker-compose build'
  170. // sh 'docker-compose up -d'
  171. // }
  172. // stage('Fin du Pipeline')
  173. // {
  174. // sh 'echo "Félicitation tout c\'est bien déroulé!"'
  175. // }
  176. // }