Jenkinsfile 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 == 'dev') {
  16. echo "Déploiement sur DEV (${DEV_URL})"
  17. } else if (env.BRANCH_NAME == 'preprod') {
  18. echo "Déploiement sur PREPROD (${PREPROD_URL})"
  19. } else if (env.BRANCH_NAME == 'prod') {
  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=sqp_09ee9072c917af8212864baf0f75c950afc14c64
  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. <<<<<<< HEAD
  97. post {
  98. success {
  99. echo "Pipeline exécuté avec succès pour la branche ${env.BRANCH_NAME}."
  100. }
  101. failure {
  102. echo "Échec du pipeline pour la branche ${env.BRANCH_NAME}."
  103. }
  104. }
  105. }
  106. // node{
  107. // env.NODEJS_HOME = "${tool 'NodeJS'}"
  108. // // on linux / mac
  109. // env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
  110. // // on windows
  111. // //env.PATH="${env.NODEJS_HOME};${env.PATH}"
  112. // sh 'npm --version'
  113. // stage('checkout')
  114. // {
  115. // deleteDir()
  116. // checkout scm
  117. // }
  118. // stage('Stop Containers')
  119. // {
  120. // sh 'docker ps | grep "workflow_" -v | awk -F " " \'{ if(NR>1) print $1}\' |xargs docker kill |xargs docker rm || true'
  121. // sh 'docker system prune -f'
  122. // }
  123. // // stage("Push preprod images to nexus")
  124. // // {
  125. // // docker.withRegistry('http://localhost:8083','885ef60c-9352-489a-bd1c-e4b695747c21')
  126. // // {
  127. // // imageApache.push('latest')
  128. // // imageExpress.push('latest')
  129. // // }*/
  130. // // }
  131. // // stage('SonarQube analysis')
  132. // // {
  133. // // def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
  134. // // withSonarQubeEnv('SonarQube')
  135. // // {
  136. // // // If you have configured more than one global server connection, you can specify its name
  137. // // sh "${scannerHome}/bin/sonar-scanner \
  138. // // -Dsonar.projectKey=FatboarProject \
  139. // // -Dsonar.sources=. \
  140. // // -Dsonar.host.url=https://sonarqube.foodgame.fr \
  141. // // -Dsonar.login=sqp_09ee9072c917af8212864baf0f75c950afc14c64"
  142. // // }
  143. // // }
  144. // stage('Build Docker MEAN Stack(Test Deployment)')
  145. // {
  146. // sh 'docker-compose -v'
  147. // sh 'docker-compose build'
  148. // sh 'docker-compose up -d'
  149. // }
  150. // stage('Fin du Pipeline')
  151. // {
  152. // sh 'echo "Félicitation tout c\'est bien déroulé!"'
  153. // }
  154. =======
  155. // stage('Test')
  156. // {
  157. // dir("${env.WORKSPACE}/angular-client")
  158. // {
  159. // // sh 'npm i @angular-devkit/build-angular'
  160. // // try
  161. // // {
  162. // // sh 'npm run test-ci'
  163. // // }
  164. // // catch(err)
  165. // // {
  166. // // sh 'echo TEST FAILED'
  167. // // junit 'target/surefire-reports/TESTS-TestSuite.xml/*.xml'
  168. // // throw err
  169. // // }
  170. // def karma = docker.image('trion/ng-cli-karma')
  171. // karma.pull()
  172. // try
  173. // {
  174. // karma.run(' -u $(id -u) -v ${WORKSPACE}:/app trion/ng-cli-karma ')
  175. // karma.inside
  176. // {
  177. // sh 'npm install'
  178. // try
  179. // {
  180. // sh ('karma start karma.conf.js')
  181. // }
  182. // catch(err)
  183. // {
  184. // sh 'echo TEST FAILED'
  185. // step([$class: 'JUnitResultArchiver', testResults: 'target/surefire-reports/TESTS-TestSuite.xml/*.xml', healthScaleFactor: 1.0])
  186. // throw err
  187. // }
  188. // sh 'echo DO SOMETHING ELSE AFTER TEST'
  189. // }
  190. // sh 'ls -al '
  191. // }
  192. // catch(err)
  193. // {
  194. // sh 'echo RUN DOCKER FAILED'
  195. // throw err
  196. // }
  197. // }
  198. // }
  199. // def imageApache = stage("Build apache preprod")
  200. // {
  201. // 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 .")
  202. // }
  203. // def imageExpress = stage("Build express preprod")
  204. // {
  205. // docker.build("server-express-preprod", "--no-cache -f express-server/Dockerfile express-server")
  206. // }
  207. /*stage("Run Container express preprod")
  208. {
  209. containerSql=imageExpress.run("--name server-express-preprod -v /var/log:/var/www/app/log/root --link database_preprod:mongo --network=web")
  210. }
  211. stage("Run Container apache preprod")
  212. {
  213. containerApache=imageApache.run("--name server-apache-preprod --link server-mysql-preprod:mysql --network=web")
  214. }*/
  215. /*stage("Run Container express preprod")
  216. {
  217. containerSql=imageExpress.run("--name server-express-preprod -v /home/projet/fatboar/${e}/mysql:/var/lib/mysql --network=web")
  218. }
  219. stage("Run Container apache preprod")
  220. {
  221. containerApache=imageApache.run("--name server-apache-preprod --link server-mysql-preprod:mysql --network=web")
  222. }*/
  223. // stage("Push preprod images to nexus")
  224. // {
  225. // /*docker.withRegistry('http://localhost:8083','885ef60c-9352-489a-bd1c-e4b695747c21')
  226. // {
  227. // imageApache.push('latest')
  228. // imageExpress.push('latest')
  229. // }*/
  230. // }
  231. // stage('SonarQube analysis')
  232. // {
  233. // def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
  234. // withSonarQubeEnv('SonarQube')
  235. // {
  236. // // If you have configured more than one global server connection, you can specify its name
  237. // sh "${scannerHome}/bin/sonar-scanner \
  238. // -Dsonar.projectKey=FatboarProject \
  239. // -Dsonar.sources=. \
  240. // -Dsonar.host.url=https://sonarqube.foodgame.fr \
  241. // -Dsonar.login=sqp_09ee9072c917af8212864baf0f75c950afc14c64"
  242. // }
  243. // }
  244. stage('Build Docker MEAN Stack(PreProduction Deployment)')
  245. {
  246. sh 'docker-compose -v'
  247. sh 'docker-compose build'
  248. sh 'docker-compose up -d'
  249. }
  250. stage('Fin du Pipeline')
  251. {
  252. sh 'echo "Félicitation tout c\'est bien déroulé!"'
  253. }
  254. }
  255. >>>>>>> jenkinsfile