[codex] Harden production database configuration #147
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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에서 자세한 실패 로그를 확인하고 코드를 수정하세요." | |
| }); |