Jenkinsfile 12 KB

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