mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-19 22:35:50 +00:00
Update Jenkinsfile
This commit is contained in:
parent
b5984edbe6
commit
e76b364a65
1 changed files with 40 additions and 45 deletions
85
Jenkinsfile
vendored
85
Jenkinsfile
vendored
|
@ -1,110 +1,105 @@
|
||||||
pipeline {
|
pipeline {
|
||||||
agent any
|
agent any
|
||||||
|
|
||||||
environment {
|
environment {
|
||||||
DOCKER_REPO_MAIN = 'marijastopa/main-jenkins'
|
DOCKER_IMAGE_MAIN = 'marijastopa/main-jenkins'
|
||||||
DOCKER_REPO_MR = 'marijastopa/mr-jenkins'
|
DOCKER_IMAGE_MR = 'marijastopa/mr-jenkins'
|
||||||
GIT_COMMIT_SHORT = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stages {
|
stages {
|
||||||
stage('Prepare') {
|
stage('Prepare') {
|
||||||
steps {
|
steps {
|
||||||
echo "Branch: ${env.BRANCH_NAME}"
|
echo "Preparing environment..."
|
||||||
echo "Commit: ${GIT_COMMIT_SHORT}"
|
script {
|
||||||
|
BRANCH_NAME = env.BRANCH_NAME ?: 'main'
|
||||||
|
COMMIT = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
|
||||||
|
echo "Branch: ${BRANCH_NAME}"
|
||||||
|
echo "Commit: ${COMMIT}"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Jobs for Merge Requests
|
|
||||||
stage('Checkstyle') {
|
stage('Checkstyle') {
|
||||||
when {
|
when {
|
||||||
expression {
|
expression {
|
||||||
return env.BRANCH_NAME != 'main'
|
BRANCH_NAME != 'main'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
steps {
|
steps {
|
||||||
echo "Running Checkstyle..."
|
echo "Running Checkstyle..."
|
||||||
sh './gradlew checkstyleMain checkstyleTest'
|
sh './gradlew checkstyleMain checkstyleTest'
|
||||||
archiveArtifacts artifacts: '**/build/reports/checkstyle/*.xml', allowEmptyArchive: true
|
archiveArtifacts artifacts: '**/build/reports/checkstyle/*.xml', allowEmptyArchive: true
|
||||||
echo "Checkstyle completed and reports archived"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Test') {
|
stage('Test') {
|
||||||
when {
|
when {
|
||||||
expression {
|
expression {
|
||||||
return env.BRANCH_NAME != 'main'
|
BRANCH_NAME != 'main'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
steps {
|
steps {
|
||||||
echo "Running Tests..."
|
echo "Running tests..."
|
||||||
sh './gradlew test'
|
sh './gradlew test'
|
||||||
junit '**/build/test-results/test/*.xml'
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Build without Tests') {
|
stage('Build without Tests') {
|
||||||
when {
|
when {
|
||||||
expression {
|
expression {
|
||||||
return env.BRANCH_NAME != 'main'
|
BRANCH_NAME != 'main'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
steps {
|
steps {
|
||||||
echo "Building without running tests..."
|
echo "Building application (without tests)..."
|
||||||
sh './gradlew clean build -x test'
|
sh './gradlew clean build -x test'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Docker Build & Push (Merge Request)') {
|
stage('Docker Build & Push (Merge Request)') {
|
||||||
when {
|
when {
|
||||||
expression {
|
expression {
|
||||||
return env.BRANCH_NAME != 'main'
|
BRANCH_NAME != 'main'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
steps {
|
steps {
|
||||||
echo "Building Docker image for MR..."
|
echo "Building Docker image for merge request..."
|
||||||
sh "docker build -t ${DOCKER_REPO_MR}:${GIT_COMMIT_SHORT} ."
|
withCredentials([usernamePassword(credentialsId: 'dockerhub-credentials', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
|
||||||
withCredentials([string(credentialsId: 'dockerhub-credentials', variable: 'DOCKER_PASS')]) {
|
sh """
|
||||||
sh '''
|
docker build -t $DOCKER_IMAGE_MR:${COMMIT} .
|
||||||
echo $DOCKER_PASS | docker login -u marijastopa --password-stdin
|
echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
|
||||||
docker push ${DOCKER_REPO_MR}:${GIT_COMMIT_SHORT}
|
docker push $DOCKER_IMAGE_MR:${COMMIT}
|
||||||
docker tag ${DOCKER_REPO_MR}:${GIT_COMMIT_SHORT} ${DOCKER_REPO_MR}:latest
|
docker tag $DOCKER_IMAGE_MR:${COMMIT} $DOCKER_IMAGE_MR:latest
|
||||||
docker push ${DOCKER_REPO_MR}:latest
|
docker push $DOCKER_IMAGE_MR:latest
|
||||||
'''
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Jobs for Main Branch
|
|
||||||
stage('Docker Build & Push (Main Branch)') {
|
stage('Docker Build & Push (Main Branch)') {
|
||||||
when {
|
when {
|
||||||
branch 'main'
|
branch 'main'
|
||||||
}
|
}
|
||||||
steps {
|
steps {
|
||||||
echo "Building Docker image for main branch..."
|
echo "Building Docker image for main branch..."
|
||||||
sh "docker build -t ${DOCKER_REPO_MAIN}:${GIT_COMMIT_SHORT} ."
|
withCredentials([usernamePassword(credentialsId: 'dockerhub-credentials', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
|
||||||
withCredentials([string(credentialsId: 'dockerhub-credentials', variable: 'DOCKER_PASS')]) {
|
sh """
|
||||||
sh '''
|
docker build -t $DOCKER_IMAGE_MAIN:${COMMIT} .
|
||||||
echo $DOCKER_PASS | docker login -u marijastopa --password-stdin
|
echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
|
||||||
docker push ${DOCKER_REPO_MAIN}:${GIT_COMMIT_SHORT}
|
docker push $DOCKER_IMAGE_MAIN:${COMMIT}
|
||||||
docker tag ${DOCKER_REPO_MAIN}:${GIT_COMMIT_SHORT} ${DOCKER_REPO_MAIN}:latest
|
docker tag $DOCKER_IMAGE_MAIN:${COMMIT} $DOCKER_IMAGE_MAIN:latest
|
||||||
docker push ${DOCKER_REPO_MAIN}:latest
|
docker push $DOCKER_IMAGE_MAIN:latest
|
||||||
'''
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
post {
|
post {
|
||||||
always {
|
always {
|
||||||
echo "Cleaning up Docker images..."
|
echo "Cleaning up Docker images..."
|
||||||
sh 'docker rmi $(docker images -f "dangling=true" -q) || true'
|
sh """
|
||||||
}
|
docker ps -a -q | xargs docker rm -f || true
|
||||||
success {
|
docker images -f dangling=true -q | xargs docker rmi -f || true
|
||||||
echo "Pipeline completed successfully!"
|
"""
|
||||||
}
|
}
|
||||||
failure {
|
failure {
|
||||||
echo "Pipeline failed. Check logs for errors."
|
echo 'Pipeline failed. Check logs for errors.'
|
||||||
|
}
|
||||||
|
success {
|
||||||
|
echo 'Pipeline executed successfully!'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue