Jenkinsfile 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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=qa_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 'prod'
  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('Cleanup') {
  86. steps {
  87. echo "Nettoyage terminé pour la branche ${env.BRANCH_NAME}"
  88. }
  89. }
  90. stage('Fin du Pipeline') {
  91. steps {
  92. sh 'echo "Félicitations, le pipeline s\'est terminé avec succès !"'
  93. }
  94. }
  95. }
  96. // stage('Test')
  97. // {
  98. // dir("${env.WORKSPACE}/angular-client")
  99. // {
  100. // // sh 'npm i @angular-devkit/build-angular'
  101. // // try
  102. // // {
  103. // // sh 'npm run test-ci'
  104. // // }
  105. // // catch(err)
  106. // // {
  107. // // sh 'echo TEST FAILED'
  108. // // junit 'target/surefire-reports/TESTS-TestSuite.xml/*.xml'
  109. // // throw err
  110. // // }
  111. // def karma = docker.image('trion/ng-cli-karma')
  112. // karma.pull()
  113. // try
  114. // {
  115. // karma.run(' -u $(id -u) -v ${WORKSPACE}:/app trion/ng-cli-karma ')
  116. // karma.inside
  117. // {
  118. // sh 'npm install'
  119. // try
  120. // {
  121. // sh ('karma start karma.conf.js')
  122. // }
  123. // catch(err)
  124. // {
  125. // sh 'echo TEST FAILED'
  126. // step([$class: 'JUnitResultArchiver', testResults: 'target/surefire-reports/TESTS-TestSuite.xml/*.xml', healthScaleFactor: 1.0])
  127. // throw err
  128. // }
  129. // sh 'echo DO SOMETHING ELSE AFTER TEST'
  130. // }
  131. // sh 'ls -al '
  132. // }
  133. // catch(err)
  134. // {
  135. // sh 'echo RUN DOCKER FAILED'
  136. // throw err
  137. // }
  138. // }
  139. // }
  140. // def imageApache = stage("Build apache preprod")
  141. // {
  142. // docker.build("server-apache-preprod", "--no-cache --label traefik.enable=true --label traefik.http.routers.server-apache-preprod.rule='Host(`angular-preprod.foodgame.fr`)' --label traefik.http.routers.server-apache-preprod.entrypoints=websecure --label traefik.http.routers.server-apache-preprod.tls.certresolver=myresolver -f build/docker/httpd/Dockerfile .")
  143. // }
  144. // def imageExpress = stage("Build express preprod")
  145. // {
  146. // docker.build("server-express-preprod", "--no-cache -f express-server/Dockerfile express-server")
  147. // }
  148. /*stage("Run Container express preprod")
  149. {
  150. containerSql=imageExpress.run("--name server-express-preprod -v /var/log:/var/www/app/log/root --link database_preprod:mongo --network=web")
  151. }
  152. stage("Run Container apache preprod")
  153. {
  154. containerApache=imageApache.run("--name server-apache-preprod --link server-mysql-preprod:mysql --network=web")
  155. }*/
  156. /*stage("Run Container express preprod")
  157. {
  158. containerSql=imageExpress.run("--name server-express-preprod -v /home/projet/fatboar/${e}/mysql:/var/lib/mysql --network=web")
  159. }
  160. stage("Run Container apache preprod")
  161. {
  162. containerApache=imageApache.run("--name server-apache-preprod --link server-mysql-preprod:mysql --network=web")
  163. }*/
  164. // stage("Push preprod images to nexus")
  165. // {
  166. // /*docker.withRegistry('http://localhost:8083','885ef60c-9352-489a-bd1c-e4b695747c21')
  167. // {
  168. // imageApache.push('latest')
  169. // imageExpress.push('latest')
  170. // }*/
  171. // }
  172. // stage('SonarQube analysis')
  173. // {
  174. // def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
  175. // withSonarQubeEnv('SonarQube')
  176. // {
  177. // // If you have configured more than one global server connection, you can specify its name
  178. // sh "${scannerHome}/bin/sonar-scanner \
  179. // -Dsonar.projectKey=FatboarProject \
  180. // -Dsonar.sources=. \
  181. // -Dsonar.host.url=https://sonarqube.foodgame.fr \
  182. // -Dsonar.login=sqp_09ee9072c917af8212864baf0f75c950afc14c64"
  183. // }
  184. // }
  185. stage('Build Docker MEAN Stack(PreProduction Deployment)')
  186. {
  187. sh 'docker-compose -v'
  188. sh 'docker-compose build'
  189. sh 'docker-compose up -d'
  190. }
  191. stage('Fin du Pipeline')
  192. {
  193. sh 'echo "Félicitation tout c\'est bien déroulé!"'
  194. }
  195. }