mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-19 14:25:50 +00:00
Update Jenkinsfile
This commit is contained in:
parent
ca9e4ce052
commit
aab630d6a3
1 changed files with 39 additions and 22 deletions
61
Jenkinsfile
vendored
61
Jenkinsfile
vendored
|
@ -1,5 +1,3 @@
|
|||
#!/usr/bin/env groovy
|
||||
|
||||
pipeline {
|
||||
agent any
|
||||
|
||||
|
@ -12,14 +10,18 @@ pipeline {
|
|||
stage('Prepare') {
|
||||
steps {
|
||||
script {
|
||||
// Grab branch name
|
||||
env.GIT_BRANCH_NAME = sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
|
||||
// Grab short commit
|
||||
env.GIT_COMMIT_SHORT = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
|
||||
echo "Branch: ${env.GIT_BRANCH_NAME}"
|
||||
|
||||
// Add this line for branch name confirmation
|
||||
echo "Detected Branch: ${env.GIT_BRANCH_NAME}"
|
||||
echo "Commit: ${env.GIT_COMMIT_SHORT}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
stage('Checkstyle') {
|
||||
when {
|
||||
expression { return env.GIT_BRANCH_NAME != 'main' }
|
||||
|
@ -30,7 +32,7 @@ pipeline {
|
|||
archiveArtifacts artifacts: 'build/reports/checkstyle/*.xml', fingerprint: true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
stage('Test') {
|
||||
when {
|
||||
expression { return env.GIT_BRANCH_NAME != 'main' }
|
||||
|
@ -40,7 +42,7 @@ pipeline {
|
|||
sh "./gradlew test"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
stage('Build without Tests') {
|
||||
when {
|
||||
expression { return env.GIT_BRANCH_NAME != 'main' }
|
||||
|
@ -50,26 +52,41 @@ pipeline {
|
|||
sh "./gradlew clean build -x test"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
stage('Docker Build & Push') {
|
||||
steps {
|
||||
script {
|
||||
def dockerRepo = (env.GIT_BRANCH_NAME == 'main') ? 'main-jenkins' : 'mr-jenkins'
|
||||
|
||||
echo "Building Docker image for ${dockerRepo} repo..."
|
||||
sh "docker build -t ${DOCKERHUB_USERNAME}/${dockerRepo}:${GIT_COMMIT_SHORT} ."
|
||||
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: "${DOCKERHUB_CREDENTIALS}",
|
||||
usernameVariable: 'DOCKER_USER',
|
||||
passwordVariable: 'DOCKER_PASS'
|
||||
)]) {
|
||||
sh "echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin"
|
||||
if (env.GIT_BRANCH_NAME == 'main') {
|
||||
echo "Building Docker image for main-jenkins repo..."
|
||||
sh "docker build -t ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT} ."
|
||||
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: "${DOCKERHUB_CREDENTIALS}",
|
||||
usernameVariable: 'DOCKER_USER',
|
||||
passwordVariable: 'DOCKER_PASS'
|
||||
)]) {
|
||||
sh "echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin"
|
||||
}
|
||||
|
||||
sh "docker push ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT}"
|
||||
sh "docker tag ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT} ${DOCKERHUB_USERNAME}/main-jenkins:latest"
|
||||
sh "docker push ${DOCKERHUB_USERNAME}/main-jenkins:latest"
|
||||
} else {
|
||||
echo "Building Docker image for mr-jenkins repo..."
|
||||
sh "docker build -t ${DOCKERHUB_USERNAME}/mr-jenkins:${GIT_COMMIT_SHORT} ."
|
||||
|
||||
withCredentials([usernamePassword(
|
||||
credentialsId: "${DOCKERHUB_CREDENTIALS}",
|
||||
usernameVariable: 'DOCKER_USER',
|
||||
passwordVariable: 'DOCKER_PASS'
|
||||
)]) {
|
||||
sh "echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin"
|
||||
}
|
||||
|
||||
sh "docker push ${DOCKERHUB_USERNAME}/mr-jenkins:${GIT_COMMIT_SHORT}"
|
||||
sh "docker tag ${DOCKERHUB_USERNAME}/mr-jenkins:${GIT_COMMIT_SHORT} ${DOCKERHUB_USERNAME}/mr-jenkins:latest"
|
||||
sh "docker push ${DOCKERHUB_USERNAME}/mr-jenkins:latest"
|
||||
}
|
||||
|
||||
sh "docker push ${DOCKERHUB_USERNAME}/${dockerRepo}:${GIT_COMMIT_SHORT}"
|
||||
sh "docker tag ${DOCKERHUB_USERNAME}/${dockerRepo}:${GIT_COMMIT_SHORT} ${DOCKERHUB_USERNAME}/${dockerRepo}:latest"
|
||||
sh "docker push ${DOCKERHUB_USERNAME}/${dockerRepo}:latest"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue