Merge pull request #134 from RunChuck/SCRUM-241-국문관광정보-API-동기화-구현 #48
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: Deploy SpringBoot to AWS EC2 | |
| on: | |
| push: | |
| branches: [ "main" ] # 'main' 브랜치에 push(merge) 이벤트가 발생했을 때 실행 | |
| workflow_dispatch: # 수동 실행을 위한 트리거 | |
| jobs: | |
| build-and-deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 1. 소스코드 체크아웃 | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| # 2. JDK 21 설치 (Gradle 빌드를 위해 필요) | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| # 3. Gradle 캐시 설정 | |
| - name: Cache Gradle packages | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| ~/.gradle/caches | |
| ~/.gradle/wrapper | |
| key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
| restore-keys: | | |
| ${{ runner.os }}-gradle- | |
| # 4. gradlew 실행 권한 부여 | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x ./gradlew | |
| # 5. Gradle로 단위 테스트 실행 | |
| - name: Run unit tests with Gradle | |
| run: ./gradlew test | |
| # 6. AWS 자격 증명 설정 (GitHub Secrets 사용) | |
| - 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: ap-northeast-2 | |
| # 7. AWS ECR에 로그인 | |
| - name: Login to Amazon ECR | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v2 | |
| # 8. Docker 이미지 빌드, 태그 지정, ECR에 푸시, image_uri.txt 생성 | |
| - name: Build, tag, and push image to Amazon ECR | |
| id: build-image | |
| env: | |
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
| ECR_REPOSITORY: "runninghandai-app" | |
| IMAGE_TAG: ${{ github.sha }} | |
| run: | | |
| docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
| echo "$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" > image_uri.txt | |
| # 9. 배포할 파일들만 모아서 압축 | |
| - name: Make zip file | |
| run: zip -r deploy.zip appspec.yml image_uri.txt scripts | |
| # 10. 압축 파일을 S3 버킷에 업로드 | |
| - name: Upload to S3 | |
| env: | |
| S3_BUCKET_NAME: "runninghandai-bucket" | |
| IMAGE_TAG: ${{ github.sha }} | |
| run: | | |
| aws s3 cp deploy.zip s3://$S3_BUCKET_NAME/codedeploy-artifacts/$IMAGE_TAG-deploy.zip | |
| # 11. AWS CodeDeploy에 배포 생성 및 완료 대기 ★★★ | |
| - name: Deploy to EC2 with CodeDeploy | |
| env: | |
| APPLICATION_NAME: "runninghandai-app" | |
| DEPLOYMENT_GROUP_NAME: "runninghandai-app-prod" | |
| S3_BUCKET_NAME: "runninghandai-bucket" | |
| IMAGE_TAG: ${{ github.sha }} | |
| run: | | |
| # 1. 배포를 생성하고, 결과에서 deploymentId를 추출하여 변수에 저장한다. | |
| DEPLOYMENT_ID=$(aws deploy create-deployment \ | |
| --application-name $APPLICATION_NAME \ | |
| --deployment-group-name $DEPLOYMENT_GROUP_NAME \ | |
| --s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=codedeploy-artifacts/$IMAGE_TAG-deploy.zip \ | |
| --query 'deploymentId' --output text) | |
| echo "Deployment started with ID: $DEPLOYMENT_ID" | |
| # 2. 해당 배포가 '성공' 상태가 될 때까지 대기한다. | |
| # 만약 배포가 실패하거나 타임아웃되면, 이 명령어는 에러를 발생시키고 워크플로우를 실패 처리한다. | |
| aws deploy wait deployment-successful --deployment-id $DEPLOYMENT_ID | |
| echo "CodeDeploy deployment to EC2 successful!" | |
| send-slack-notification: | |
| name: Send Slack Notification | |
| runs-on: ubuntu-latest | |
| needs: build-and-deploy | |
| if: always() | |
| steps: | |
| - name: Send notification on success | |
| if: needs.build-and-deploy.result == 'success' | |
| uses: act10ns/slack@v2 | |
| with: | |
| status: success | |
| message: | | |
| ✅ 배포 성공! | |
| `main` 브랜치의 최신 코드가 EC2에 성공적으로 배포되었습니다. | |
| <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|배포 로그 보기> | |
| env: | |
| SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} | |
| - name: Send notification on failure | |
| if: needs.build-and-deploy.result == 'failure' | |
| uses: act10ns/slack@v2 | |
| with: | |
| status: failure | |
| message: | | |
| 🚨 배포 실패! | |
| 워크플로우 실행 중 에러가 발생했습니다. 아래 링크에서 로그를 확인해주세요. | |
| <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|실패 로그 보기> | |
| env: | |
| SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |