-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
269 lines (242 loc) · 14.4 KB
/
Copy pathJenkinsfile
File metadata and controls
269 lines (242 loc) · 14.4 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// ═══════════════════════════════════════════════════════════════════════════════
// FreshMart — Jenkins CI/CD Pipeline
// ═══════════════════════════════════════════════════════════════════════════════
//
// PURPOSE:
// Automates the full build, test, and deploy cycle for the FreshMart platform.
// Triggered manually (Build Now) or automatically via GitHub webhook on push.
//
// PIPELINE STAGES:
// 1. Checkout — clean clone from GitHub (prevents git cache corruption)
// 2. Inject Secrets — copies .env from Jenkins credentials, generates dev configs
// 3. Build .NET — dotnet restore + build all 15 projects
// 4. Test .NET — runs 30 NUnit unit tests, publishes JUnit results
// 5. Build Frontend — npm ci + ng build --configuration production
// 6. Fix Docker Socket — chmod 666 /var/run/docker.sock (Jenkins runs as root)
// 7. Docker Build — builds all 14 service images in parallel
// 8. Deploy — docker compose up -d (starts all containers)
//
// SECRETS MANAGEMENT:
// - Only ONE credential needed: 'grocery-env-file' (Secret file in Jenkins)
// - All other secrets (JWT, RabbitMQ, email, Razorpay, Gemini) are extracted
// from the .env file at build time — no individual credentials needed
// - Secrets are deleted from workspace in post { always { ... } }
//
// DOCKER BUILD:
// - All 14 images built in parallel to save time
// - Frontend image receives RAZORPAY_KEY and GOOGLE_CLIENT_ID as --build-arg
// so they are baked into the Angular bundle at build time
// - Images tagged as grocery/<service>:<build-number> and grocery/<service>:latest
//
// REQUIREMENTS:
// Jenkins image must have: dotnet 10, node 20, docker CLI, python3
// See infrastructure/jenkins/Dockerfile
// ═══════════════════════════════════════════════════════════════════════════════
pipeline {
agent any
environment {
COMPOSE_FILE = 'infrastructure/docker-compose.yml'
}
options {
buildDiscarder(logRotator(numToKeepStr: '10'))
timeout(time: 45, unit: 'MINUTES')
disableConcurrentBuilds()
skipDefaultCheckout(false)
}
stages {
// ── 1. Checkout ───────────────────────────────────────────────────────
stage('Checkout') {
steps {
// Clean workspace before checkout to prevent git object corruption
cleanWs()
checkout([
$class: 'GitSCM',
branches: [[name: '*/main']],
extensions: [[$class: 'CleanBeforeCheckout']],
userRemoteConfigs: [[
url: 'https://github.com/maheshsingh20/FreshMart.git',
credentialsId: 'github-token'
]]
])
echo "Branch: ${env.GIT_BRANCH} | Commit: ${env.GIT_COMMIT?.take(8)}"
}
}
// ── 2. Inject secrets (create files Jenkins needs but git doesn't have)
stage('Inject Secrets') {
steps {
// Inject the .env file from Jenkins credentials store
// This replaces the gitignored infrastructure/.env at build time
withCredentials([file(credentialsId: 'grocery-env-file', variable: 'ENV_FILE')]) {
sh 'cp $ENV_FILE infrastructure/.env'
sh 'cp $ENV_FILE .env'
// Generate NotificationService dev config from the env file values
sh '''
JWT_SECRET=$(grep "^JWT_SECRET=" infrastructure/.env | cut -d= -f2)
RABBITMQ_USER=$(grep "^RABBITMQ_USER=" infrastructure/.env | cut -d= -f2)
RABBITMQ_PASS=$(grep "^RABBITMQ_PASS=" infrastructure/.env | cut -d= -f2)
EMAIL_USERNAME=$(grep "^EMAIL_USERNAME=" infrastructure/.env | cut -d= -f2)
EMAIL_PASSWORD=$(grep "^EMAIL_PASSWORD=" infrastructure/.env | cut -d= -f2-)
cat > services/NotificationService/appsettings.Development.json << JSONEOF
{
"ConnectionStrings": {
"NotificationDb": "Server=.\\\\SQLEXPRESS;Database=GroceryNotifications;Trusted_Connection=True;TrustServerCertificate=True"
},
"Jwt": { "Secret": "$JWT_SECRET", "Issuer": "GroceryPlatform", "Audience": "GroceryPlatformClients" },
"RabbitMQ": { "Host": "localhost", "Port": "5672", "Username": "$RABBITMQ_USER", "Password": "$RABBITMQ_PASS", "VirtualHost": "/" },
"Email": { "Enabled": "true", "Host": "smtp.gmail.com", "Port": "587", "Username": "$EMAIL_USERNAME", "Password": "$EMAIL_PASSWORD", "From": "FreshMart <$EMAIL_USERNAME>" },
"Urls": "http://localhost:5007"
}
JSONEOF
'''
}
echo "Secrets injected successfully"
}
}
// ── 3. Build .NET ─────────────────────────────────────────────────────
stage('Build .NET') {
steps {
sh 'dotnet restore CapgeminiSprint.sln'
sh 'dotnet build CapgeminiSprint.sln --no-restore -c Release'
}
}
// ── 4. Run Unit Tests ─────────────────────────────────────────────────
stage('Test .NET') {
steps {
sh '''
dotnet test tests/AuthService.Tests/AuthService.Tests.csproj \
--no-build -c Release \
--logger "trx;LogFileName=auth-results.trx" \
--results-directory ./TestResults || true
dotnet test tests/ProductService.Tests/ProductService.Tests.csproj \
--no-build -c Release \
--logger "trx;LogFileName=product-results.trx" \
--results-directory ./TestResults || true
dotnet test tests/OrderService.Tests/OrderService.Tests.csproj \
--no-build -c Release \
--logger "trx;LogFileName=order-results.trx" \
--results-directory ./TestResults || true
dotnet test tests/CartService.Tests/CartService.Tests.csproj \
--no-build -c Release \
--logger "trx;LogFileName=cart-results.trx" \
--results-directory ./TestResults || true
dotnet test tests/PaymentService.Tests/PaymentService.Tests.csproj \
--no-build -c Release \
--logger "trx;LogFileName=payment-results.trx" \
--results-directory ./TestResults || true
dotnet test tests/DeliveryService.Tests/DeliveryService.Tests.csproj \
--no-build -c Release \
--logger "trx;LogFileName=delivery-results.trx" \
--results-directory ./TestResults || true
dotnet test tests/CouponService.Tests/CouponService.Tests.csproj \
--no-build -c Release \
--logger "trx;LogFileName=coupon-results.trx" \
--results-directory ./TestResults || true
'''
}
post {
always {
junit allowEmptyResults: true, testResults: 'TestResults/**/*.trx'
}
}
}
// ── 5. Build Angular Frontend ─────────────────────────────────────────
stage('Build Frontend') {
steps {
// Frontend is built inside Docker via --build-arg in dockerBuildFrontend()
// No need to modify source files here — Dockerfile handles placeholder replacement
echo "Frontend will be built in Docker Build stage with real keys injected via --build-arg"
}
}
// ── 6. Docker Build (parallel) ────────────────────────────────────────
stage('Fix Docker Socket') {
steps {
sh 'chmod 666 /var/run/docker.sock || true'
// Login to Docker Hub to avoid anonymous pull rate limits (100/6h → 200/6h)
withCredentials([usernamePassword(credentialsId: 'dockerhub-credentials', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh 'echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin || true'
}
}
}
stage('Docker Build') {
parallel {
stage('auth-service') { steps { dockerBuild('auth-service', 'services/AuthService/Dockerfile') } }
stage('product-service') { steps { dockerBuild('product-service', 'services/ProductService/Dockerfile') } }
stage('order-service') { steps { dockerBuild('order-service', 'services/OrderService/Dockerfile') } }
stage('payment-service') { steps { dockerBuild('payment-service', 'services/PaymentService/Dockerfile') } }
stage('notification-service') { steps { dockerBuild('notification-service', 'services/NotificationService/Dockerfile') } }
stage('cart-service') { steps { dockerBuild('cart-service', 'services/CartService/Dockerfile') } }
stage('delivery-service') { steps { dockerBuild('delivery-service', 'services/DeliveryService/Dockerfile') } }
stage('review-service') { steps { dockerBuild('review-service', 'services/ReviewService/Dockerfile') } }
stage('coupon-service') { steps { dockerBuild('coupon-service', 'services/CouponService/Dockerfile') } }
stage('ai-service') { steps { dockerBuild('ai-service', 'services/AiService/Dockerfile') } }
stage('user-service') { steps { dockerBuild('user-service', 'services/UserService/Dockerfile') } }
stage('support-service') { steps { dockerBuild('support-service', 'services/SupportService/Dockerfile') } }
stage('api-gateway') { steps { dockerBuild('api-gateway', 'services/ApiGateway/Dockerfile') } }
stage('frontend') { steps { dockerBuildFrontend() } }
}
}
// ── 7. Deploy ─────────────────────────────────────────────────────────
stage('Deploy') {
steps {
withCredentials([file(credentialsId: 'grocery-env-file', variable: 'ENV_FILE')]) {
sh '''
cp $ENV_FILE infrastructure/.env
# Fix network label mismatch — recreate with proper Compose labels
docker network disconnect infrastructure_grocery-net jenkins || true
docker network rm infrastructure_grocery-net || true
docker network create \
--label com.docker.compose.network=grocery-net \
--label com.docker.compose.project=infrastructure \
infrastructure_grocery-net || true
docker network connect infrastructure_grocery-net jenkins || true
# Pull infrastructure base images (redis, rabbitmq, sqlserver)
# These are NOT built by Jenkins — they come from Docker Hub
# --pull never would fail if they don't exist locally after a clean wipe
docker pull redis:7-alpine || true
docker pull rabbitmq:3.13-management-alpine || true
docker pull mcr.microsoft.com/mssql/server:2022-latest || true
docker compose -f infrastructure/docker-compose.yml up -d --no-build --pull never
'''
}
}
}
}
post {
always {
sh '''
rm -f infrastructure/.env .env
rm -f services/NotificationService/appsettings.Development.json
'''
cleanWs()
}
success {
echo "Pipeline succeeded — ${env.GIT_BRANCH} @ ${env.GIT_COMMIT?.take(8)}"
}
failure {
echo "Pipeline FAILED — ${env.GIT_BRANCH} @ ${env.GIT_COMMIT?.take(8)}"
}
}
}
// ── Helpers ───────────────────────────────────────────────────────────────────
def dockerBuild(String service, String dockerfile) {
sh """
docker build \\
-f ${dockerfile} \\
-t grocery/${service}:${env.BUILD_NUMBER} \\
-t grocery/${service}:latest \\
.
"""
}
def dockerBuildFrontend() {
sh """
RAZORPAY_KEY=\$(grep "^RAZORPAY_KEY_ID=" infrastructure/.env | cut -d= -f2 | tr -d '\\r\\n ')
GOOGLE_CLIENT_ID=\$(grep "^GOOGLE_CLIENT_ID=" infrastructure/.env | cut -d= -f2 | tr -d '\\r\\n ')
docker build \\
-f Frontend/Dockerfile \\
--build-arg RAZORPAY_KEY=\${RAZORPAY_KEY} \\
--build-arg GOOGLE_CLIENT_ID=\${GOOGLE_CLIENT_ID} \\
-t grocery/frontend:${env.BUILD_NUMBER} \\
-t grocery/frontend:latest \\
Frontend/
"""
}