Selaa lähdekoodia

ajout du stage "push" dans jenkinsfile

henri carmelo 11 kuukautta sitten
vanhempi
commit
33f75e747d
1 muutettua tiedostoa jossa 129 lisäystä ja 165 poistoa
  1. 129 165
      Jenkinsfile

+ 129 - 165
Jenkinsfile

@@ -26,48 +26,48 @@ pipeline {
             }
         }
 
-        stage('Checkout Code') {
-            steps {
-                deleteDir()
-                checkout scm
-            }
-        }
+        // stage('Checkout Code') {
+        //     steps {
+        //         deleteDir()
+        //         checkout scm
+        //     }
+        // }
 
-        stage('SonarQube Analysis') {
-            steps {
-                script {
-                    def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
-                    withSonarQubeEnv('SonarQube') { // Nom du serveur SonarQube configuré dans Jenkins
-                        sh """
-                        ${scannerHome}/bin/sonar-scanner \
-                        -Dsonar.projectKey=FatboarProject-${env.BRANCH_NAME} \
-                        -Dsonar.sources=. \
-                        -Dsonar.host.url=https://sonarqube.foodgame.fr \
-                        -Dsonar.login=sqa_d81445ef2d3961cddf7e29e31c1aeb2505aacdbd
-                        """
-                    }
-                }
-            }
-        }
+        // stage('SonarQube Analysis') {
+        //     steps {
+        //         script {
+        //             def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
+        //             withSonarQubeEnv('SonarQube') { // Nom du serveur SonarQube configuré dans Jenkins
+        //                 sh """
+        //                 ${scannerHome}/bin/sonar-scanner \
+        //                 -Dsonar.projectKey=FatboarProject-${env.BRANCH_NAME} \
+        //                 -Dsonar.sources=. \
+        //                 -Dsonar.host.url=https://sonarqube.foodgame.fr \
+        //                 -Dsonar.login=sqa_d81445ef2d3961cddf7e29e31c1aeb2505aacdbd
+        //                 """
+        //             }
+        //         }
+        //     }
+        // }
 
-        stage('Stop Containers') {
-            steps {
-                sh '''
-                docker ps | grep "workflow_" -v | awk -F " " '{ if(NR>1) print $1}' | xargs docker kill || true
-                docker system prune -f
-                '''
-            }
-        }
+        // stage('Stop Containers') {
+        //     steps {
+        //         sh '''
+        //         docker ps | grep "workflow_" -v | awk -F " " '{ if(NR>1) print $1}' | xargs docker kill || true
+        //         docker system prune -f
+        //         '''
+        //     }
+        // }
 
-        stage('Build & Deploy') {
-            steps {
-                sh '''
-                docker-compose stop
-                docker-compose build
-                docker-compose up -d
-                '''
-            }
-        }
+        // stage('Build & Deploy') {
+        //     steps {
+        //         sh '''
+        //         docker-compose stop
+        //         docker-compose build
+        //         docker-compose up -d
+        //         '''
+        //     }
+        // }
 
         // stage('Push Docker Image (Nexus)') {
         //     when {
