forked from spring-petclinic/spring-petclinic-microservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
83 lines (74 loc) · 3.02 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
pipeline {
agent any
environment {
MAVEN_OPTS = "-Dmaven.repo.local=.m2/repository"
}
options {
skipDefaultCheckout()
}
stages {
stage('Checkout') {
steps {
script {
def previousCommit = sh(script: "git rev-parse HEAD~1", returnStdout: true).trim()
def changedFiles = sh(script: "git diff --name-only ${previousCommit}", returnStdout: true).trim().split('\n')
def services = ['customers-service', 'vets-service', 'visits-service', 'api-gateway', 'config-server', 'discovery-server']
def affectedServices = services.findAll { service ->
changedFiles.any { it.startsWith("${service}/") }
}
if (affectedServices.isEmpty()) {
echo "No services affected, skipping pipeline."
currentBuild.result = 'SUCCESS'
return
}
env.AFFECTED_SERVICES = affectedServices.join(' ')
echo "Affected services: ${env.AFFECTED_SERVICES}"
}
}
}
stage('Test') {
when {
expression { env.AFFECTED_SERVICES != null && env.AFFECTED_SERVICES.trim() != '' }
}
steps {
script {
env.AFFECTED_SERVICES.split(' ').each { service ->
dir(service) {
echo "Running tests for ${service}"
sh 'mvn test'
// Debug: Kiểm tra thư mục test result
sh 'ls -l target/surefire-reports/ || echo "No test reports found"'
// Thu thập kết quả test
junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml'
// Thu thập độ phủ code
jacoco execPattern: '**/target/jacoco.exec'
}
}
}
}
}
stage('Build') {
when {
expression { env.AFFECTED_SERVICES != null && env.AFFECTED_SERVICES.trim() != '' }
}
steps {
script {
env.AFFECTED_SERVICES.split(' ').each { service ->
dir(service) {
echo "Building ${service}"
sh 'mvn package -DskipTests'
}
}
}
}
}
}
post {
always {
// Archive test result để Jenkins UI có thể hiển thị
archiveArtifacts artifacts: '**/target/surefire-reports/*.xml', allowEmptyArchive: true
// Đảm bảo JUnit luôn post test result lên Jenkins
junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml'
}
}
}