diff --git a/Jenkinsfile b/Jenkinsfile index 4726067fa..5e5b4f72b 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -4,7 +4,6 @@ pipeline { environment { NEXUS_CREDS = credentials('nexus-cred') NEXUS_DOCKER_REPO_MR = '34.241.46.54:8085' - NEXUS_DOCKER_REPO_MAIN = '34.241.46.54:8084' } tools { @@ -35,8 +34,7 @@ pipeline { sh './gradlew build -x test -x check -x checkFormat -x processTestAot --no-daemon' archiveArtifacts artifacts: 'build/libs/*.jar', fingerprint: true } } - stage('Docker Build') { - + stage('Docker Build (MR)') { steps { echo 'Building docker Image' sh 'docker build -t $NEXUS_DOCKER_REPO_MR/spring-petclinic:${GIT_COMMIT} .' @@ -53,7 +51,7 @@ pipeline { } } } - stage('Docker Push') { + stage('Docker Push (MR)') { steps { echo 'Pushing Image to docker hub' sh 'docker push $NEXUS_DOCKER_REPO_MR/spring-petclinic:${GIT_COMMIT}' @@ -61,6 +59,55 @@ pipeline { } } + post { + always { + cleanWs() + } + } +} + +// Main branch pipeline +pipeline { + agent any + + environment { + NEXUS_CREDS = credentials('nexus-cred') + NEXUS_DOCKER_REPO_MAIN = '34.241.46.54:8084' + } + + stage('Docker Build (Main)') { + when { + branch 'main' + } + steps { + echo 'Building docker Image' + sh 'docker build -t $NEXUS_DOCKER_REPO_MAIN/spring-petclinic:${GIT_COMMIT} .' + } + } + stage('Docker Login') { + when { + branch 'main' + } + steps { + echo 'Nexus Docker Repository Login' + script{ + withCredentials([usernamePassword(credentialsId: 'nexus-cred', usernameVariable: 'USER', passwordVariable: 'PASS' )]){ + sh 'echo $PASS | docker login -u $USER --password-stdin $NEXUS_DOCKER_REPO_MAIN' + } + + } + } + } + stage('Docker Push (Main)') { + when { + branch 'main' + } + steps { + echo 'Pushing Image to docker hub' + sh 'docker push $NEXUS_DOCKER_REPO_MAIN/spring-petclinic:${GIT_COMMIT}' + } + } + post { always { cleanWs()