Jenkinsfile for building and pushing Docker image to Nexus Docker repo

This commit is contained in:
JustFiesta 2024-04-28 14:51:26 +02:00
parent 664a740b9a
commit 73d1376a73

43
Jenkinsfile vendored
View file

@ -1,31 +1,36 @@
pipeline { pipeline {
agent any agent any
environment {
tools { NEXUS_CREDS = credentials('nexus-cred')
// Install the Maven version configured as "M3" and add it to the path. NEXUS_DOCKER_REPO = '3.252.205.41:8085'
gradle "8.6"
} }
stages { stages {
stage('Build') {
stage('Docker Build') {
steps { steps {
// Get some code from a GitHub repository echo 'Building docker Image'
git 'https://github.com/jglick/simple-maven-project-with-tests.git' sh 'docker build -t $NEXUS_DOCKER_REPO/spring-petclinic:${GIT_COMMIT} .'
}
// 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 { stage('Docker Login') {
// If Maven was able to run the tests, even if some of the test steps {
// failed, record the test results and archive the jar file. echo 'Nexus Docker Repository Login'
success { script{
junit '**/target/surefire-reports/TEST-*.xml' withCredentials([usernamePassword(credentialsId: 'nexus-cred', usernameVariable: 'USER', passwordVariable: 'PASS' )]){
archiveArtifacts 'target/*.jar' 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}'
} }
} }
} }