Jenkinsfile 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. pipeline {
  2. agent any
  3. environment {
  4. NODEJS_HOME = "${tool 'NodeJS'}"
  5. PATH = "${env.NODEJS_HOME}/bin:${env.PATH}"
  6. }
  7. stages {
  8. stage('Select Environment') {
  9. steps {
  10. script {
  11. if (env.BRANCH_NAME == 'feature') {
  12. env.ENV = 'dev'
  13. env.COMPOSE_FILE = 'docker-compose.dev.yml'
  14. env.URL = "dev.foodgame.fr"
  15. env.BACKUP_CONTAINER = "mongodb-backup-dev"
  16. } else if (env.BRANCH_NAME == 'dev') {
  17. env.ENV = 'preprod'
  18. env.COMPOSE_FILE = 'docker-compose.preprod.yml'
  19. env.URL = "preprod.foodgame.fr"
  20. env.BACKUP_CONTAINER = "mongodb-backup-preprod"
  21. } else if (env.BRANCH_NAME == 'master') {
  22. env.ENV = 'prod'
  23. env.COMPOSE_FILE = 'docker-compose.prod.yml'
  24. env.URL = "fatboar.foodgame.fr"
  25. env.BACKUP_CONTAINER = "mongodb-backup-prod"
  26. } else {
  27. error "Branche non gérée : ${env.BRANCH_NAME}"
  28. }
  29. echo "Déploiement sur l'environnement ${env.ENV} (${env.URL})"
  30. }
  31. }
  32. }
  33. // stage('Install Dependencies & Test') {
  34. // steps {
  35. // withEnv(["PATH=${tool 'NodeJS'}/bin:${env.PATH}"]) {
  36. // sh 'npm install --legacy-peer-deps'
  37. // sh 'npm run test -- --no-watch --no-progress --browsers=ChromeHeadless'
  38. // }
  39. // }
  40. // }
  41. stage('SonarQube Analysis') {
  42. steps {
  43. script {
  44. def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
  45. withSonarQubeEnv('SonarQube') {
  46. sh """
  47. ${scannerHome}/bin/sonar-scanner \
  48. -Dsonar.projectKey=FatboarProject-${env.BRANCH_NAME} \
  49. -Dsonar.sources=. \
  50. -Dsonar.host.url=https://sonarqube.foodgame.fr \
  51. -Dsonar.login=sqa_9ec3588a80a0b8458d9273dbb6eb7f6ae91b446a
  52. """
  53. }
  54. }
  55. }
  56. }
  57. // stage('Stop & Clean Containers') {
  58. // steps {
  59. // sh """
  60. // docker ps | grep "workflow_" -v | awk '{if(NR>1) print \$1}' | xargs docker kill || true
  61. // docker-compose -f ${env.COMPOSE_FILE} down || true
  62. // docker system prune -f
  63. // """
  64. // }
  65. // }
  66. // stage('Build & Deploy') {
  67. // steps {
  68. // sh """
  69. // docker-compose -f ${env.COMPOSE_FILE} build --no-cache
  70. // docker-compose -f ${env.COMPOSE_FILE} up -d --force-recreate --remove-orphans
  71. // """
  72. // script {
  73. // echo "Lancement du backup sur le container ${env.BACKUP_CONTAINER}"
  74. // def backupCmd = "docker exec ${env.BACKUP_CONTAINER} /backup.sh || echo '⚠️ Backup ${env.ENV.toUpperCase()} échoué'"
  75. // sh backupCmd
  76. // }
  77. // }
  78. // }
  79. stage('Push to Private Registry (Optional)') {
  80. when {
  81. anyOf {
  82. branch 'dev'
  83. branch 'master'
  84. }
  85. }
  86. steps {
  87. script {
  88. def registryUrl = 'nexus.foodgame.fr:8123'
  89. def imageName = 'grafana/tns-db'
  90. def imageVersion = 'latest'
  91. docker.withRegistry("https://${registryUrl}", 'nexus') {
  92. try {
  93. //Try pulling the image from the registry
  94. echo "Trying to pull image: ${registryUrl}/${imageName}:${imageVersion}"
  95. docker.image("${registryUrl}/${imageName}:${imageVersion}").pull()
  96. } catch (Exception e) {
  97. echo "Image pull failed. Attempting to build and push."
  98. //Pull base image from Docker Hub
  99. echo "Pulling base image: ${imageName}:${imageVersion}"
  100. sh "docker pull ${imageName}:${imageVersion}"
  101. //Tag the image for the private registry
  102. echo "Tagging image for private registry"
  103. sh "docker tag ${imageName}:${imageVersion} ${registryUrl}/${imageName}:${imageVersion}"
  104. //Push the tagged image to the private registry
  105. echo "Pushing image to private registry"
  106. sh "docker push ${registryUrl}/${imageName}:${imageVersion}"
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. post {
  114. success {
  115. echo "✅ Déploiement réussi sur ${env.ENV} !"
  116. }
  117. failure {
  118. echo "❌ Échec du pipeline sur ${env.ENV}."
  119. }
  120. }
  121. }
  122. // pipeline {
  123. // agent any
  124. // environment {
  125. // NODEJS_HOME = "${tool 'NodeJS'}"
  126. // PATH = "${env.NODEJS_HOME}/bin:${env.PATH}"
  127. // DEV_URL = "dev.foodgame.fr"
  128. // dev_URL = "dev.foodgame.fr"
  129. // PROD_URL = "prod.foodgame.fr"
  130. // }
  131. // stages {
  132. // stage('Setup Environment') {
  133. // steps {
  134. // script {
  135. // echo "Environnement détecté : ${env.BRANCH_NAME}"
  136. // if (env.BRANCH_NAME == 'test') {
  137. // echo "Déploiement sur DEV (${DEV_URL})"
  138. // } else if (env.BRANCH_NAME == 'dev') {
  139. // echo "Déploiement sur dev (${dev_URL})"
  140. // } else if (env.BRANCH_NAME == 'master') {
  141. // echo "Déploiement sur PROD (${PROD_URL})"
  142. // } else {
  143. // error "Branche non prise en charge : ${env.BRANCH_NAME}"
  144. // }
  145. // }
  146. // sh 'npm --version'
  147. // }
  148. // }
  149. // stage('Checkout Code') {
  150. // steps {
  151. // deleteDir()
  152. // checkout scm
  153. // }
  154. // }
  155. // stage('Stop Containers') {
  156. // steps {
  157. // sh '''
  158. // docker ps | grep "workflow_" -v | awk -F " " '{ if(NR>1) print $1}' | xargs docker kill || true
  159. // docker system prune -f
  160. // '''
  161. // }
  162. // }
  163. // stage('Build & Deploy') {
  164. // steps {
  165. // sh '''
  166. // docker-compose stop
  167. // docker-compose build
  168. // docker-compose up -d
  169. // '''
  170. // }
  171. // }
  172. // // stage('Push Docker Image (Nexus)') {
  173. // // when {
  174. // // branch 'master'
  175. // // }
  176. // // steps {
  177. // // withDockerRegistry([credentialsId: 'nexus', url: 'https://nexus.foodgame.fr']) {
  178. // // script {
  179. // // def dockerImageName = 'workflow_jenkins_1'
  180. // // def dockerImageTag = 'latest'
  181. // // def nexusRepository = 'fatboar_repo'
  182. // // sh """
  183. // // docker tag ${dockerImageName}:${dockerImageTag} ${nexusRepository}/${dockerImageName}:${dockerImageTag}
  184. // // docker push ${nexusRepository}/${dockerImageName}:${dockerImageTag}
  185. // // """
  186. // // }
  187. // // }
  188. // // }
  189. // // }
  190. // stage('Docker Registry Login, Pull, and Push') {
  191. // when {
  192. // branch 'dev'
  193. // }
  194. // steps {
  195. // script {
  196. // def registryUrl = 'nexus.foodgame.fr:8123'
  197. // def imageName = 'grafana/tns-db'
  198. // def imageVersion = 'latest'
  199. // docker.withRegistry("https://${registryUrl}", 'nexus') {
  200. // try {
  201. // Try pulling the image from the registry
  202. // echo "Trying to pull image: ${registryUrl}/${imageName}:${imageVersion}"
  203. // docker.image("${registryUrl}/${imageName}:${imageVersion}").pull()
  204. // } catch (Exception e) {
  205. // echo "Image pull failed. Attempting to build and push."
  206. // Pull base image from Docker Hub
  207. // echo "Pulling base image: ${imageName}:${imageVersion}"
  208. // sh "docker pull ${imageName}:${imageVersion}"
  209. // Tag the image for the private registry
  210. // echo "Tagging image for private registry"
  211. // sh "docker tag ${imageName}:${imageVersion} ${registryUrl}/${imageName}:${imageVersion}"
  212. // Push the tagged image to the private registry
  213. // echo "Pushing image to private registry"
  214. // sh "docker push ${registryUrl}/${imageName}:${imageVersion}"
  215. // }
  216. // }
  217. // }
  218. // }
  219. // }
  220. // stage('Cleanup') {
  221. // steps {
  222. // echo "Nettoyage terminé pour la branche ${env.BRANCH_NAME}"
  223. // }
  224. // }
  225. // stage('Fin du Pipeline') {
  226. // steps {
  227. // sh 'echo "Félicitations, le pipeline s\'est terminé avec succès !"'
  228. // }
  229. // }
  230. // }
  231. // post {
  232. // success {
  233. // echo "Pipeline exécuté avec succès pour la branche ${env.BRANCH_NAME}."
  234. // }
  235. // failure {
  236. // echo "Échec du pipeline pour la branche ${env.BRANCH_NAME}."
  237. // }
  238. // }
  239. // }
  240. // // node{
  241. // // env.NODEJS_HOME = "${tool 'NodeJS'}"
  242. // // // on linux / mac
  243. // // env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
  244. // // // on windows
  245. // // //env.PATH="${env.NODEJS_HOME};${env.PATH}"
  246. // // sh 'npm --version'
  247. // // stage('checkout')
  248. // // {
  249. // // deleteDir()
  250. // // checkout scm
  251. // // }
  252. // // stage('Stop Containers')
  253. // // {
  254. // // sh 'docker ps | grep "workflow_" -v | awk -F " " \'{ if(NR>1) print $1}\' |xargs docker kill |xargs docker rm || true'
  255. // // sh 'docker system prune -f'
  256. // // }
  257. // // // stage("Push dev images to nexus")
  258. // // // {
  259. // // // docker.withRegistry('http://localhost:8083','885ef60c-9352-489a-bd1c-e4b695747c21')
  260. // // // {
  261. // // // imageApache.push('latest')
  262. // // // imageExpress.push('latest')
  263. // // // }*/
  264. // // // }
  265. // // // stage('SonarQube analysis')
  266. // // // {
  267. // // // def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
  268. // // // withSonarQubeEnv('SonarQube')
  269. // // // {
  270. // // // // If you have configured more than one global server connection, you can specify its name
  271. // // // sh "${scannerHome}/bin/sonar-scanner \
  272. // // // -Dsonar.projectKey=FatboarProject \
  273. // // // -Dsonar.sources=. \
  274. // // // -Dsonar.host.url=https://sonarqube.foodgame.fr \
  275. // // // -Dsonar.login=sqp_09ee9072c917af8212864baf0f75c950afc14c64"
  276. // // // }
  277. // // // }
  278. // // stage('Build Docker MEAN Stack(Test Deployment)')
  279. // // {
  280. // // sh 'docker-compose -v'
  281. // // sh 'docker-compose build'
  282. // // sh 'docker-compose up -d'
  283. // // }
  284. // // stage('Fin du Pipeline')
  285. // // {
  286. // // sh 'echo "Félicitation tout c\'est bien déroulé!"'
  287. // // }
  288. // // }