Some checks failed
ecommerce-backend/pipeline/head There was a failure building this commit
124 lines
4.2 KiB
Groovy
124 lines
4.2 KiB
Groovy
pipeline {
|
|
agent any
|
|
|
|
environment {
|
|
HARBOR = 'harbor.myriadcara.com'
|
|
IMAGE = 'harbor.myriadcara.com/vaishnavi-ecommerce/backend'
|
|
SONAR_PROJECT = 'ecommerce'
|
|
VERSION_FILE = '/home/jenkins/vaishnavi-backend-version.txt' // ✅ fixed path
|
|
}
|
|
|
|
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('Install SonarScanner') {
|
|
steps {
|
|
sh '''
|
|
if [ ! -f /opt/sonar-scanner/bin/sonar-scanner ]; then
|
|
echo "Installing sonar-scanner..."
|
|
cd /opt
|
|
wget -q https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-5.0.1.3006-linux.zip
|
|
unzip -q sonar-scanner-cli-5.0.1.3006-linux.zip
|
|
mv sonar-scanner-5.0.1.3006-linux sonar-scanner
|
|
chmod +x /opt/sonar-scanner/jre/bin/*
|
|
apk add --no-cache openjdk17 gcompat libc6-compat
|
|
sed -i 's/use_embedded_jre=true/use_embedded_jre=false/' /opt/sonar-scanner/bin/sonar-scanner
|
|
echo "sonar-scanner installed successfully"
|
|
else
|
|
echo "sonar-scanner already installed, skipping..."
|
|
fi
|
|
'''
|
|
}
|
|
}
|
|
|
|
stage('SonarQube Scan') {
|
|
steps {
|
|
withSonarQubeEnv('SonarQube') {
|
|
withCredentials([string(
|
|
credentialsId: 'sonarqube-token',
|
|
variable: 'SONAR_TOKEN'
|
|
)]) {
|
|
sh """
|
|
/opt/sonar-scanner/bin/sonar-scanner \
|
|
-Dsonar.projectKey=${SONAR_PROJECT} \
|
|
-Dsonar.projectName=${SONAR_PROJECT} \
|
|
-Dsonar.sources=. \
|
|
-Dsonar.token=\$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}"
|
|
}
|
|
}
|
|
} |