Update Jenkinsfile

Signed-off-by: prankumargrid <prankumar@griddynamics.com>
This commit is contained in:
prankumargrid 2025-05-01 16:15:21 +05:30 committed by GitHub
parent 96c8f864d0
commit d2f5f2445d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

61
Jenkinsfile vendored
View file

@ -7,7 +7,8 @@ pipeline {
} }
environment { environment {
IMAGE_NAME = 'prankumar313-main' IMAGE_NAME = "spring-petclinic"
DOCKERHUB_USER = "prankumar313" // Change to your Docker Hub username
} }
stages { stages {
@ -16,29 +17,63 @@ pipeline {
sh ''' sh '''
apt-get update apt-get update
apt-get install -y docker.io apt-get install -y docker.io
docker --version
''' '''
} }
} }
stage('Build Docker Image') { stage('Checkstyle') {
when {
expression { env.BRANCH_NAME != 'main' }
}
steps { steps {
script { sh './gradlew checkstyleMain checkstyleTest'
COMMIT = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim() archiveArtifacts artifacts: '**/build/reports/checkstyle/*.xml', allowEmptyArchive: true
sh "docker build -t ${IMAGE_NAME}:${COMMIT} ."
}
} }
} }
stage('Push to Docker Hub') { stage('Test') {
when {
expression { env.BRANCH_NAME != 'main' }
}
steps { steps {
withCredentials([usernamePassword(credentialsId: 'dockerhub-creds', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) { sh './gradlew test'
sh ''' }
echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin }
docker tag ${IMAGE_NAME}:${COMMIT} ${DOCKER_USER}/${IMAGE_NAME}:${COMMIT}
docker push ${DOCKER_USER}/${IMAGE_NAME}:${COMMIT} stage('Build (No Tests)') {
''' when {
expression { env.BRANCH_NAME != 'main' }
}
steps {
sh './gradlew build -x test'
}
}
stage('Build & Push Docker Image') {
steps {
script {
COMMIT = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
IMAGE_TAG = "${DOCKERHUB_USER}/${env.BRANCH_NAME == 'main' ? 'main' : 'mr'}:${COMMIT}"
sh """
docker build -t ${IMAGE_TAG} .
"""
withCredentials([usernamePassword(credentialsId: 'dockerhub-creds', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh """
echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
docker push ${IMAGE_TAG}
"""
}
} }
} }
} }
} }
post {
always {
cleanWs()
}
}
} }