diff --git a/Jenkinsfile b/Jenkinsfile index 7fecd3c2b..e90c25bd0 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,122 +1,79 @@ pipeline { - - agent any -/* - tools { - maven "maven3" - - } -*/ + agent any + environment { - NEXUS_VERSION = "nexus3" - NEXUS_PROTOCOL = "http" - NEXUS_URL = "172.31.40.209:8081" - NEXUS_REPOSITORY = "vprofile-release" - NEXUS_REPO_ID = "vprofile-release" - NEXUS_CREDENTIAL_ID = "nexuslogin" - ARTVERSION = "${env.BUILD_ID}" + // Use Tomcat image for building and running your app + DOCKER_IMAGE = "darsan03/tomcat" // Change this if you want to use a custom Tomcat image + DOCKER_TAG = "9.0-${BUILD_NUMBER}" // Ensure it uses the correct Tomcat version + SONARQUBE_URL = 'http://34.236.145.115:9000' + SONARQUBE_TOKEN = credentials('sonarqube-token') + } + + tools { + maven 'maven' } - - stages{ - - stage('BUILD'){ + + stages { + + stage('Checkout Code') { steps { - sh 'mvn clean install -DskipTests' - } - post { - success { - echo 'Now Archiving...' - archiveArtifacts artifacts: '**/target/*.war' - } + git branch: 'main', + url: 'https://github.com/hkhcoder/vprofile-action.git', + credentialsId: 'github-credentials' } } - stage('UNIT TEST'){ + stage('Build with Maven') { steps { - sh 'mvn test' + echo "🔧 Building the project with Maven..." + sh 'mvn clean package -DskipTests -e -X' } } - stage('INTEGRATION TEST'){ + stage('SonarQube Analysis') { steps { - sh 'mvn verify -DskipUnitTests' + echo "🔍 Running SonarQube analysis..." + sh """ + mvn sonar:sonar \ + -Dsonar.projectKey=my-project-key \ + -Dsonar.host.url=${SONARQUBE_URL} \ + -Dsonar.login=${SONARQUBE_TOKEN} + """ } } - - stage ('CODE ANALYSIS WITH CHECKSTYLE'){ - steps { - sh 'mvn checkstyle:checkstyle' - } - post { - success { - echo 'Generated Analysis Result' - } - } - } - - stage('CODE ANALYSIS with SONARQUBE') { - - environment { - scannerHome = tool 'sonarscanner4' - } - - steps { - withSonarQubeEnv('sonar-pro') { - sh '''${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=vprofile \ - -Dsonar.projectName=vprofile-repo \ - -Dsonar.projectVersion=1.0 \ - -Dsonar.sources=src/ \ - -Dsonar.java.binaries=target/test-classes/com/visualpathit/account/controllerTest/ \ - -Dsonar.junit.reportsPath=target/surefire-reports/ \ - -Dsonar.jacoco.reportsPath=target/jacoco.exec \ - -Dsonar.java.checkstyle.reportPaths=target/checkstyle-result.xml''' - } - timeout(time: 10, unit: 'MINUTES') { - waitForQualityGate abortPipeline: true + stage('Build Docker Image with Tomcat') { + steps { + echo "🐳 Building Docker image for Tomcat with your app..." + sh """ + docker build -t ${DOCKER_IMAGE}:${DOCKER_TAG} -f Dockerfile . + """ } - } } - stage("Publish to Nexus Repository Manager") { + stage('Push Docker Image to DockerHub') { steps { - script { - pom = readMavenPom file: "pom.xml"; - filesByGlob = findFiles(glob: "target/*.${pom.packaging}"); - echo "${filesByGlob[0].name} ${filesByGlob[0].path} ${filesByGlob[0].directory} ${filesByGlob[0].length} ${filesByGlob[0].lastModified}" - artifactPath = filesByGlob[0].path; - artifactExists = fileExists artifactPath; - if(artifactExists) { - echo "*** File: ${artifactPath}, group: ${pom.groupId}, packaging: ${pom.packaging}, version ${pom.version} ARTVERSION"; - nexusArtifactUploader( - nexusVersion: NEXUS_VERSION, - protocol: NEXUS_PROTOCOL, - nexusUrl: NEXUS_URL, - groupId: pom.groupId, - version: ARTVERSION, - repository: NEXUS_REPOSITORY, - credentialsId: NEXUS_CREDENTIAL_ID, - artifacts: [ - [artifactId: pom.artifactId, - classifier: '', - file: artifactPath, - type: pom.packaging], - [artifactId: pom.artifactId, - classifier: '', - file: "pom.xml", - type: "pom"] - ] - ); - } - else { - error "*** File: ${artifactPath}, could not be found"; - } + echo "📤 Pushing Docker image to DockerHub..." + withCredentials([usernamePassword( + credentialsId: 'dockerhub-credentials', + usernameVariable: 'DOCKER_USER', + passwordVariable: 'DOCKER_PASS' + )]) { + sh """ + echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin + docker push ${DOCKER_IMAGE}:${DOCKER_TAG} + """ } } } - - } - + post { + success { + echo '✅ Pipeline succeeded — Image pushed to DockerHub!' + } + failure { + echo '❌ Pipeline failed — Check the logs!' + } + } }