-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJenkinsfile
More file actions
215 lines (195 loc) · 6.79 KB
/
Copy pathJenkinsfile
File metadata and controls
215 lines (195 loc) · 6.79 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
pipeline {
agent any
environment {
SONARQUBE_SERVER = 'SonarQube'
dockerHubRepo = 'marwaniwael/gestion-ski'
imageTag = "1.0-${env.BUILD_NUMBER}"
dockerHubCredentials = 'docker-hub'
EMAIL_RECIPIENTS = '[email protected]'
EMAIL_SENDER = '[email protected]'
}
stages {
stage('Checkout') {
steps {
git branch: 'subscription-wael', credentialsId: 'github', url: 'https://github.com/marwaniiwael18/DEVOPS-Project.git'
}
}
stage('Build') {
steps {
sh 'mvn clean compile jacoco:prepare-agent'
}
}
stage('Run Tests') {
steps {
sh 'mvn test jacoco:report'
}
post {
always {
junit '**/target/surefire-reports/*.xml'
}
}
}
stage('Publish JaCoCo Report') {
steps {
jacoco(
execPattern: 'target/jacoco.exec',
classPattern: 'target/classes',
sourcePattern: 'src/main/java',
exclusionPattern: '**/test/**'
)
}
}
stage('SonarQube Analysis') {
steps {
script {
def scannerHome = tool 'scanner'
withSonarQubeEnv("${SONARQUBE_SERVER}") {
sh """
${scannerHome}/bin/sonar-scanner \\
-Dsonar.projectKey=gestion-station-ski \\
-Dsonar.sources=src/main/java \\
-Dsonar.tests=src/test/java \\
-Dsonar.java.binaries=target/classes \\
-Dsonar.junit.reportsPath=target/surefire-reports \\
-Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
"""
}
}
}
}
stage('Package') {
steps {
sh 'mvn clean package -DskipTests'
}
}
stage('Build Docker Image') {
steps {
script {
writeFile file: 'Dockerfile', text: '''
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY target/*.jar /app/app.jar
EXPOSE 8081
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
'''
sh "docker build -t ${dockerHubRepo}:${imageTag} ."
}
}
}
stage('Push to Docker Hub') {
steps {
script {
docker.withRegistry('https://index.docker.io/v1/', dockerHubCredentials) {
sh "docker push ${dockerHubRepo}:${imageTag}"
}
}
}
}
stage('Run Application') {
steps {
script {
// Libère le port 8081 s’il est déjà utilisé
sh 'fuser -k 8081/tcp || true'
// Arrêt des conteneurs existants et suppression des orphelins
sh 'docker-compose down --remove-orphans || true'
writeFile file: 'docker-compose.yml', text: """
version: '3.8'
services:
spring_backend:
image: ${dockerHubRepo}:${imageTag}
container_name: spring_backend
environment:
SPRING_DATASOURCE_URL: jdbc:h2:mem:testdb
SPRING_DATASOURCE_DRIVER_CLASS_NAME: org.h2.Driver
SPRING_DATASOURCE_USERNAME: sa
SPRING_DATASOURCE_PASSWORD: TestDbStrongP@ss123
SPRING_JPA_DATABASE_PLATFORM: org.hibernate.dialect.H2Dialect
SPRING_H2_CONSOLE_ENABLED: "true"
SPRING_H2_CONSOLE_PATH: /h2-console
SERVER_PORT: 8081
SPRING_JPA_HIBERNATE_DDL_AUTO: update
SPRING_JPA_SHOW_SQL: "true"
LOGGING_LEVEL_ROOT: info
LOGGING_PATTERN_CONSOLE: "%d{yyyy-MM-dd HH:mm:ss} - %-5level - %logger{45} - %msg %n"
TZ: Africa/Tunis
ports:
- "8081:8081"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8081/actuator/health"]
interval: 30s
timeout: 10s
retries: 3
restart: always
"""
sh 'docker-compose up -d'
}
}
}
stage('Run Prometheus') {
steps {
script {
sh 'docker stop prometheus || true'
sh 'docker rm prometheus || true'
sh 'docker run -d --name prometheus -p 9090:9090 prom/prometheus'
}
}
}
stage('Run Grafana') {
steps {
script {
sh 'docker stop grafana || true'
sh 'docker rm grafana || true'
sh 'docker run -d --name grafana -p 3000:3000 grafana/grafana'
}
}
}
}
post {
success {
echo "✅ Build successful!"
emailext(
subject: "✅ [JENKINS] Build #${BUILD_NUMBER} - ${currentBuild.currentResult} - ${env.JOB_NAME}",
body: """
<html>
<body>
<h2>✅ Build Successful: ${env.JOB_NAME}</h2>
<p>Build #${BUILD_NUMBER} completed successfully!</p>
<ul>
<li>Tag: ${dockerHubRepo}:${imageTag}</li>
<li>Build URL: <a href="${BUILD_URL}">${BUILD_URL}</a></li>
<li>Duration: ${currentBuild.durationString}</li>
</ul>
</body>
</html>
""",
to: "${EMAIL_RECIPIENTS}",
from: "${EMAIL_SENDER}",
mimeType: 'text/html',
attachLog: true
)
}
failure {
echo "❌ Build failed!"
emailext(
subject: "❌ [JENKINS] Build #${BUILD_NUMBER} - ${currentBuild.currentResult} - ${env.JOB_NAME}",
body: """
<html>
<body>
<h2>❌ Build Failed: ${env.JOB_NAME}</h2>
<p>Build #${BUILD_NUMBER} has failed!</p>
<ul>
<li>Tag: ${dockerHubRepo}:${imageTag}</li>
<li>Build URL: <a href="${BUILD_URL}">${BUILD_URL}</a></li>
<li>Duration: ${currentBuild.durationString}</li>
</ul>
</body>
</html>
""",
to: "${EMAIL_RECIPIENTS}",
from: "${EMAIL_SENDER}",
mimeType: 'text/html',
attachLog: true
)
}
}
}