chore : spotless 수정 #15
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
| # 워크플로우의 이름 | |
| name: Spring Boot CI/CD with AWS | |
| # 워크플로우가 실행될 조건 정의 | |
| on: | |
| push: | |
| branches: [ develop ] # develop 브랜치에 push 이벤트가 발생했을 때 실행 | |
| pull_request: | |
| branches: [ develop ] # develop 브랜치로의 PR 생성/업데이트 시 실행 | |
| # OIDC 토큰 요청을 위한 권한 설정 (필수) | |
| permissions: | |
| id-token: write | |
| contents: read | |
| jobs: | |
| # 코드 포맷팅 및 기본 검증 (항상 첫 번째로 실행) | |
| code-quality: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| quality-check: ${{ steps.quality-status.outputs.success }} | |
| steps: | |
| # 1. 소스 코드 체크아웃 | |
| - name: Checkout Source Code | |
| uses: actions/checkout@v4 | |
| # 2. JDK 17 설치 (Corretto 배포판 사용) | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'corretto' | |
| # 3. Gradle 캐시 설정 | |
| - name: Cache Gradle packages | |
| uses: actions/cache@v3 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
| restore-keys: | | |
| ${{ runner.os }}-gradle- | |
| # 4. Gradle 실행 권한 부여 | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x ./gradlew | |
| # 5. 코드 포맷팅 검사 (PR일 때는 검사만, push일 때는 자동 적용) | |
| - name: Check code formatting (PR) | |
| if: github.event_name == 'pull_request' | |
| run: ./gradlew spotlessCheck | |
| - name: Apply code formatting (Push) | |
| if: github.event_name == 'push' | |
| run: ./gradlew spotlessApply | |
| # 6. 포맷팅 변경사항 확인 및 커밋 (Push일 때만) | |
| - name: Check for formatting changes | |
| if: github.event_name == 'push' | |
| id: verify-changed-files | |
| run: | | |
| if [ -n "$(git status --porcelain)" ]; then | |
| echo "changed=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "changed=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Commit formatting changes | |
| if: github.event_name == 'push' && steps.verify-changed-files.outputs.changed == 'true' | |
| run: | | |
| git config --local user.email "action@github.com" | |
| git config --local user.name "GitHub Action" | |
| git add . | |
| git commit -m "🎨 Auto-format code with Spotless [skip ci]" | |
| git push | |
| # 7. 코드 품질 검사 완료 상태 설정 | |
| - name: Set quality check status | |
| id: quality-status | |
| run: echo "success=true" >> $GITHUB_OUTPUT | |
| # 빌드 및 테스트 작업 (코드 품질 검사 통과 후 실행) | |
| build: | |
| runs-on: ubuntu-latest | |
| needs: code-quality | |
| outputs: | |
| build-success: ${{ steps.build-status.outputs.success }} | |
| steps: | |
| # 1. 소스 코드 체크아웃 | |
| - name: Checkout Source Code | |
| uses: actions/checkout@v4 | |
| # 2. JDK 17 설치 (Corretto 배포판 사용) | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'corretto' | |
| # 3. Gradle 캐시 재사용 | |
| - name: Cache Gradle packages | |
| uses: actions/cache@v3 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
| restore-keys: | | |
| ${{ runner.os }}-gradle- | |
| # 4. Gradle 실행 권한 부여 | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x ./gradlew | |
| # 5. Gradle로 빌드 및 단위 테스트 실행 | |
| - name: Build with Gradle | |
| run: ./gradlew build | |
| env: | |
| # --- DB 관련 시크릿 추가 --- | |
| DB_URL: ${{ secrets.DB_URL }} | |
| DB_USERNAME: ${{ secrets.DB_USERNAME }} | |
| DB_PASSWORD: ${{ secrets.DB_PASSWORD }} | |
| # --- 기존 R2 관련 시크릿 --- | |
| R2_ENDPOINT: ${{ secrets.R2_ENDPOINT }} | |
| R2_BUCKET: ${{ secrets.R2_BUCKET }} | |
| R2_ACCESS_KEY: ${{ secrets.R2_ACCESS_KEY }} | |
| R2_SECRET_KEY: ${{ secrets.R2_SECRET_KEY }} | |
| # 6. 테스트 결과 업로드 | |
| - name: Publish Test Results | |
| uses: dorny/test-reporter@v1 | |
| if: success() || failure() | |
| with: | |
| name: Gradle Tests | |
| path: build/test-results/test/*.xml | |
| reporter: java-junit | |
| # 7. 빌드 성공 상태 설정 | |
| - name: Set build success status | |
| id: build-status | |
| run: echo "success=true" >> $GITHUB_OUTPUT | |
| # EC2 상태 확인 작업 (Push일 때만 실행) | |
| check-ec2: | |
| runs-on: ubuntu-latest | |
| needs: build | |
| if: github.event_name == 'push' | |
| outputs: | |
| ec2-available: ${{ steps.check-ec2-status.outputs.available }} | |
| steps: | |
| # EC2 인스턴스 상태 확인 | |
| - name: Check EC2 instance status | |
| id: check-ec2-status | |
| run: | | |
| # EC2에 ping 테스트 (timeout 10초) | |
| if timeout 10 nc -z ${{ secrets.EC2_HOST }} 22 2>/dev/null; then | |
| echo "EC2 instance is reachable" | |
| echo "available=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "EC2 instance is not reachable or stopped" | |
| echo "available=false" >> $GITHUB_OUTPUT | |
| fi | |
| continue-on-error: true | |
| # Docker 이미지 빌드 및 ECR 푸시 (Push + EC2가 사용 가능할 때만) | |
| build-and-push-image: | |
| runs-on: ubuntu-latest | |
| needs: [build, check-ec2] | |
| if: github.event_name == 'push' && needs.check-ec2.outputs.ec2-available == 'true' | |
| outputs: | |
| image-pushed: ${{ steps.push-status.outputs.success }} | |
| steps: | |
| # 1. 소스 코드 체크아웃 | |
| - name: Checkout Source Code | |
| uses: actions/checkout@v4 | |
| # 2. AWS 자격 증명 설정 (OIDC 방식) | |
| - name: Configure AWS credentials using OIDC | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} | |
| aws-region: ${{ secrets.AWS_REGION }} | |
| # 3. Amazon ECR에 로그인 | |
| - name: Login to Amazon ECR | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v2 | |
| # 4. Docker 이미지 빌드, 태깅, ECR에 푸시 | |
| - name: Build, tag, and push image to Amazon ECR | |
| id: build-image | |
| env: | |
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
| ECR_REPOSITORY: cp_main_be | |
| IMAGE_TAG: ${{ github.sha }} | |
| run: | | |
| docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
| docker tag $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG $ECR_REGISTRY/$ECR_REPOSITORY:latest | |
| docker push --all-tags $ECR_REGISTRY/$ECR_REPOSITORY | |
| # 5. 이미지 푸시 성공 상태 설정 | |
| - name: Set push success status | |
| id: push-status | |
| run: echo "success=true" >> $GITHUB_OUTPUT | |
| # EC2 배포 작업 (이미지 푸시가 성공했을 때만) | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: [build, check-ec2, build-and-push-image] | |
| if: github.event_name == 'push' && needs.build-and-push-image.outputs.image-pushed == 'true' | |
| steps: | |
| # 1. AWS 자격 증명 설정 (ECR 레지스트리 정보 가져오기 위해) | |
| - name: Configure AWS credentials using OIDC | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} | |
| aws-region: ${{ secrets.AWS_REGION }} | |
| # 2. Amazon ECR에 로그인 (레지스트리 정보 가져오기 위해) | |
| - name: Login to Amazon ECR | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v2 | |
| # 3. EC2에 접속하여 배포 스크립트 실행 | |
| - name: Deploy to EC2 instance | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | | |
| # 하드코딩된 값으로 테스트 | |
| ECR_REGISTRY="${{ steps.login-ecr.outputs.registry }}" | |
| ECR_REPOSITORY="cp_main_be" | |
| AWS_REGION="${{ secrets.AWS_REGION }}" | |
| echo "ECR_REGISTRY: $ECR_REGISTRY" | |
| echo "ECR_REPOSITORY: $ECR_REPOSITORY" | |
| echo "AWS_REGION: $AWS_REGION" | |
| # AWS CLI 설정 | |
| aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REGISTRY | |
| # 1. 이미지 pull | |
| docker pull $ECR_REGISTRY/$ECR_REPOSITORY:latest | |
| # 2. 기존 컨테이너 정리 | |
| if [ $(docker ps -a -q -f name=spring-app-container) ]; then | |
| docker stop spring-app-container | |
| docker rm spring-app-container | |
| fi | |
| # 환경변수 로드 | |
| source /etc/environment | |
| # 3. 새 컨테이너 실행 | |
| docker run -d --name spring-app-container -p 8080:8080 \ | |
| -e DB_URL="$DB_URL" \ | |
| -e DB_USERNAME="$DB_USERNAME" \ | |
| -e DB_PASSWORD="$DB_PASSWORD" \ | |
| -e SPRING_PROFILES_ACTIVE=prod \ | |
| -e R2_ENDPOINT="${{ secrets.R2_ENDPOINT }}" \ | |
| -e R2_BUCKET="${{ secrets.R2_BUCKET }}" \ | |
| -e R2_ACCESS_KEY="${{ secrets.R2_ACCESS_KEY }}" \ | |
| -e R2_SECRET_KEY="${{ secrets.R2_SECRET_KEY }}" \ | |
| $ECR_REGISTRY/$ECR_REPOSITORY:latest | |
| # 워크플로우 실행 결과 요약 | |
| summary: | |
| runs-on: ubuntu-latest | |
| needs: [code-quality, build, check-ec2, build-and-push-image, deploy] | |
| if: always() | |
| steps: | |
| - name: Workflow Summary | |
| run: | | |
| echo "## 워크플로우 실행 결과" >> $GITHUB_STEP_SUMMARY | |
| echo "| 단계 | 상태 |" >> $GITHUB_STEP_SUMMARY | |
| echo "|------|------|" >> $GITHUB_STEP_SUMMARY | |
| echo "| 코드 품질 검사 | ${{ needs.code-quality.result == 'success' && '✅ 성공' || '❌ 실패' }} |" >> $GITHUB_STEP_SUMMARY | |
| echo "| 빌드 및 테스트 | ${{ needs.build.result == 'success' && '✅ 성공' || '❌ 실패' }} |" >> $GITHUB_STEP_SUMMARY | |
| if [[ "${{ github.event_name }}" == "push" ]]; then | |
| echo "| EC2 상태 확인 | ${{ needs.check-ec2.outputs.ec2-available == 'true' && '✅ 사용 가능' || '⚠️ 사용 불가' }} |" >> $GITHUB_STEP_SUMMARY | |
| if [[ "${{ needs.check-ec2.outputs.ec2-available }}" == "true" ]]; then | |
| echo "| Docker 이미지 빌드/푸시 | ${{ needs.build-and-push-image.result == 'success' && '✅ 성공' || '❌ 실패' }} |" >> $GITHUB_STEP_SUMMARY | |
| echo "| EC2 배포 | ${{ needs.deploy.result == 'success' && '✅ 성공' || '❌ 실패' }} |" >> $GITHUB_STEP_SUMMARY | |
| else | |
| echo "| Docker 이미지 빌드/푸시 | ⏭️ 건너뜀 (EC2 사용 불가) |" >> $GITHUB_STEP_SUMMARY | |
| echo "| EC2 배포 | ⏭️ 건너뜀 (EC2 사용 불가) |" >> $GITHUB_STEP_SUMMARY | |
| fi | |
| else | |
| echo "| 배포 관련 작업 | ⏭️ 건너뜀 (PR 이벤트) |" >> $GITHUB_STEP_SUMMARY | |
| fi | |
| echo "" >> $GITHUB_STEP_SUMMARY | |
| if [[ "${{ github.event_name }}" == "pull_request" ]]; then | |
| echo "💡 **PR 모드**: 코드 품질 검사와 빌드/테스트만 실행되었습니다." >> $GITHUB_STEP_SUMMARY | |
| elif [[ "${{ needs.check-ec2.outputs.ec2-available }}" == "false" ]]; then | |
| echo "💡 **참고**: EC2 인스턴스가 중지되어 있거나 접근할 수 없어서 배포 단계를 건너뛰었습니다." >> $GITHUB_STEP_SUMMARY | |
| echo "빌드와 테스트는 정상적으로 완료되었습니다." >> $GITHUB_STEP_SUMMARY | |
| fi |