@@ -88,32 +88,43 @@ pipeline {
         //         }
         //     }
         // }
-
-        stage('Push Docker Image (Nexus)') {
-    when {
-        branch 'master'
-    }
-    steps {
-        withDockerRegistry([credentialsId: 'nexus', url: 'https://nexus.foodgame.fr']) {
-            script {
-                def dockerImageName = 'fatboar_master_express_preprod '
-                def dockerImageTag = 'latest'
-                def nexusRepository = 'nexus.foodgame.fr:8123'
-
-                sh """
-                    echo "Tagging Docker image..."
-                    docker tag ${dockerImageName}:${dockerImageTag} ${nexusRepository}/${dockerImageName}:${dockerImageTag}
-                    
-                    echo "Pushing to Nexus repository..."
-                    docker push ${nexusRepository}/${dockerImageName}:${dockerImageTag}
+    
+        stage('Docker Registry Login, Pull, and Push') {
+            when {
+                branch 'master'
+            }
+            steps {
+                script {
+                    def registryUrl = 'nexus.foodgame.fr:8123'
+                    def imageName = 'sonarqube'
+                    def imageVersion = 'latest'
                     
-                    echo "Cleaning up local images..."
-                    docker rmi ${nexusRepository}/${dockerImageName}:${dockerImageTag} || true
-                """
+                    docker.withRegistry("https://${registryUrl}", 'nexus') {
+                        try {
+                            // Try pulling the image from the registry
+                            echo "Trying to pull image: ${registryUrl}/${imageName}:${imageVersion}"
+                            docker.image("${registryUrl}/${imageName}:${imageVersion}").pull()
+                        } catch (Exception e) {
+                            echo "Image pull failed. Attempting to build and push."
+
+                            // Pull base image from Docker Hub
+                            echo "Pulling base image: ${imageName}:${imageVersion}"
+                            sh "docker pull ${imageName}:${imageVersion}"
+                            
+                            // Tag the image for the private registry
+                            echo "Tagging image for private registry"
+                            sh "docker tag ${imageName}:${imageVersion} ${registryUrl}/${imageName}:${imageVersion}"
+                            
+                            // Push the tagged image to the private registry
+                            echo "Pushing image to private registry"
+                            sh "docker push ${registryUrl}/${imageName}:${imageVersion}"
+                        }
+                    }
+                }
             }
         }
-    }
-}
+    
+
 
         stage('Cleanup') {
             steps {
@@ -127,112 +138,65 @@ pipeline {
             }
         }
     }
-    // stage('Test') 
-    // {
-        
-    //    dir("${env.WORKSPACE}/angular-client")
-    //    {
-    //         // sh 'npm i @angular-devkit/build-angular'
-          		
-	// 		// try
-	// 		// {
-	// 		// 	sh 'npm run test-ci'
-	// 		// } 
-	// 		// catch(err) 
-	// 		// {
-	// 		// 	sh 'echo TEST FAILED'
-	// 		// 	junit 'target/surefire-reports/TESTS-TestSuite.xml/*.xml'
-	// 		// 	throw err
-	// 		// }
-            
-    //         def karma = docker.image('trion/ng-cli-karma')
-    //         karma.pull()
-
-    //         try 
-    //         {
-    //             karma.run(' -u $(id -u) -v ${WORKSPACE}:/app trion/ng-cli-karma ')
-    //             karma.inside 
-    //             {
-    //                 sh 'npm install'
-                
-    //                 try 
-    //                 {
-    //                     sh ('karma start karma.conf.js')
-    //                 }
-    //                 catch(err) 
-    //                 {
-    //                     sh 'echo TEST FAILED'
-    //                     step([$class: 'JUnitResultArchiver', testResults: 'target/surefire-reports/TESTS-TestSuite.xml/*.xml', healthScaleFactor: 1.0])
-    //                     throw err
-    //                 }
-    //                 sh 'echo DO SOMETHING ELSE AFTER TEST'
-    //             }
-    //             sh 'ls -al '
-    //         } 
-    //         catch(err) 
-    //         {
-    //         sh 'echo RUN DOCKER FAILED'
-    //         throw err
-    //         }
-    //    }
-    // }
-
-
-    // def imageApache = stage("Build apache preprod")
-	// {
-	// 	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 .")
-	// }
+    post {
+        success {
+            echo "Pipeline exécuté avec succès pour la branche ${env.BRANCH_NAME}."
+        }
+        failure {
+            echo "Échec du pipeline pour la branche ${env.BRANCH_NAME}."
+        }
+    }
+}
+
+
+
+
+// node{
+
+//     env.NODEJS_HOME = "${tool 'NodeJS'}"
+//     // on linux / mac
+//     env.PATH="${env.NODEJS_HOME}/bin:${env.PATH}"
+//     // on windows
+//     //env.PATH="${env.NODEJS_HOME};${env.PATH}"
+//     sh 'npm --version'
+
+// 	stage('checkout')
+// 	{
+// 		deleteDir()
+// 		checkout scm
+// 	}
+	 
+// 	 stage('Stop Containers')
+//     {
+//         sh 'docker ps | grep "workflow_" -v | awk -F " " \'{ if(NR>1) print $1}\' |xargs docker kill |xargs docker rm || true'
+//         sh 'docker system prune -f'
+//     }
 	
