Skip to content

Testing #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Jenkins/Jenkinsfile-CD
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
pipeline {
agent any

parameters {
string(name: 'eureka-services', defaultValue: 'main')
string(name: 'admin-server', defaultValue: 'main')
string(name: 'zipkin', defaultValue: 'main')
string(name: 'api-gateway', defaultValue: 'main')
string(name: 'customers-services', defaultValue: 'main')
string(name: 'genai-services', defaultValue: 'main')
string(name: 'vets-services', defaultValue: 'main')
string(name: 'visits-services', defaultValue: 'main')
}

environment {
REPO_URL = 'https://github.com/vominh-source/spring-petclinic-microservices.git'
IMAGE_NAME = '22127475/devops-project02'
}



stages {
stage('Deploy services') {
steps {
script {
services = [
[name: 'eureka-services', branch: params.eureka-services],
[name: 'admin-server', branch: params.admin-server],
[name: 'zipkin', branch: params.zipkin],
[name: 'api-gateway', branch: params.api-gateway]
[name: 'customers-services', branch: params.customers-services],
[name: 'visits-services', branch: params.visits-services],
[name: 'vets-services', branch: params.vets-services],
[name: 'genai-services', branch: params.genai-services]
]

tags = []
for (service in services) {
tags.append(tag.branch == 'main' ? 'latest' : sh(script: "git ls-remote ${REPO_URL} refs/heads/${services.branch} | cut -f1", returnStdout: true).trim())

echo "tags: ${tags[-1]}"
}
}
}
}

stage('Pull images') {
steps {
script {
for (tag in tags)
sh "docker image pull ${IMAGE_NAME}:${tag}"
echo "pulled image: ${IMAGE_NAME}:${tag}"
}
}
}



}


}
94 changes: 94 additions & 0 deletions Jenkins/Jenkinsfile-CI
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Jenkinsfile
pipeline {
agent any
// {
// label '!built-in'
// }


options {
timeout(time: 90, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '10'))
disableConcurrentBuilds()
}

environment {
DOCKERHUB_CREDENTIALS_ID = 'dockerhub'
DOCKER_REGISTRY = '22127146'
}

