From 79eb4625f17adc7bd536f703217b6a41bc469820 Mon Sep 17 00:00:00 2001 From: Memogoz Date: Thu, 12 Jun 2025 16:29:30 -0600 Subject: [PATCH] feat: add Jenkinsfile and modify Dockerfile.two --- Dockerfile.two | 8 ++--- Jenkinsfile | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 Jenkinsfile diff --git a/Dockerfile.two b/Dockerfile.two index 295456ea3..7f2afecf6 100644 --- a/Dockerfile.two +++ b/Dockerfile.two @@ -4,9 +4,9 @@ WORKDIR /app COPY . . -RUN ["mvn", "clean", "install"] +RUN ["mvn", "clean", "install", "-DskipTests"] -FROM eclipse-temurin +FROM gcr.io/distroless/java21-debian12 WORKDIR /app @@ -14,6 +14,4 @@ COPY --from=builder /app/target/spring-petclinic-*.jar app.jar EXPOSE 8080 -ENTRYPOINT ["java","-jar","app.jar"] - - +ENTRYPOINT ["java","-jar","app.jar"] \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..eb8b69c59 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,85 @@ +pipeline { + agent any + + environment { + GIT_COMMIT_SHORT = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim() + } + + stages { + stage('Checkout') { + steps { + checkout scm + } + } + + // === Merge Request (MR) jobs only === + stage('Checkstyle') { + when { + expression { return env.CHANGE_ID != null } + } + steps { + sh 'mvn checkstyle:checkstyle' + } + post { + always { + archiveArtifacts artifacts: 'target/checkstyle-result.xml', fingerprint: true + } + } + } + + stage('Test') { + when { + expression { return env.CHANGE_ID != null } + } + steps { + sh 'mvn test' + } + } + + stage('Build (no tests)') { + when { + expression { return env.CHANGE_ID != null } + } + steps { + sh 'mvn package -DskipTests' + } + } + + // === Docker build and push for both MR and main === + stage('Build & Push Docker Image') { + steps { + script { + def isMR = env.CHANGE_ID != null + + // Define repo and tag based on context + def repo = isMR ? "ggonzalezx/mr" : "ggonzalezx/main" + + def tag = isMR ? "${GIT_COMMIT_SHORT}" : "latest" + + def fullImageName = "${repo}:${tag}" + + echo "Building Docker image with Dockerfile.two: ${fullImageName}" + + // Build using Dockerfile.two + docker.build(fullImageName, "-f Dockerfile.two .") + + // Authenticate and push to Docker Hub + docker.withRegistry('https://index.docker.io/v1/', 'dockerhub-credentials') { + docker.image(fullImageName).push() + } + + echo "Docker image pushed to: https://hub.docker.com/repository/docker/${repo}" + } + } + } + } + + post { + success { + echo "Pipeline SUCCESS for ${env.BRANCH_NAME} (${env.CHANGE_ID ?: 'not an MR'})" + } + failure { + echo "Pipeline FAILURE for ${env.BRANCH_NAME} (${env.CHANGE_ID ?: 'not an MR'})" + } + } +}