Update Jenkinsfile

This commit is contained in:
Mihai Georgescu 2023-11-29 11:23:37 +02:00 committed by GitHub
parent 76f9ce0862
commit 3bda3fccf7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

77
Jenkinsfile vendored
View file

@ -7,7 +7,6 @@ pipeline {
stage('Checkstyle') { stage('Checkstyle') {
steps { steps {
sh 'export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64' sh 'export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64'
sh 'echo $JAVA_HOME'
sh 'mvn checkstyle:checkstyle' sh 'mvn checkstyle:checkstyle'
archiveArtifacts artifacts: 'checkstyle-result.html', onlyIfSuccessful: true archiveArtifacts artifacts: 'checkstyle-result.html', onlyIfSuccessful: true
} }
@ -37,31 +36,62 @@ pipeline {
script { script {
def dockerBuildOutput = sh(script: 'docker build -t imagine_spring_petclinic:0.1 .', returnStatus: true) def dockerBuildOutput = sh(script: 'docker build -t imagine_spring_petclinic:0.1 .', returnStatus: true)
if (dockerBuildOutput == 0) { if (dockerBuildOutput == 0) {
echo "Build successful" echo "Docker image creation successful"
} else { } else {
error "Build failed" // Optional: Terminate the pipeline with an error message if the build fails error "Docker image creation FAILED" // Optional: Terminate the pipeline with an error message if the build fails
} }
} }
} }
} }
stage('Tag the docker image') { stage('Tag the docker image for merge request repo - mr') {
steps { steps {
script {
echo "now we will tag the docker image" echo "now we will tag the docker image"
sh "docker tag imagine_spring_petclinic:0.1 mihaivalentingeorgescu/mr:0.1" when {
// Condition to execute the stage when the branch is main
expression {
return (env.CHANGE_ID == null && env.BRANCH_NAME == 'main')
}
}
script {
def imageTag = sh(script: 'docker tag imagine_spring_petclinic:0.1 mihaivalentingeorgescu/mr:0.1', returnStatus: true)
if (imageTag == 0) {
echo "Image tagged successfully"
} else {
error "Image tagging FAILED"
}
} }
} }
} }
stage('Login to Dockerhub') { stage('Login to Dockerhub') {
steps { steps {
echo "now we will login to dockerhub" echo "now we will login to dockerhub"
sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin' script {
def loginDocker = sh(script: 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR -p ', returnStatus: true)
if (loginDocker == 0) {
echo "Login ended successfully"
} else {
error "Login FAILED"
}
}
}
}
stage('Push to DockerHub to merge rewuest repo - mr') {
when {
// Condition to execute the stage when the branch is main
expression {
return (env.CHANGE_ID == null && env.BRANCH_NAME == 'main')
} }
} }
stage('Push to DockerHub') {
steps { steps {
echo "now we will push to the docker file" echo "now we will push to the docker file"
sh 'docker push mihaivalentingeorgescu/mr:0.1' script {
def pushToDocker = sh(script: 'docker push mihaivalentingeorgescu/mr:0.1', returnStatus: true)
if (pushToDocker == 0) {
echo "Push made successfully"
} else {
error "Docker push FAILED"
}
}
} }
} }
stage('Change stages') { stage('Change stages') {
@ -70,15 +100,40 @@ pipeline {
} }
} }
stage('Tag docker image again for the main repo') { stage('Tag docker image again for the main repo') {
when {
// Condition to execute the stage on a pull request event
expression {
return env.CHANGE_ID != null
}
}
steps { steps {
echo "now we will tag the docker image for the main branch" echo "now we will tag the docker image for the main branch"
sh "docker tag imagine_spring_petclinic:0.1 mihaivalentingeorgescu/main:0.1" script {
def tagDockerImage = sh(script: 'docker tag imagine_spring_petclinic:0.1 mihaivalentingeorgescu/main:0.1', returnStatus: true)
if (tagDockerImage == 0) {
echo "Docke tag ended successfully"
} else {
error "Docker tag FAILED"
}
}
} }
} }
stage('Push docker image to main repository') { stage('Push docker image to main repository') {
steps { steps {
echo "now we will push the image to the docker main repository" echo "now we will push the image to the docker main repository"
sh 'docker push mihaivalentingeorgescu/main:0.1' when {
// Condition to execute the stage on a pull request event
expression {
return env.CHANGE_ID != null
}
script {
def pushDockerImageToMain = sh(script: 'docker push mihaivalentingeorgescu/main:0.1', returnStatus: true)
if (pushDockerImageToMain == 0) {
echo "Docke tag ended successfully"
} else {
error "Docker tag FAILED"
}
}
} }
} }
} }