From 00c5a0147129158da7db56f4b30812e99fd670b1 Mon Sep 17 00:00:00 2001 From: subodh Date: Thu, 12 Mar 2026 04:12:58 +0000 Subject: [PATCH] updated jenkinsfile --- Jenkinsfile | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..0df328b --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,108 @@ +pipeline { + agent any + + environment { + HARBOR = 'harbor.myriadcara.com' + IMAGE = 'harbor.myriadcara.com/vaishnavi-ecommerce/backend' + SONAR_HOST = 'https://sonarqube.myriadcara.com' + SONAR_PROJECT = 'ecommerce' + VERSION_FILE = '/var/jenkins_home/vaishnavi-backend-version.txt' + } + + stages { + + stage('Generate Tag') { + steps { + script { + def version + if (fileExists(VERSION_FILE)) { + version = readFile(VERSION_FILE).trim() + } else { + version = '1.0.0' + } + + def parts = version.tokenize('.') + def major = parts[0].toInteger() + def minor = parts[1].toInteger() + def patch = parts[2].toInteger() + 1 + + def newVersion = "${major}.${minor}.${patch}" + writeFile file: VERSION_FILE, text: newVersion + + env.TAG = newVersion + echo "🏷️ New image tag: v${env.TAG}" + } + } + } + + stage('SonarQube Scan') { + steps { + withSonarQubeEnv('SonarQube') { + withCredentials([string( + credentialsId: 'sonarqube-token', + variable: 'SONAR_TOKEN' + )]) { + sh """ + sonar-scanner \ + -Dsonar.projectKey=${SONAR_PROJECT} \ + -Dsonar.projectName=${SONAR_PROJECT} \ + -Dsonar.sources=. \ + -Dsonar.login=\$SONAR_TOKEN \ + -Dsonar.projectVersion=${env.TAG} + """ + } + } + } + } + + stage('Quality Gate') { + steps { + timeout(time: 5, unit: 'MINUTES') { + waitForQualityGate abortPipeline: true + } + } + } + + stage('Build Image') { + steps { + sh """ + docker build \ + -t ${IMAGE}:${env.TAG} \ + -t ${IMAGE}:latest \ + --label build-number=${env.BUILD_NUMBER} \ + --label version=${env.TAG} \ + . + """ + } + } + + stage('Push to Harbor') { + steps { + withCredentials([usernamePassword( + credentialsId: 'harbor-updated-creds', + usernameVariable: 'HARBOR_USER', + passwordVariable: 'HARBOR_PASS' + )]) { + sh """ + echo "\$HARBOR_PASS" | docker login http://${HARBOR} -u "\$HARBOR_USER" --password-stdin + docker push ${IMAGE}:${env.TAG} + docker push ${IMAGE}:latest + """ + } + } + } + } + + post { + always { + sh "docker rmi ${IMAGE}:${env.TAG} || true" + sh "docker rmi ${IMAGE}:latest || true" + } + success { + echo "✅ Successfully pushed: ${IMAGE}:${env.TAG}" + } + failure { + echo "❌ Pipeline failed at tag: ${env.TAG}" + } + } +} \ No newline at end of file