-
Notifications
You must be signed in to change notification settings - Fork 1
118 lines (103 loc) · 3.79 KB
/
Copy pathtest.yml
File metadata and controls
118 lines (103 loc) · 3.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
# PR이 올라왔을 때 자동으로 테스트코드를 실행하고 하나라도 Fail했을 시, PR을 Close하고 실패 코멘트를 달고 성공시에는 머지를 가능케하는 파이프라인.
name: PR Test
on:
pull_request:
branches:
- main # main 브랜치로 머지되는 PR에서 실행
jobs:
test:
runs-on: ubuntu-latest
outputs:
test_result: ${{ steps.run-tests.outcome }} # 테스트 결과를 output으로 저장
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: test_db
MYSQL_USER: test_user
MYSQL_PASSWORD: test_password
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping -h 127.0.0.1 -u root --password=root"
--health-interval=10s
--health-timeout=5s
--health-retries=3
steps:
# 1. 코드 체크아웃
- name: Checkout repository
uses: actions/checkout@v3
# 2. JDK 17 설정
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'temurin'
# 3. MySQL 인스톨까지 대기 및 연결상태 확인(디버깅용)
- name: Wait for MySQL
run: |
echo "Waiting for MySQL to be ready..."
for i in {1..30}; do
if mysql -h 127.0.0.1 -u root --password=root -e "SELECT 1" &> /dev/null; then
echo "MySQL is ready!"
exit 0
fi
echo "Waiting for MySQL..."
sleep 2
done
echo "MySQL did not become ready in time!" >&2
exit 1
- name: Check MySQL Connectivity
run: |
echo "Checking MySQL connection..."
mysql -h 127.0.0.1 -u test_user --password=test_password -e "SHOW DATABASES;"
# 4. Gradle 빌드 & JUnit 테스트 실행
- name: Run Tests with Gradle
id: run-tests # 실행 결과를 output으로 저장할 id 추가
continue-on-error: true
env:
SPRING_PROFILES_ACTIVE: test
run: |
cd ontime-back
./gradlew test
- name: Verify Flyway Migrations
run: |
echo "Checking Flyway migration history..."
mysql -h 127.0.0.1 -u test_user --password=test_password -e "SELECT * FROM test_db.flyway_schema_history;"
handle-failure:
needs: test
if: needs.test.outputs.test_result == 'failure' # test job의 output 값이 실패(failure)일 때 실행
runs-on: ubuntu-latest
permissions:
pull-requests: write
issues: write
steps:
- name: Close PR
uses: octokit/request-action@v2.x
with:
route: PATCH /repos/{owner}/{repo}/pulls/{pull_number}
owner: ${{ github.repository_owner }}
repo: ${{ github.event.repository.name }}
pull_number: ${{ github.event.pull_request.number }}
state: closed
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Wait for PR to close
run: sleep 5
- name: Comment on PR
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const prNumber = context.payload.pull_request?.number || context.payload.issue?.number;
if (!prNumber) {
console.log("PR 번호를 찾을 수 없습니다.");
return;
}
github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: "실패하는 테스트코드가 있어 PR이 자동으로 닫혔습니다.\nGithub Action에서 자세한 실패 로그를 확인하고 코드를 수정하세요."
});