Jenkinsfile 12 KB

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