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

111
Jenkinsfile vendored
View file

@ -1,71 +1,50 @@
pipeline {
agent any
environment {
DOCKERHUB_CREDENTIALS = credentials('dockerhub-credentials')
IMAGE_NAME_MAIN = "prankumar313/main"
IMAGE_NAME_MR = "prankumar313/mr"
agent any
environment {
DOCKER_IMAGE = "prankumar313"
COMMIT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
}
stages {
stage('Checkstyle') {
when {
not { branch 'main' }
}
steps {
sh './gradlew checkstyleMain checkstyleTest'
archiveArtifacts artifacts: '**/build/reports/checkstyle/*.xml', allowEmptyArchive: true
}
}
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') {
when {
expression { IS_MR }
}
steps {
sh './gradlew checkstyleMain'
}
post {
always {
archiveArtifacts artifacts: 'build/reports/checkstyle/*.html', allowEmptyArchive: true
}
}
}
stage('Test') {
when {
expression { IS_MR }
}
steps {
sh './gradlew test'
}
}
stage('Build') {
when {
expression { IS_MR }
}
steps {
sh './gradlew build -x test'
}
}
stage('Build Docker Image') {
steps {
script {
IMAGE_TAG = "${IS_MR ? IMAGE_NAME_MR : IMAGE_NAME_MAIN}:${COMMIT_HASH}"
sh "docker buildx build -t ${IMAGE_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}"
}
}
}
stage('Test') {
when {
not { branch 'main' }
}
steps {
sh './gradlew test'
}
}
stage('Build (No Tests)') {
when {
not { branch 'main' }
}
steps {
sh './gradlew build -x test'
}
}
stage('Build Docker Image') {
steps {
script {
def tag = BRANCH_NAME == 'main' ? 'latest' : "${COMMIT}"
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}"
}
}
}
}
}
}