mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 07:45:49 +00:00
Update Jenkinsfile
This commit is contained in:
parent
708cb3b8b9
commit
ca9e4ce052
1 changed files with 15 additions and 40 deletions
51
Jenkinsfile
vendored
51
Jenkinsfile
vendored
|
@ -12,9 +12,7 @@ pipeline {
|
||||||
stage('Prepare') {
|
stage('Prepare') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// Grab branch name
|
|
||||||
env.GIT_BRANCH_NAME = sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
|
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()
|
env.GIT_COMMIT_SHORT = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
|
||||||
echo "Branch: ${env.GIT_BRANCH_NAME}"
|
echo "Branch: ${env.GIT_BRANCH_NAME}"
|
||||||
echo "Commit: ${env.GIT_COMMIT_SHORT}"
|
echo "Commit: ${env.GIT_COMMIT_SHORT}"
|
||||||
|
@ -29,7 +27,6 @@ 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,44 +54,22 @@ pipeline {
|
||||||
stage('Docker Build & Push') {
|
stage('Docker Build & Push') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
// Decide which repo to push to based on the branch
|
def dockerRepo = (env.GIT_BRANCH_NAME == 'main') ? 'main-jenkins' : 'mr-jenkins'
|
||||||
if (env.GIT_BRANCH_NAME == 'main') {
|
|
||||||
// main branch
|
|
||||||
echo "Building Docker image for MAIN repo..."
|
|
||||||
sh "docker build -t ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT} ."
|
|
||||||
|
|
||||||
// Login & push
|
echo "Building Docker image for ${dockerRepo} repo..."
|
||||||
withCredentials([usernamePassword(
|
sh "docker build -t ${DOCKERHUB_USERNAME}/${dockerRepo}:${GIT_COMMIT_SHORT} ."
|
||||||
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}"
|
withCredentials([usernamePassword(
|
||||||
// Optional: push latest tag
|
credentialsId: "${DOCKERHUB_CREDENTIALS}",
|
||||||
sh "docker tag ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT} ${DOCKERHUB_USERNAME}/main-jenkins:latest"
|
usernameVariable: 'DOCKER_USER',
|
||||||
sh "docker push ${DOCKERHUB_USERNAME}/main-jenkins:latest"
|
passwordVariable: 'DOCKER_PASS'
|
||||||
|
)]) {
|
||||||
} else {
|
sh "echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin"
|
||||||
// merge-request or feature branch
|
|
||||||
echo "Building Docker image for MR 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}"
|
|
||||||
// Optional: push latest tag
|
|
||||||
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