Update Jenkinsfile

Signed-off-by: prankumargrid <prankumar@griddynamics.com>
This commit is contained in:
prankumargrid 2025-04-30 15:15:29 +05:30 committed by GitHub
parent 8ce35bef8d
commit ea795ed756
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

49
Jenkinsfile vendored
View file

@ -1,49 +1,32 @@
pipeline { pipeline {
agent any agent any
environment { environment {
DOCKERHUB_CREDENTIALS = credentials('dockerhub-credentials') DOCKER_IMAGE = "prankumar313"
IMAGE_NAME_MAIN = "prankumar313/main" COMMIT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
IMAGE_NAME_MR = "prankumar313/mr"
} }
stages { stages {
stage('Check Git Branch') {
steps {
script {
BRANCH_NAME = env.BRANCH_NAME ?: sh(script: 'git rev-parse --abbrev-ref HEAD', returnStdout: true).trim()
IS_MR = BRANCH_NAME != 'main'
COMMIT_HASH = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
}
}
}
stage('Checkstyle') { stage('Checkstyle') {
when { when {
expression { IS_MR } not { branch 'main' }
} }
steps { steps {
sh './gradlew checkstyleMain' sh './gradlew checkstyleMain checkstyleTest'
} archiveArtifacts artifacts: '**/build/reports/checkstyle/*.xml', allowEmptyArchive: true
post {
always {
archiveArtifacts artifacts: 'build/reports/checkstyle/*.html', allowEmptyArchive: true
}
} }
} }
stage('Test') { stage('Test') {
when { when {
expression { IS_MR } not { branch 'main' }
} }
steps { steps {
sh './gradlew test' sh './gradlew test'
} }
} }
stage('Build') { stage('Build (No Tests)') {
when { when {
expression { IS_MR } not { branch 'main' }
} }
steps { steps {
sh './gradlew build -x test' sh './gradlew build -x test'
@ -53,19 +36,15 @@ pipeline {
stage('Build Docker Image') { stage('Build Docker Image') {
steps { steps {
script { script {
IMAGE_TAG = "${IS_MR ? IMAGE_NAME_MR : IMAGE_NAME_MAIN}:${COMMIT_HASH}" def tag = BRANCH_NAME == 'main' ? 'latest' : "${COMMIT}"
sh "docker buildx build -t ${IMAGE_TAG} ." def repo = BRANCH_NAME == 'main' ? "${DOCKER_IMAGE}-main" : "${DOCKER_IMAGE}-mr"
sh "docker build -t ${repo}:${tag} ."
withCredentials([usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh "echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin"
sh "docker push ${repo}:${tag}"
} }
} }
} }
stage('Push Docker Image') {
steps {
script {
sh "echo ${DOCKERHUB_CREDENTIALS_PSW} | docker login -u ${DOCKERHUB_CREDENTIALS_USR} --password-stdin"
sh "docker push ${IMAGE_TAG}"
}
}
} }
} }
} }