-
Notifications
You must be signed in to change notification settings - Fork 0
315 lines (255 loc) · 10.1 KB
/
integrate-prod.yml
File metadata and controls
315 lines (255 loc) · 10.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
# ============================================================================
# Backend Integration Workflow - Production
# ============================================================================
# 역할:
# - 소스 코드 정적 분석 (Spotless, Checkstyle)
# - 테스트 수행 및 JaCoCo 커버리지 생성
# - SonarQube 코드 품질/커버리지/보안 분석
# - JAR artifact 생성 (deploy-prod workflow에서 사용)
#
# Job 구조:
# lint ──┬──> analyze ──> build
# test ──┘
#
# 대상 브랜치: main (production 환경)
#
# GitHub Secrets:
# - SONAR_TOKEN : SonarQube Token
# - SONAR_HOST_URL : SonarQube Server URL (e.g., https://sonar.example.com)
# ============================================================================
name: Integrate Production
on:
pull_request:
branches:
- main
push:
branches:
- main
env:
JAVA_VERSION: '17'
jobs:
# ==========================================================================
# Lint Job - 코드 스타일 검사
# ==========================================================================
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: temurin
cache: gradle
- name: Grant execution to Gradle wrapper
run: chmod +x gradlew
- name: Run Spotless Check
run: ./gradlew spotlessCheck
- name: Run Checkstyle
run: ./gradlew checkstyleMain checkstyleTest
# ==========================================================================
# Test Job - 테스트 및 커버리지
# ==========================================================================
test:
name: Test
runs-on: ubuntu-latest
permissions:
contents: read
checks: write
pull-requests: write
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: temurin
cache: gradle
- name: Grant execution to Gradle wrapper
run: chmod +x gradlew
- name: Run Tests with JaCoCo
run: ./gradlew test jacocoTestReport
- name: Upload test results
uses: actions/upload-artifact@v4
if: always()
with:
name: test-results
path: |
build/test-results/
build/reports/jacoco/
if-no-files-found: ignore
# ==========================================================================
# Analyze Job - SonarQube 분석
# ==========================================================================
analyze:
name: Analyze
runs-on: ubuntu-latest
needs: [lint, test]
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout source
uses: actions/checkout@v4
with:
fetch-depth: 0 # SonarQube 분석을 위해 전체 히스토리 필요
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: temurin
cache: gradle
- name: Cache SonarQube packages
uses: actions/cache@v4
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Download test results
uses: actions/download-artifact@v4
continue-on-error: true
with:
name: test-results
path: build/
- name: Grant execution to Gradle wrapper
run: chmod +x gradlew
- name: Compile classes for SonarQube analysis
run: ./gradlew compileJava
- name: SonarQube Scan
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
run: ./gradlew sonar --info
- name: SonarQube Quality Gate + PR Comment (Community)
if: github.event_name == 'pull_request'
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
BRANCH: ${{ github.head_ref }}
run: |
set -e
REPORT_FILE="build/sonar/report-task.txt"
if [ ! -f "$REPORT_FILE" ]; then
echo "report-task.txt not found"
exit 1
fi
# ------------------------------------------------------------------
# Basic info from report-task.txt
# ------------------------------------------------------------------
CE_TASK_ID=$(grep '^ceTaskId=' $REPORT_FILE | cut -d'=' -f2)
PROJECT_KEY=$(grep '^projectKey=' $REPORT_FILE | cut -d'=' -f2)
DASHBOARD_URL=$(grep '^dashboardUrl=' $REPORT_FILE | cut -d'=' -f2-)
echo "ProjectKey: $PROJECT_KEY"
echo "Branch: $BRANCH"
# ------------------------------------------------------------------
# Wait for CE task
# ------------------------------------------------------------------
for i in {1..30}; do
STATUS_JSON=$(curl -s -u "$SONAR_TOKEN:" \
"$SONAR_HOST_URL/api/ce/task?id=$CE_TASK_ID")
TASK_STATUS=$(echo "$STATUS_JSON" | jq -r '.task.status')
if [ "$TASK_STATUS" = "SUCCESS" ]; then
break
fi
echo "Waiting for SonarQube task... ($i)"
sleep 5
done
ANALYSIS_ID=$(echo "$STATUS_JSON" | jq -r '.task.analysisId')
# ------------------------------------------------------------------
# Quality Gate
# ------------------------------------------------------------------
QG_JSON=$(curl -s -u "$SONAR_TOKEN:" \
"$SONAR_HOST_URL/api/qualitygates/project_status?analysisId=$ANALYSIS_ID")
QG_STATUS=$(echo "$QG_JSON" | jq -r '.projectStatus.status')
# ------------------------------------------------------------------
# Branch-level Issues (SAFE)
# ------------------------------------------------------------------
ISSUES_JSON=$(curl -s -u "$SONAR_TOKEN:" \
"$SONAR_HOST_URL/api/issues/search?projectKeys=$PROJECT_KEY&branch=$BRANCH&resolved=false")
BUGS=$(echo "$ISSUES_JSON" | jq '[ (.issues // [])[] | select(.type=="BUG") ] | length')
VULNS=$(echo "$ISSUES_JSON" | jq '[ (.issues // [])[] | select(.type=="VULNERABILITY") ] | length')
SMELLS=$(echo "$ISSUES_JSON" | jq '[ (.issues // [])[] | select(.type=="CODE_SMELL") ] | length')
# ------------------------------------------------------------------
# Branch-level Measures (SAFE)
# ------------------------------------------------------------------
MEASURES_JSON=$(curl -s -u "$SONAR_TOKEN:" \
"$SONAR_HOST_URL/api/measures/component?component=$PROJECT_KEY&branch=$BRANCH&metricKeys=coverage,duplicated_lines_density")
COVERAGE=$(echo "$MEASURES_JSON" | jq -r '.component.measures // [] | map(select(.metric=="coverage")) | .[0].value // "0"')
DUPLICATION=$(echo "$MEASURES_JSON" | jq -r '.component.measures // [] | map(select(.metric=="duplicated_lines_density")) | .[0].value // "0"')
# ------------------------------------------------------------------
# Result formatting
# ------------------------------------------------------------------
if [ "$QG_STATUS" = "OK" ]; then
ICON="✅"
RESULT="PASSED"
else
ICON="❌"
RESULT="FAILED"
fi
BODY=$(cat <<EOF
## SonarQube Quality Summary (Community)
${ICON} **Quality Gate ${RESULT}**
**Branch:** \`${BRANCH}\`
**Compared to:** default branch
### Issues
- 🐞 Bugs: ${BUGS}
- 🔐 Vulnerabilities: ${VULNS}
- 📎 Code Smells: ${SMELLS}
### Measures
- Coverage: ${COVERAGE}%
- Duplication: ${DUPLICATION}%
🔗 Dashboard: ${DASHBOARD_URL}&branch=${BRANCH}
_Generated automatically by GitHub Actions._
EOF
)
# ------------------------------------------------------------------
# PR comment
# ------------------------------------------------------------------
gh api repos/$REPO/issues/$PR_NUMBER/comments \
-f body="$BODY"
# ------------------------------------------------------------------
# Fail workflow if Quality Gate failed
# ------------------------------------------------------------------
if [ "$QG_STATUS" != "OK" ]; then
echo "Quality Gate FAILED"
exit 1
fi
# ==========================================================================
# Build Job - JAR 빌드 및 Artifact 업로드
# ==========================================================================
build:
name: Build
runs-on: ubuntu-latest
needs: [analyze]
permissions:
actions: write
contents: write
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: ${{ env.JAVA_VERSION }}
distribution: temurin
cache: gradle
- name: Grant execution to Gradle wrapper
run: chmod +x gradlew
- name: Build Spring Boot JAR
run: ./gradlew bootJar
- name: Upload Build Artifact
uses: actions/upload-artifact@v4
with:
name: spring-boot-app
path: build/libs/*.jar
- name: Integration Summary
run: |
echo "## Integration Summary" >> $GITHUB_STEP_SUMMARY
echo "- **Branch** : ${{ github.ref }}" >> $GITHUB_STEP_SUMMARY
echo "- **Commit** : ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY