From 7f4b266dde5efb33020520cc8784ea2d97aec39d Mon Sep 17 00:00:00 2001 From: DevopsGuy1997 Date: Wed, 17 Dec 2025 08:05:52 +0530 Subject: [PATCH 1/3] Refactor Jenkinsfile by removing unused stages Removed several stages related to testing, analysis, and Docker operations from the Jenkins pipeline. --- Jenkinsfile | 90 ++--------------------------------------------------- 1 file changed, 2 insertions(+), 88 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index c805a644..6c753787 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -4,112 +4,26 @@ pipeline { jdk 'Java17' maven 'Maven3' } - environment { - APP_NAME = "register-app-pipeline" - RELEASE = "1.0.0" - DOCKER_USER = "ashfaque9x" - DOCKER_PASS = 'dockerhub' - IMAGE_NAME = "${DOCKER_USER}" + "/" + "${APP_NAME}" - IMAGE_TAG = "${RELEASE}-${BUILD_NUMBER}" - JENKINS_API_TOKEN = credentials("JENKINS_API_TOKEN") - } stages{ stage("Cleanup Workspace"){ steps { cleanWs() } } - stage("Checkout from SCM"){ steps { git branch: 'main', credentialsId: 'github', url: 'https://github.com/Ashfaque-9x/register-app' } } - stage("Build Application"){ steps { sh "mvn clean package" } - - } - - stage("Test Application"){ - steps { - sh "mvn test" - } - } - - stage("SonarQube Analysis"){ - steps { - script { - withSonarQubeEnv(credentialsId: 'jenkins-sonarqube-token') { - sh "mvn sonar:sonar" - } - } - } - } - - stage("Quality Gate"){ - steps { - script { - waitForQualityGate abortPipeline: false, credentialsId: 'jenkins-sonarqube-token' - } - } - } - - stage("Build & Push Docker Image") { - steps { - script { - docker.withRegistry('',DOCKER_PASS) { - docker_image = docker.build "${IMAGE_NAME}" - } - - docker.withRegistry('',DOCKER_PASS) { - docker_image.push("${IMAGE_TAG}") - docker_image.push('latest') - } - } - } - - } - - stage("Trivy Scan") { + stage("Test Application"){ steps { - script { - sh ('docker run -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy image ashfaque9x/register-app-pipeline:latest --no-progress --scanners vuln --exit-code 0 --severity HIGH,CRITICAL --format table') - } + sh "mvn test" } - } - - stage ('Cleanup Artifacts') { - steps { - script { - sh "docker rmi ${IMAGE_NAME}:${IMAGE_TAG}" - sh "docker rmi ${IMAGE_NAME}:latest" - } - } - } - - stage("Trigger CD Pipeline") { - steps { - script { - sh "curl -v -k --user clouduser:${JENKINS_API_TOKEN} -X POST -H 'cache-control: no-cache' -H 'content-type: application/x-www-form-urlencoded' --data 'IMAGE_TAG=${IMAGE_TAG}' 'ec2-13-232-128-192.ap-south-1.compute.amazonaws.com:8080/job/gitops-register-app-cd/buildWithParameters?token=gitops-token'" - } - } - } - } - - post { - failure { - emailext body: '''${SCRIPT, template="groovy-html.template"}''', - subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Failed", - mimeType: 'text/html',to: "ashfaque.s510@gmail.com" } - success { - emailext body: '''${SCRIPT, template="groovy-html.template"}''', - subject: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - Successful", - mimeType: 'text/html',to: "ashfaque.s510@gmail.com" - } } } From 779c80a975c94514b5a8ec25b0b0b7abf5058020 Mon Sep 17 00:00:00 2001 From: DevopsGuy1997 Date: Wed, 17 Dec 2025 08:14:40 +0530 Subject: [PATCH 2/3] Update Jenkinsfile --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 6c753787..abc4f4eb 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -12,7 +12,7 @@ pipeline { } stage("Checkout from SCM"){ steps { - git branch: 'main', credentialsId: 'github', url: 'https://github.com/Ashfaque-9x/register-app' + git branch: 'main', credentialsId: 'github', url: 'https://github.com/DevopsGuy1997/register-app.git' } } stage("Build Application"){ From 52ea12a864aac7c2c72dcb823252ef832f1b5d21 Mon Sep 17 00:00:00 2001 From: DevopsGuy1997 Date: Wed, 17 Dec 2025 08:18:22 +0530 Subject: [PATCH 3/3] Refactor Jenkinsfile for improved stages and steps --- Jenkinsfile | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index abc4f4eb..bd8388d8 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -6,24 +6,48 @@ pipeline { } stages{ stage("Cleanup Workspace"){ - steps { + steps { cleanWs() - } + } } stage("Checkout from SCM"){ - steps { - git branch: 'main', credentialsId: 'github', url: 'https://github.com/DevopsGuy1997/register-app.git' - } + steps { + git branch: 'main', credentialsId: 'github', url: 'https://github.com/DevopsGuy1997/register-app.git' + } + } + stage("Build & Unit Test"){ + steps { + // Use 'mvn verify' which includes all phases up to test + sh "mvn clean verify" + } } - stage("Build Application"){ + stage("Test Reporting & Archive"){ steps { - sh "mvn clean package" + echo "Publishing test results and archiving artifact..." + + // 1. Publish JUnit test results for Jenkins reporting + junit '**/target/surefire-reports/*.xml' + + // 2. Archive the built application artifact (e.g., the JAR file) + archiveArtifacts artifacts: 'target/*.jar', fingerprint: true } } - stage("Test Application"){ - steps { - sh "mvn test" - } - } - } + stage("Deploy to Staging"){ + steps { + // TODO: Replace this with your actual deployment logic + sh "echo 'Starting deployment of the archived artifact...'" + // Example: sh "scp target/*.jar user@staging-server:/opt/app/" + } + } + } + // Post-actions run after all stages, useful for cleanup or notifications + post { + always { + echo "Pipeline finished. Status: ${currentBuild.result}" + } + failure { + // Optional: Send an email notification on failure + // mail to: 'devops-team@example.com', subject: "Pipeline Failed: ${env.JOB_NAME}" + } + } }