-
Notifications
You must be signed in to change notification settings - Fork 1
405 lines (338 loc) · 14.1 KB
/
Copy pathci-cd.yml
File metadata and controls
405 lines (338 loc) · 14.1 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
name: CI/CD Pipeline for Microservices (Ultra-Fast)
on:
pull_request:
branches:
- main
workflow_dispatch:
permissions:
contents: read
security-events: write
env:
DOCKER_REGISTRY: softbank2025
IMAGE_TAG: ${{ github.sha }}
jobs:
build-jars:
name: Build All JARs
runs-on: ubuntu-latest
outputs:
services: ${{ steps.set-matrix.outputs.services }}
has-changes: ${{ steps.set-matrix.outputs.has-changes }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: 'gradle'
- name: Detect changed files
id: changes
run: |
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "서비스 전체 배포 (수동 실행)"
echo "server=true" >> $GITHUB_OUTPUT
echo "gateway=true" >> $GITHUB_OUTPUT
echo "fe=true" >> $GITHUB_OUTPUT
echo "deploy=true" >> $GITHUB_OUTPUT
echo "user=true" >> $GITHUB_OUTPUT
elif [ "${{ github.event_name }}" == "pull_request" ]; then
CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }})
echo "Changed files:"
echo "$CHANGED_FILES"
if echo "$CHANGED_FILES" | grep -qE "^server/|^build.gradle$|^settings.gradle$"; then
echo "server=true" >> $GITHUB_OUTPUT
else
echo "server=false" >> $GITHUB_OUTPUT
fi
if echo "$CHANGED_FILES" | grep -qE "^gateway/|^build.gradle$|^settings.gradle$"; then
echo "gateway=true" >> $GITHUB_OUTPUT
else
echo "gateway=false" >> $GITHUB_OUTPUT
fi
if echo "$CHANGED_FILES" | grep -qE "^fe/|^build.gradle$|^settings.gradle$"; then
echo "fe=true" >> $GITHUB_OUTPUT
else
echo "fe=false" >> $GITHUB_OUTPUT
fi
if echo "$CHANGED_FILES" | grep -qE "^deploy/|^build.gradle$|^settings.gradle$"; then
echo "deploy=true" >> $GITHUB_OUTPUT
else
echo "deploy=false" >> $GITHUB_OUTPUT
fi
if echo "$CHANGED_FILES" | grep -qE "^user/|^build.gradle$|^settings.gradle$"; then
echo "user=true" >> $GITHUB_OUTPUT
else
echo "user=false" >> $GITHUB_OUTPUT
fi
else
echo "메인 브랜치 푸시 - 전체 배포"
echo "server=true" >> $GITHUB_OUTPUT
echo "gateway=true" >> $GITHUB_OUTPUT
echo "fe=true" >> $GITHUB_OUTPUT
echo "deploy=true" >> $GITHUB_OUTPUT
echo "user=true" >> $GITHUB_OUTPUT
fi
- name: Set matrix services
id: set-matrix
run: |
CHANGED_SERVICES=""
if [ "${{ steps.changes.outputs.server }}" == "true" ]; then
CHANGED_SERVICES="${CHANGED_SERVICES}\"server\","
fi
if [ "${{ steps.changes.outputs.gateway }}" == "true" ]; then
CHANGED_SERVICES="${CHANGED_SERVICES}\"gateway\","
fi
if [ "${{ steps.changes.outputs.fe }}" == "true" ]; then
CHANGED_SERVICES="${CHANGED_SERVICES}\"fe\","
fi
if [ "${{ steps.changes.outputs.deploy }}" == "true" ]; then
CHANGED_SERVICES="${CHANGED_SERVICES}\"deploy\","
fi
if [ "${{ steps.changes.outputs.user }}" == "true" ]; then
CHANGED_SERVICES="${CHANGED_SERVICES}\"user\","
fi
CHANGED_SERVICES=$(echo $CHANGED_SERVICES | sed 's/,$//')
if [ -z "$CHANGED_SERVICES" ]; then
echo "services=[]" >> $GITHUB_OUTPUT
echo "has-changes=false" >> $GITHUB_OUTPUT
echo "⚠️ 변경된 서비스 없음"
else
echo "services=[$CHANGED_SERVICES]" >> $GITHUB_OUTPUT
echo "has-changes=true" >> $GITHUB_OUTPUT
echo "✅ 변경된 서비스: [$CHANGED_SERVICES]"
fi
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: Build all services (parallel)
run: |
echo "🔨 Building all services in parallel..."
./gradlew build -x test --parallel --max-workers=5 --build-cache
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: build-artifacts
path: |
server/build/libs/*.jar
gateway/build/libs/*.jar
fe/build/libs/*.jar
deploy/build/libs/*.jar
user/build/libs/*.jar
retention-days: 1
docker-build-scan-push:
name: Docker Build & Push
needs: build-jars
runs-on: ubuntu-latest
if: needs.build-jars.outputs.has-changes == 'true'
strategy:
matrix:
service: ${{ fromJson(needs.build-jars.outputs.services) }}
fail-fast: false
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: build-artifacts
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Create optimized Dockerfile
run: |
cat > ${{ matrix.service }}/Dockerfile.fast << 'EOF'
FROM eclipse-temurin:17-jre-focal
WORKDIR /app
COPY ${{ matrix.service }}/build/libs/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
EOF
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./${{ matrix.service }}/Dockerfile.fast
push: true
tags: |
${{ env.DOCKER_REGISTRY }}/${{ matrix.service }}:${{ env.IMAGE_TAG }}
${{ env.DOCKER_REGISTRY }}/${{ matrix.service }}:latest
cache-from: |
type=registry,ref=${{ env.DOCKER_REGISTRY }}/${{ matrix.service }}:buildcache
type=gha
cache-to: |
type=registry,ref=${{ env.DOCKER_REGISTRY }}/${{ matrix.service }}:buildcache,mode=max
type=gha,mode=max
- name: Run Trivy scan
uses: aquasecurity/trivy-action@master
continue-on-error: true
with:
image-ref: ${{ env.DOCKER_REGISTRY }}/${{ matrix.service }}:${{ env.IMAGE_TAG }}
format: 'sarif'
output: 'trivy-results-${{ matrix.service }}.sarif'
severity: 'CRITICAL,HIGH'
timeout: '5m0s'
- name: Upload Trivy results
uses: github/codeql-action/upload-sarif@v3
if: always()
continue-on-error: true
with:
sarif_file: 'trivy-results-${{ matrix.service }}.sarif'
category: 'trivy-${{ matrix.service }}'
deploy-to-ec2:
name: Deploy to EC2
needs: [build-jars, docker-build-scan-push]
runs-on: ubuntu-latest
if: (github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/heads/feat/')) && needs.build-jars.outputs.has-changes == 'true'
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Prepare changed services list
id: services
run: |
SERVICES=$(echo '${{ needs.build-jars.outputs.services }}' | jq -r '.[]' | tr '\n' ' ')
echo "list=$SERVICES" >> $GITHUB_OUTPUT
echo "📦 Services to deploy: $SERVICES"
- name: Deploy to EC2 via AWS SSM
id: deploy
run: |
SERVICES="${{ steps.services.outputs.list }}"
cat > deploy.sh << 'DEPLOY_EOF'
#!/bin/bash
set -e
SERVICES="$CHANGED_SERVICES"
IMAGE_TAG="$GITHUB_SHA"
echo "📂 Navigating to project directory..."
cd /home/ubuntu/Raspberry
echo "🔧 Changing file ownership..."
sudo chown -R $USER:$USER .
echo "🔄 Pulling latest code..."
git fetch origin
git reset --hard $GITHUB_SHA
# 스왑 체크 (간소화)
if ! sudo swapon --show | grep -q "/swapfile" && [ ! -f /swapfile ]; then
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
fi
echo "🐳 Docker login..."
echo "$DOCKERHUB_PASSWORD" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
sudo chmod 666 /var/run/docker.sock
echo "🔧 Updating docker-compose.yml..."
cp docker-compose.yml docker-compose.yml.backup
for service in $SERVICES; do
sed -i "s|image: softbank2025/${service}:.*|image: softbank2025/${service}:${IMAGE_TAG}|g" docker-compose.yml
done
sed -i '/build:/,+2d' docker-compose.yml
echo "📥 Pulling images (parallel)..."
for service in $SERVICES; do
docker pull softbank2025/${service}:${IMAGE_TAG} &
done
wait
echo "🛑 Stopping and removing old services..."
docker-compose stop $SERVICES || true
docker-compose rm -f $SERVICES || true
echo "🚀 Creating new services..."
docker-compose up -d --no-deps $SERVICES
echo "📊 Ensuring monitoring services are running..."
docker-compose up -d prometheus grafana
echo "⏳ Waiting 15s..."
sleep 15
docker image prune -a -f || true
echo "✅ Deployment completed!"
docker-compose ps
DEPLOY_EOF
ENCODED_SCRIPT=$(cat deploy.sh | base64 -w 0)
COMMAND_ID=$(aws ssm send-command \
--instance-ids "${{ secrets.EC2_INSTANCE_ID }}" \
--document-name "AWS-RunShellScript" \
--parameters "commands=[\"echo $ENCODED_SCRIPT | base64 -d > /tmp/deploy.sh && chmod +x /tmp/deploy.sh && DOCKERHUB_PASSWORD='${{ secrets.DOCKERHUB_PASSWORD }}' DOCKERHUB_USERNAME='${{ secrets.DOCKERHUB_USERNAME }}' GITHUB_SHA='${{ github.sha }}' CHANGED_SERVICES='$SERVICES' bash /tmp/deploy.sh\"]" \
--timeout-seconds 300 \
--output text \
--query 'Command.CommandId')
echo "command_id=$COMMAND_ID" >> $GITHUB_OUTPUT
echo "✅ SSM Command ID: $COMMAND_ID"
- name: Wait for deployment
run: |
COMMAND_ID="${{ steps.deploy.outputs.command_id }}"
for i in {1..30}; do
STATUS=$(aws ssm get-command-invocation \
--command-id "$COMMAND_ID" \
--instance-id "${{ secrets.EC2_INSTANCE_ID }}" \
--query 'Status' \
--output text 2>/dev/null || echo "Pending")
echo "[$i/30] Status: $STATUS"
if [ "$STATUS" = "Success" ]; then
echo "✅ Deployment completed!"
aws ssm get-command-invocation \
--command-id "$COMMAND_ID" \
--instance-id "${{ secrets.EC2_INSTANCE_ID }}" \
--query 'StandardOutputContent' \
--output text
break
elif [ "$STATUS" = "Failed" ] || [ "$STATUS" = "TimedOut" ]; then
echo "❌ Deployment failed: $STATUS"
aws ssm get-command-invocation \
--command-id "$COMMAND_ID" \
--instance-id "${{ secrets.EC2_INSTANCE_ID }}" \
--query 'StandardErrorContent' \
--output text
exit 1
fi
sleep 5
done
- name: Quick health check
run: |
cat > verify.sh << 'VERIFY_EOF'
#!/bin/bash
cd /home/ubuntu/Raspberry
echo "🔍 Health check..."
if curl -sf http://localhost:8761 > /dev/null 2>&1; then
echo "✅ Eureka OK"
else
echo "❌ Eureka DOWN"
exit 1
fi
if curl -sf http://localhost:8080/actuator/health > /dev/null 2>&1; then
echo "✅ Gateway OK"
fi
docker-compose ps --format "table {{.Service}}\t{{.Status}}"
VERIFY_EOF
ENCODED_SCRIPT=$(cat verify.sh | base64 -w 0)
VERIFY_CMD=$(aws ssm send-command \
--instance-ids "${{ secrets.EC2_INSTANCE_ID }}" \
--document-name "AWS-RunShellScript" \
--parameters "commands=[\"echo $ENCODED_SCRIPT | base64 -d | bash\"]" \
--timeout-seconds 120 \
--output text \
--query 'Command.CommandId')
sleep 8
aws ssm get-command-invocation \
--command-id "$VERIFY_CMD" \
--instance-id "${{ secrets.EC2_INSTANCE_ID }}" \
--query 'StandardOutputContent' \
--output text
- name: Summary
if: always()
run: |
if [ "${{ job.status }}" == "success" ]; then
echo "✅ Deployment successful!"
echo "🔖 Image tag: ${{ github.sha }}"
echo "📦 Services: ${{ steps.services.outputs.list }}"
else
echo "❌ Deployment failed!"
exit 1
fi