-	// def imageExpress = stage("Build express preprod")
-	// {
-	// 	docker.build("server-express-preprod", "--no-cache   -f express-server/Dockerfile express-server")
-	// }
-
-    /*stage("Run Container express preprod")
-	{
-		containerSql=imageExpress.run("--name server-express-preprod -v /var/log:/var/www/app/log/root  --link database_preprod:mongo --network=web")
-	}
-    stage("Run Container apache preprod")
-	{
-		containerApache=imageApache.run("--name server-apache-preprod --link server-mysql-preprod:mysql --network=web")
-	}*/
-
-
-	/*stage("Run Container express preprod")
-	{
-		containerSql=imageExpress.run("--name server-express-preprod -v /home/projet/fatboar/${e}/mysql:/var/lib/mysql --network=web")
-	}
 	
-	stage("Run Container apache preprod")
-	{
-		containerApache=imageApache.run("--name server-apache-preprod --link server-mysql-preprod:mysql --network=web")
-	}*/
-
-
-    // stage("Push preprod images to nexus")
-	// {
-	// 	/*docker.withRegistry('http://localhost:8083','885ef60c-9352-489a-bd1c-e4b695747c21')
-	// 	{
-	// 		imageApache.push('latest')
-	// 		imageExpress.push('latest')
-	// 	}*/
-	// }
-    // stage('SonarQube analysis') 
-    // {  
-    //      def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
-    //     withSonarQubeEnv('SonarQube') 
-    //     { 
-    //     // If you have configured more than one global server connection, you can specify its name
-    //     sh "${scannerHome}/bin/sonar-scanner  \
-    //     -Dsonar.projectKey=FatboarProject \
-    //     -Dsonar.sources=. \
-    //     -Dsonar.host.url=https://sonarqube.foodgame.fr \
-    //     -Dsonar.login=sqp_09ee9072c917af8212864baf0f75c950afc14c64"
-    //     }
-    // }
+// 	// stage("Push preprod images to nexus")
+// 	// {
+// 	// 	docker.withRegistry('http://localhost:8083','885ef60c-9352-489a-bd1c-e4b695747c21')
+// 	// 	{
+// 	// 		imageApache.push('latest')
+// 	// 		imageExpress.push('latest')
+// 	// 	}*/
+// 	// }
+//     // stage('SonarQube analysis') 
+//     // {  
+//     //      def scannerHome = tool name: 'SonarQube Scanner', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
+//     //     withSonarQubeEnv('SonarQube') 
+//     //     { 
+//     //     // If you have configured more than one global server connection, you can specify its name
+//     //     sh "${scannerHome}/bin/sonar-scanner  \
+//     //     -Dsonar.projectKey=FatboarProject \
+//     //     -Dsonar.sources=. \
+//     //     -Dsonar.host.url=https://sonarqube.foodgame.fr \
+//     //     -Dsonar.login=sqp_09ee9072c917af8212864baf0f75c950afc14c64"
+//     //     }
+//     // }
 	
 
-//     stage('Build Docker MEAN Stack(PreProduction Deployment)') 
+//     stage('Build Docker MEAN Stack(Test Deployment)') 
 //     {
 //         sh 'docker-compose -v'
 //         sh 'docker-compose build'
@@ -243,4 +207,4 @@ pipeline {
 //     {
 //        sh 'echo "Félicitation tout c\'est bien déroulé!"'  
 //    }
-}
+// }