Update Jenkinsfile

This commit is contained in:
lwj9812 2024-03-22 12:07:53 +09:00 committed by GitHub
parent a332ddf09d
commit 72cd1612fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

49
Jenkinsfile vendored
View file

@ -5,6 +5,15 @@ pipeline {
maven "M3" maven "M3"
} }
environment {
AWS_CREDENTIAL_NAME = "AWSCredentials"pipeline {
agent any
tools {
jdk "jdk17"
maven "M3"
}
environment { environment {
AWS_CREDENTIAL_NAME = "AWSCredentials" AWS_CREDENTIAL_NAME = "AWSCredentials"
REGION = "ap-northeast-2" REGION = "ap-northeast-2"
@ -14,37 +23,33 @@ pipeline {
} }
stages { stages {
stage('Git clone') { stage('Git Clone') {
steps { steps {
echo 'Git clone' echo 'Git Clone'
git url: 'https://github.com/lwj9812/spring-petclinic.git', git url: 'https://github.com/lwj9812/spring-petclinic.git',
branch: 'efficient-webjars' branch: 'efficient-webjars'
} }
post { post {
success { success {
echo 'Git Clone Success!!' echo 'success clone project'
} }
failure { failure {
echo 'Git Clone Fail' error 'fail clone project' // exit pipeline
} }
} }
} }
stage ('mvn Build') {
stage('Maven Build') {
steps { steps {
echo 'Maven Build' sh 'mvn -Dmaven.test.failure.ignore=true install'
sh 'mvn -Dmaven.test.failure.ignore=true clean package'
} }
post { post {
success { success {
junit 'target/surefire-reports/**/*.xml' junit '**/target/surefire-reports/TEST-*.xml'
} }
} }
} }
stage ('Docker Build') {
stage ('Docker Image Build') {
steps { steps {
echo 'Docker Image Build'
dir("${env.WORKSPACE}") { dir("${env.WORKSPACE}") {
sh """ sh """
docker build -t $ECR_DOCKER_IMAGE:$BUILD_NUMBER . docker build -t $ECR_DOCKER_IMAGE:$BUILD_NUMBER .
@ -53,24 +58,29 @@ pipeline {
} }
} }
} }
stage('Push Docker Image') {
stage ('Push Docker Image to ECR') {
steps { steps {
echo "Push Docker Image to ECR" echo "Push Docker Image to ECR"
script{ script{
// cleanup current user docker credentials
sh 'rm -f ~/.dockercfg ~/.docker/config.json || true' sh 'rm -f ~/.dockercfg ~/.docker/config.json || true'
docker.withRegistry("http://${ECR_REPOSITORY}", "ecr:${REGION}:${AWS_CREDENTIAL_NAME}") { docker.withRegistry("https://${ECR_REPOSITORY}", "ecr:${REGION}:${AWS_CREDENTIAL_NAME}") {
docker.image("${ECR_DOCKER_IMAGE}:${BUILD_NUMBER}").push() docker.image("${ECR_DOCKER_IMAGE}:${BUILD_NUMBER}").push()
docker.image("${ECR_DOCKER_IMAGE}:latest").push() docker.image("${ECR_DOCKER_IMAGE}:latest").push()
} }
}
}
}
}
}
post {
success {
echo "Push Docker Image success!"
}
}
}
stage('Clean Up Docker Images on Jenkins Server') { stage('Clean Up Docker Images on Jenkins Server') {
steps { steps {
echo 'Cleaning up unused Docker images on Jenkins server' echo 'Cleaning up unused Docker images on Jenkins server'
sh "docker image prune -a -f" sh "docker image prune -f -a"
} }
} }
stage('Upload to S3') { stage('Upload to S3') {
@ -86,5 +96,6 @@ pipeline {
} }
} }
} }
} }