mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-19 22:35:50 +00:00
Update Jenkinsfile
This commit is contained in:
parent
aab630d6a3
commit
522d1c380e
1 changed files with 16 additions and 11 deletions
27
Jenkinsfile
vendored
27
Jenkinsfile
vendored
|
@ -1,3 +1,5 @@
|
||||||
|
#!/usr/bin/env groovy
|
||||||
|
|
||||||
pipeline {
|
pipeline {
|
||||||
agent any
|
agent any
|
||||||
|
|
||||||
|
@ -10,18 +12,15 @@ pipeline {
|
||||||
stage('Prepare') {
|
stage('Prepare') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// Grab branch name
|
// Use Jenkins environment variable or fallback to git command
|
||||||
env.GIT_BRANCH_NAME = sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
|
env.GIT_BRANCH_NAME = env.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()
|
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}"
|
echo "Commit: ${env.GIT_COMMIT_SHORT}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Checkstyle') {
|
stage('Checkstyle') {
|
||||||
when {
|
when {
|
||||||
expression { return env.GIT_BRANCH_NAME != 'main' }
|
expression { return env.GIT_BRANCH_NAME != 'main' }
|
||||||
|
@ -29,10 +28,11 @@ pipeline {
|
||||||
steps {
|
steps {
|
||||||
echo 'Running Checkstyle...'
|
echo 'Running Checkstyle...'
|
||||||
sh "./gradlew checkstyleMain checkstyleTest"
|
sh "./gradlew checkstyleMain checkstyleTest"
|
||||||
|
// Archive checkstyle reports as artifacts
|
||||||
archiveArtifacts artifacts: 'build/reports/checkstyle/*.xml', fingerprint: true
|
archiveArtifacts artifacts: 'build/reports/checkstyle/*.xml', fingerprint: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Test') {
|
stage('Test') {
|
||||||
when {
|
when {
|
||||||
expression { return env.GIT_BRANCH_NAME != 'main' }
|
expression { return env.GIT_BRANCH_NAME != 'main' }
|
||||||
|
@ -42,7 +42,7 @@ pipeline {
|
||||||
sh "./gradlew test"
|
sh "./gradlew test"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Build without Tests') {
|
stage('Build without Tests') {
|
||||||
when {
|
when {
|
||||||
expression { return env.GIT_BRANCH_NAME != 'main' }
|
expression { return env.GIT_BRANCH_NAME != 'main' }
|
||||||
|
@ -52,14 +52,17 @@ pipeline {
|
||||||
sh "./gradlew clean build -x test"
|
sh "./gradlew clean build -x test"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Docker Build & Push') {
|
stage('Docker Build & Push') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
|
// Decide which repo to push to based on the branch
|
||||||
if (env.GIT_BRANCH_NAME == 'main') {
|
if (env.GIT_BRANCH_NAME == 'main') {
|
||||||
|
// Main branch
|
||||||
echo "Building Docker image for main-jenkins repo..."
|
echo "Building Docker image for main-jenkins repo..."
|
||||||
sh "docker build -t ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT} ."
|
sh "docker build -t ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT} ."
|
||||||
|
|
||||||
|
// Login & push
|
||||||
withCredentials([usernamePassword(
|
withCredentials([usernamePassword(
|
||||||
credentialsId: "${DOCKERHUB_CREDENTIALS}",
|
credentialsId: "${DOCKERHUB_CREDENTIALS}",
|
||||||
usernameVariable: 'DOCKER_USER',
|
usernameVariable: 'DOCKER_USER',
|
||||||
|
@ -67,11 +70,13 @@ pipeline {
|
||||||
)]) {
|
)]) {
|
||||||
sh "echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin"
|
sh "echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin"
|
||||||
}
|
}
|
||||||
|
|
||||||
sh "docker push ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT}"
|
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 tag ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT} ${DOCKERHUB_USERNAME}/main-jenkins:latest"
|
||||||
sh "docker push ${DOCKERHUB_USERNAME}/main-jenkins:latest"
|
sh "docker push ${DOCKERHUB_USERNAME}/main-jenkins:latest"
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
// Feature or merge-request branch
|
||||||
echo "Building Docker image for mr-jenkins repo..."
|
echo "Building Docker image for mr-jenkins repo..."
|
||||||
sh "docker build -t ${DOCKERHUB_USERNAME}/mr-jenkins:${GIT_COMMIT_SHORT} ."
|
sh "docker build -t ${DOCKERHUB_USERNAME}/mr-jenkins:${GIT_COMMIT_SHORT} ."
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue