From f3066809c99f405cfa7df4c2b56a16ebdc53f5e7 Mon Sep 17 00:00:00 2001 From: nesrinedridi1988 Date: Thu, 16 May 2024 16:37:59 +0200 Subject: [PATCH 1/7] Enhance Jenkinsfile --- Jenkinsfile | 24 ++--- pom.xml | 247 ++++++++++++++++++++++++++-------------------------- 2 files changed, 135 insertions(+), 136 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 57d3199..dba5869 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,15 +1,15 @@ -def CONTAINER_NAME = "calculator" def ENV_NAME = getEnvName(env.BRANCH_NAME) +def CONTAINER_NAME = "calculator-" +ENV_NAME def CONTAINER_TAG = getTag(env.BUILD_NUMBER, env.BRANCH_NAME) def HTTP_PORT = getHTTPPort(env.BRANCH_NAME) -def EMAIL_RECIPIENTS = "philippe.guemkamsimo@gmail.com" +def EMAIL_RECIPIENTS = "nesrine.dridi@esprit.tn" node { try { stage('Initialize') { - def dockerHome = tool 'DockerLatest' - def mavenHome = tool 'MavenLatest' + def dockerHome = tool 'dockerlatest' + def mavenHome = tool 'mavenlatest' env.PATH = "${dockerHome}/bin:${mavenHome}/bin:${env.PATH}" } @@ -23,7 +23,7 @@ node { } stage('Sonarqube Analysis') { - withSonarQubeEnv('SonarQubeLocalServer') { + withSonarQubeEnv('localhost_sonarqube') { sh " mvn sonar:sonar -Dintegration-tests.skip=true -Dmaven.test.failure.ignore=true" } timeout(time: 1, unit: 'MINUTES') { @@ -43,13 +43,13 @@ node { } stage('Push to Docker Registry') { - withCredentials([usernamePassword(credentialsId: 'DockerhubCredentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { + withCredentials([usernamePassword(credentialsId: 'dockerhubcredentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { pushToImage(CONTAINER_NAME, CONTAINER_TAG, USERNAME, PASSWORD) } } stage('Run App') { - withCredentials([usernamePassword(credentialsId: 'DockerhubCredentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { + withCredentials([usernamePassword(credentialsId: 'dockerhubcredentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { runApp(CONTAINER_NAME, CONTAINER_TAG, USERNAME, HTTP_PORT, ENV_NAME) } @@ -96,22 +96,22 @@ def sendEmail(recipients) { } String getEnvName(String branchName) { - if (branchName == 'main') { + if (branchName == 'master') { return 'prod' } return (branchName == 'develop') ? 'uat' : 'dev' } String getHTTPPort(String branchName) { - if (branchName == 'main') { + if (branchName == 'master') { return '9003' } return (branchName == 'develop') ? '9002' : '9001' } String getTag(String buildNumber, String branchName) { - if (branchName == 'main') { - return buildNumber + '-unstable' + if (branchName == 'master') { + return buildNumber + '-stable' } - return buildNumber + '-stable' + return buildNumber + '-unstable' } diff --git a/pom.xml b/pom.xml index 1b16bd0..cc8ec87 100644 --- a/pom.xml +++ b/pom.xml @@ -1,128 +1,127 @@ - 4.0.0 - tech.zerofiltre.testing - calculator - 0.0.1-SNAPSHOT - - 5.5.2 - UTF-8 - 11 - ${maven.compiler.source} - 4.3.1 - 2.2.0.RELEASE - tech.zerofiltre.testing:calculator - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.openclassrooms.testing + calculator + 0.0.1-SNAPSHOT + + 5.5.2 + UTF-8 + 11 + ${maven.compiler.source} + 4.3.1 + 2.2.0.RELEASE + - - org.springframework.boot - spring-boot-starter-parent - 2.2.0.RELEASE - + + org.springframework.boot + spring-boot-starter-parent + 2.2.0.RELEASE + - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-test - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - org.springframework.boot - spring-boot-devtools - runtime - - - org.webjars - bootstrap - ${bootstrap.version} - - - org.junit.jupiter - junit-jupiter-api - test - - - org.junit.jupiter - junit-jupiter - test - - - org.mockito - mockito-junit-jupiter - test - - - org.assertj - assertj-core - test - - - org.apache.logging.log4j - log4j-core - - - javax.inject - javax.inject - 1 - - - org.seleniumhq.selenium - selenium-java - test - - - io.github.bonigarcia - webdrivermanager - 3.7.1 - - - - calculator - - - org.springframework.boot - spring-boot-maven-plugin - - - org.apache.maven.plugins - maven-failsafe-plugin - - - - integration-test - verify - - - - - - org.jacoco - jacoco-maven-plugin - 0.8.2 - - - - prepare-agent - - - - - report - test - - report - - - - - - + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-devtools + runtime + + + org.webjars + bootstrap + ${bootstrap.version} + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter + test + + + org.mockito + mockito-junit-jupiter + test + + + org.assertj + assertj-core + test + + + org.apache.logging.log4j + log4j-core + + + javax.inject + javax.inject + 1 + + + org.seleniumhq.selenium + selenium-java + test + + + io.github.bonigarcia + webdrivermanager + 3.7.1 + + + + calculator + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-failsafe-plugin + + + + integration-test + verify + + + + + + org.jacoco + jacoco-maven-plugin + 0.8.2 + + + + prepare-agent + + + + + report + test + + report + + + + + + \ No newline at end of file From dfde37396c6ef06c958dbee6b9ff04420800400f Mon Sep 17 00:00:00 2001 From: nesrinedridi1988 <50553328+nesrinedridi1988@users.noreply.github.com> Date: Fri, 17 May 2024 15:07:16 +0200 Subject: [PATCH 2/7] Update Calculator.java --- .../tech/zerofiltre/testing/calcul/domain/Calculator.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/tech/zerofiltre/testing/calcul/domain/Calculator.java b/src/main/java/tech/zerofiltre/testing/calcul/domain/Calculator.java index afe011e..62ec1e3 100644 --- a/src/main/java/tech/zerofiltre/testing/calcul/domain/Calculator.java +++ b/src/main/java/tech/zerofiltre/testing/calcul/domain/Calculator.java @@ -54,10 +54,11 @@ public int fact(int a) { return a * fact(a - 1); } - public void longCalculation() { + public void longCalculation() { try { Thread.sleep(500); - } catch (final InterruptedException e) { + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); // Réinterruption logger.debug("Thread has been interrupted", e); } } From e637e0cb920d9b8f999c8b218128b722c787c277 Mon Sep 17 00:00:00 2001 From: nesrinedridi1988 <50553328+nesrinedridi1988@users.noreply.github.com> Date: Fri, 17 May 2024 15:39:26 +0200 Subject: [PATCH 3/7] Update Jenkinsfile --- Jenkinsfile | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index dba5869..97ba94c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -17,10 +17,7 @@ node { checkout scm } - stage('Build with test') { - - sh "mvn clean install" - } + stage('Sonarqube Analysis') { withSonarQubeEnv('localhost_sonarqube') { From 6976aefe08d7c8144ade1c191d55bca28a0b00e7 Mon Sep 17 00:00:00 2001 From: nesrinedridi1988 <50553328+nesrinedridi1988@users.noreply.github.com> Date: Fri, 17 May 2024 15:42:01 +0200 Subject: [PATCH 4/7] Update Jenkinsfile --- Jenkinsfile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 97ba94c..13136a7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -17,6 +17,10 @@ node { checkout scm } + stage('Build with test') { + + sh "mvn clean install" + } stage('Sonarqube Analysis') { From 52d09eba9c2f7eda182db4c54497c93cedb39cd8 Mon Sep 17 00:00:00 2001 From: nesrinedridi1988 <50553328+nesrinedridi1988@users.noreply.github.com> Date: Fri, 17 May 2024 15:53:53 +0200 Subject: [PATCH 5/7] Update Jenkinsfile --- Jenkinsfile | 103 +--------------------------------------------------- 1 file changed, 1 insertion(+), 102 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 13136a7..48e0b5e 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -13,106 +13,5 @@ node { env.PATH = "${dockerHome}/bin:${mavenHome}/bin:${env.PATH}" } - stage('Checkout') { - checkout scm - } - - stage('Build with test') { - - sh "mvn clean install" - } - - - stage('Sonarqube Analysis') { - withSonarQubeEnv('localhost_sonarqube') { - sh " mvn sonar:sonar -Dintegration-tests.skip=true -Dmaven.test.failure.ignore=true" - } - timeout(time: 1, unit: 'MINUTES') { - def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv - if (qg.status != 'OK') { - error "Pipeline aborted due to quality gate failure: ${qg.status}" - } - } - } - - stage("Image Prune") { - imagePrune(CONTAINER_NAME) - } - - stage('Image Build') { - imageBuild(CONTAINER_NAME, CONTAINER_TAG) - } - - stage('Push to Docker Registry') { - withCredentials([usernamePassword(credentialsId: 'dockerhubcredentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { - pushToImage(CONTAINER_NAME, CONTAINER_TAG, USERNAME, PASSWORD) - } - } - - stage('Run App') { - withCredentials([usernamePassword(credentialsId: 'dockerhubcredentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { - runApp(CONTAINER_NAME, CONTAINER_TAG, USERNAME, HTTP_PORT, ENV_NAME) - - } - } - - } finally { - deleteDir() - sendEmail(EMAIL_RECIPIENTS); - } - -} - -def imagePrune(containerName) { - try { - sh "docker image prune -f" - sh "docker stop $containerName" - } catch (ignored) { - } -} - -def imageBuild(containerName, tag) { - sh "docker build -t $containerName:$tag -t $containerName --pull --no-cache ." - echo "Image build complete" -} - -def pushToImage(containerName, tag, dockerUser, dockerPassword) { - sh "docker login -u $dockerUser -p $dockerPassword" - sh "docker tag $containerName:$tag $dockerUser/$containerName:$tag" - sh "docker push $dockerUser/$containerName:$tag" - echo "Image push complete" -} - -def runApp(containerName, tag, dockerHubUser, httpPort, envName) { - sh "docker pull $dockerHubUser/$containerName" - sh "docker run --rm --env SPRING_ACTIVE_PROFILES=$envName -d -p $httpPort:$httpPort --name $containerName $dockerHubUser/$containerName:$tag" - echo "Application started on port: ${httpPort} (http)" -} - -def sendEmail(recipients) { - mail( - to: recipients, - subject: "Build ${env.BUILD_NUMBER} - ${currentBuild.currentResult} - (${currentBuild.fullDisplayName})", - body: "Check console output at: ${env.BUILD_URL}/console" + "\n") -} - -String getEnvName(String branchName) { - if (branchName == 'master') { - return 'prod' - } - return (branchName == 'develop') ? 'uat' : 'dev' -} - -String getHTTPPort(String branchName) { - if (branchName == 'master') { - return '9003' - } - return (branchName == 'develop') ? '9002' : '9001' -} - -String getTag(String buildNumber, String branchName) { - if (branchName == 'master') { - return buildNumber + '-stable' - } - return buildNumber + '-unstable' + } From 38f87c545e21d94a7e7f34772202c3ebca86ab7f Mon Sep 17 00:00:00 2001 From: nesrinedridi1988 <50553328+nesrinedridi1988@users.noreply.github.com> Date: Fri, 17 May 2024 15:56:53 +0200 Subject: [PATCH 6/7] Update Jenkinsfile --- Jenkinsfile | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 48e0b5e..13136a7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -13,5 +13,106 @@ node { env.PATH = "${dockerHome}/bin:${mavenHome}/bin:${env.PATH}" } - + stage('Checkout') { + checkout scm + } + + stage('Build with test') { + + sh "mvn clean install" + } + + + stage('Sonarqube Analysis') { + withSonarQubeEnv('localhost_sonarqube') { + sh " mvn sonar:sonar -Dintegration-tests.skip=true -Dmaven.test.failure.ignore=true" + } + timeout(time: 1, unit: 'MINUTES') { + def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv + if (qg.status != 'OK') { + error "Pipeline aborted due to quality gate failure: ${qg.status}" + } + } + } + + stage("Image Prune") { + imagePrune(CONTAINER_NAME) + } + + stage('Image Build') { + imageBuild(CONTAINER_NAME, CONTAINER_TAG) + } + + stage('Push to Docker Registry') { + withCredentials([usernamePassword(credentialsId: 'dockerhubcredentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { + pushToImage(CONTAINER_NAME, CONTAINER_TAG, USERNAME, PASSWORD) + } + } + + stage('Run App') { + withCredentials([usernamePassword(credentialsId: 'dockerhubcredentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) { + runApp(CONTAINER_NAME, CONTAINER_TAG, USERNAME, HTTP_PORT, ENV_NAME) + + } + } + + } finally { + deleteDir() + sendEmail(EMAIL_RECIPIENTS); + } + +} + +def imagePrune(containerName) { + try { + sh "docker image prune -f" + sh "docker stop $containerName" + } catch (ignored) { + } +} + +def imageBuild(containerName, tag) { + sh "docker build -t $containerName:$tag -t $containerName --pull --no-cache ." + echo "Image build complete" +} + +def pushToImage(containerName, tag, dockerUser, dockerPassword) { + sh "docker login -u $dockerUser -p $dockerPassword" + sh "docker tag $containerName:$tag $dockerUser/$containerName:$tag" + sh "docker push $dockerUser/$containerName:$tag" + echo "Image push complete" +} + +def runApp(containerName, tag, dockerHubUser, httpPort, envName) { + sh "docker pull $dockerHubUser/$containerName" + sh "docker run --rm --env SPRING_ACTIVE_PROFILES=$envName -d -p $httpPort:$httpPort --name $containerName $dockerHubUser/$containerName:$tag" + echo "Application started on port: ${httpPort} (http)" +} + +def sendEmail(recipients) { + mail( + to: recipients, + subject: "Build ${env.BUILD_NUMBER} - ${currentBuild.currentResult} - (${currentBuild.fullDisplayName})", + body: "Check console output at: ${env.BUILD_URL}/console" + "\n") +} + +String getEnvName(String branchName) { + if (branchName == 'master') { + return 'prod' + } + return (branchName == 'develop') ? 'uat' : 'dev' +} + +String getHTTPPort(String branchName) { + if (branchName == 'master') { + return '9003' + } + return (branchName == 'develop') ? '9002' : '9001' +} + +String getTag(String buildNumber, String branchName) { + if (branchName == 'master') { + return buildNumber + '-stable' + } + return buildNumber + '-unstable' } From bc261b6228fe839c85c35301172ba7c4b1a2cc84 Mon Sep 17 00:00:00 2001 From: nesrinedridi1988 Date: Fri, 17 May 2024 16:02:22 +0200 Subject: [PATCH 7/7] Enhance Calculator.java --- .../tech/zerofiltre/testing/calcul/domain/Calculator.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/tech/zerofiltre/testing/calcul/domain/Calculator.java b/src/main/java/tech/zerofiltre/testing/calcul/domain/Calculator.java index afe011e..bd11228 100644 --- a/src/main/java/tech/zerofiltre/testing/calcul/domain/Calculator.java +++ b/src/main/java/tech/zerofiltre/testing/calcul/domain/Calculator.java @@ -57,11 +57,13 @@ public int fact(int a) { public void longCalculation() { try { Thread.sleep(500); - } catch (final InterruptedException e) { + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); // Réinterruption logger.debug("Thread has been interrupted", e); } } + public Set digitsSet(int number) { final Set integers = new HashSet<>(); final String numberString = String.valueOf(number);