From 73d1376a73b8447fc678de4ac9332efd110f9510 Mon Sep 17 00:00:00 2001 From: JustFiesta Date: Sun, 28 Apr 2024 14:51:26 +0200 Subject: [PATCH] Jenkinsfile for building and pushing Docker image to Nexus Docker repo --- Jenkinsfile | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 071ce4f46..e4e9dd149 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,32 +1,37 @@ pipeline { agent any - - tools { - // Install the Maven version configured as "M3" and add it to the path. - gradle "8.6" + environment { + NEXUS_CREDS = credentials('nexus-cred') + NEXUS_DOCKER_REPO = '3.252.205.41:8085' } stages { - stage('Build') { + + stage('Docker Build') { + + steps { + echo 'Building docker Image' + sh 'docker build -t $NEXUS_DOCKER_REPO/spring-petclinic:${GIT_COMMIT} .' + } + } + + stage('Docker Login') { steps { - // Get some code from a GitHub repository - git 'https://github.com/jglick/simple-maven-project-with-tests.git' - - // Run Maven on a Unix agent. - sh "mvn -Dmaven.test.failure.ignore=true clean package" - - // To run Maven on a Windows agent, use - // bat "mvn -Dmaven.test.failure.ignore=true clean package" - } - - post { - // If Maven was able to run the tests, even if some of the test - // failed, record the test results and archive the jar file. - success { - junit '**/target/surefire-reports/TEST-*.xml' - archiveArtifacts 'target/*.jar' + 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' + } + } } } + + stage('Docker Push') { + steps { + echo 'Pushing Imgaet to docker hub' + sh 'docker push $NEXUS_DOCKER_REPO/spring-petclinic:${GIT_COMMIT}' + } + } } -} +} \ No newline at end of file