mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-21 15:25:49 +00:00
Update Jenkinsfile
This commit is contained in:
parent
ca9e4ce052
commit
aab630d6a3
1 changed files with 39 additions and 22 deletions
37
Jenkinsfile
vendored
37
Jenkinsfile
vendored
|
@ -1,5 +1,3 @@
|
||||||
#!/usr/bin/env groovy
|
|
||||||
|
|
||||||
pipeline {
|
pipeline {
|
||||||
agent any
|
agent any
|
||||||
|
|
||||||
|
@ -12,9 +10,13 @@ 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}"
|
|
||||||
|
// 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}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,10 +56,9 @@ pipeline {
|
||||||
stage('Docker Build & Push') {
|
stage('Docker Build & Push') {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
def dockerRepo = (env.GIT_BRANCH_NAME == 'main') ? 'main-jenkins' : 'mr-jenkins'
|
if (env.GIT_BRANCH_NAME == 'main') {
|
||||||
|
echo "Building Docker image for main-jenkins repo..."
|
||||||
echo "Building Docker image for ${dockerRepo} repo..."
|
sh "docker build -t ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT} ."
|
||||||
sh "docker build -t ${DOCKERHUB_USERNAME}/${dockerRepo}:${GIT_COMMIT_SHORT} ."
|
|
||||||
|
|
||||||
withCredentials([usernamePassword(
|
withCredentials([usernamePassword(
|
||||||
credentialsId: "${DOCKERHUB_CREDENTIALS}",
|
credentialsId: "${DOCKERHUB_CREDENTIALS}",
|
||||||
|
@ -67,9 +68,25 @@ 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}/${dockerRepo}:${GIT_COMMIT_SHORT}"
|
sh "docker push ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT}"
|
||||||
sh "docker tag ${DOCKERHUB_USERNAME}/${dockerRepo}:${GIT_COMMIT_SHORT} ${DOCKERHUB_USERNAME}/${dockerRepo}:latest"
|
sh "docker tag ${DOCKERHUB_USERNAME}/main-jenkins:${GIT_COMMIT_SHORT} ${DOCKERHUB_USERNAME}/main-jenkins:latest"
|
||||||
sh "docker push ${DOCKERHUB_USERNAME}/${dockerRepo}: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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue