Jenkinsfile 12 KB

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