stages {
stage('Prepare Workspace') {
steps {
echo 'Cleaning workspace...'
cleanWs()
checkout scm
}
}

stage('Initialize') {
steps {
script {
// Lấy commit ID
if (env.GIT_COMMIT) {
env.COMMIT_ID = env.GIT_COMMIT
} else {
//env.COMMIT_ID = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
env.COMMIT_ID = bat(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
}
// In thông tin
env.BRANCH_NAME = env.BRANCH_NAME
echo "Building branch: ${env.BRANCH_NAME}"
echo "Commit ID: ${env.COMMIT_ID}"
echo "Docker Hub Credentials ID: ${env.DOCKERHUB_CREDENTIALS_ID}"
echo "Docker Registry (for image prefix): ${env.DOCKER_REGISTRY}"
}
}
}

stage('Build and Push Images to Docker Hub') {
steps {
script{
docker.withRegistry("https://index.docker.io/v1/", env.DOCKERHUB_CREDENTIALS_ID){
try{

def commit_id = env.COMMIT_ID

if (env.BRANCH_NAME == 'main') {
commit_id = 'latest'
}

def mvnCommand = "./mvnw.cmd clean install -P buildDocker -DskipTests "+
"-Ddocker.image.prefix=${env.DOCKER_REGISTRY} "+
"-Ddocker.image.tag.commit=${commit_id} "+
"-Dcontainer.build.extraarg=\"--push\""

echo "Executing Maven command on Windows to build images: ${mvnCommand}"
//sh mvnCommand // Thực thi lệnh build
bat mvnCommand

echo "Maven build completed successfully."
}
catch (e) {
echo "Error building images via Maven: ${e.getMessage()}"
error(message: "Failed to build images via Maven")
}
}
}
}
}
}

post {
always {
echo 'CI Pipeline finished.'
}
success {
// Cập nhật thông báo thành công
echo "Successfully built, tagged (${env.COMMIT_ID}), and pushed images for branch ${env.BRANCH_NAME}"
}
failure {
echo "Pipeline failed for commit ${env.COMMIT_ID} on branch ${env.BRANCH_NAME}"
}
}
}
144 changes: 144 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
pipeline {
agent any

stages {
stage('Checkout') {
steps {
checkout scm
}
}

stage('Determine Build') {
steps {
script {
// Lấy danh sách các file đã thay đổi
env.CHANGED_FILES = sh(returnStdout: true, script: 'git diff --name-only HEAD^ HEAD').trim()
// Xác định service nào cần build/test
def servicesToBuild = determineService(env.CHANGED_FILES.readLines())

// Kiểm tra xem có thay đổi nào nằm ngoài các thư mục service không
def changedOutsideServices = env.CHANGED_FILES.readLines().any { filePath ->
!servicesToBuild.any { service -> filePath.contains(service) }
}

if (changedOutsideServices) {
env.SERVICES_TO_BUILD = "all" // Build tất cả nếu có thay đổi bên ngoài
} else {
env.SERVICES_TO_BUILD = servicesToBuild // Sử dụng kết quả từ hàm determineServices
}

echo "Services to build: ${env.SERVICES_TO_BUILD}" // In ra để kiểm tra
}
}
}

stage('Test') {
when {
expression { env.SERVICES_TO_BUILD != null }
}
steps {
script {
if (env.SERVICES_TO_BUILD instanceof String && env.SERVICES_TO_BUILD != 'all') {
// Test 1 service
echo "Testing service: ${env.SERVICES_TO_BUILD}"
sh "./mvnw -f ${env.SERVICES_TO_BUILD}/pom.xml test"

// JUnit report
junit(
testResults: "${env.SERVICES_TO_BUILD}/target/surefire-reports/*.xml",
allowEmptyResults: true
)

} else if (env.SERVICES_TO_BUILD == 'all'){
// Test all services
echo "Testing all services"
sh "./mvnw test"

// JUnit report
junit(
testResults: "**/target/surefire-reports/*.xml",
allowEmptyResults: true
)

}
}
}
}

stage('Code Coverage') {
when {
expression { env.SERVICES_TO_BUILD != null }
}
steps {
script {
if (env.SERVICES_TO_BUILD instanceof String && env.SERVICES_TO_BUILD != 'all') {
// Code coverage cho 1 service
echo "Generating code coverage for service: ${env.SERVICES_TO_BUILD}"
sh "./mvnw -f ${env.SERVICES_TO_BUILD}/pom.xml org.jacoco:jacoco-maven-plugin:report"
recordCoverage(
tools: [[parser: 'JACOCO', pattern: "${env.SERVICES_TO_BUILD}/target/site/jacoco/**/*.xml"]]
)
} else if(env.SERVICES_TO_BUILD == 'all'){
// Test all services
echo "Generating code coverage for all services"
sh "./mvnw org.jacoco:jacoco-maven-plugin:report"
recordCoverage(
tools: [[parser: 'JACOCO', pattern: "**/target/site/jacoco/**/*.xml"]]
)

}
}
}
}

stage('Build') {
when {
expression { env.SERVICES_TO_BUILD != null }
}
steps {
script {
if (env.SERVICES_TO_BUILD instanceof String && env.SERVICES_TO_BUILD != 'all') {
// Build 1 service
echo "Building service: ${env.SERVICES_TO_BUILD}"
sh "./mvnw -f ${env.SERVICES_TO_BUILD}/pom.xml clean install -DskipTests"
archiveArtifacts artifacts: "${env.SERVICES_TO_BUILD}/target/*.jar"

} else if (env.SERVICES_TO_BUILD == 'all'){
// Build all services
echo "Building all services"
sh "./mvnw clean install -DskipTests"
archiveArtifacts artifacts: "**/target/*.jar"

}
}
}
}
}

post {
always {
echo "Finished pipeline"
}
}
}

// Function to determine which services to build/test
def determineService(changedFiles) {
println "Changed Files: ${changedFiles}"
def services = [
'spring-petclinic-customers-service',
'spring-petclinic-vets-service',
'spring-petclinic-visits-service',
'spring-petclinic-api-gateway',
'spring-petclinic-admin-server'
]
// Không cần danh sách servicesToBuild nữa, vì chỉ cần tìm một

for (service in services) {
if (changedFiles.any { it.contains(service) }) {
return service // Trả về ngay khi tìm thấy service đầu tiên
}
}

return null // Trả về null nếu không tìm thấy
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Distributed version of the Spring PetClinic Sample Application built with Spring Cloud and Spring AI
# Update for webhook

[![Build Status](https://github.com/spring-petclinic/spring-petclinic-microservices/actions/workflows/maven-build.yml/badge.svg)](https://github.com/spring-petclinic/spring-petclinic-microservices/actions/workflows/maven-build.yml)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
Expand Down
21 changes: 20 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Expand Down Expand Up @@ -186,7 +205,7 @@
<argument>${container.platform}</argument>
<argument>${container.build.extraarg}</argument>
<argument>-t</argument>
<argument>${docker.image.prefix}/${project.artifactId}</argument>
<argument>${docker.image.prefix}/${project.artifactId}:${docker.image.tag.commit}</argument>
<argument>${project.build.directory}</argument>
</arguments>
</configuration>
Expand Down