Merge pull request #212 from YAPP-Github/develop #55
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 PROD & COMMON Infra, Service | |
| on: | |
| push: | |
| branches: [ main ] | |
| paths: | |
| - 'src/**' | |
| - 'terraform/common/**' | |
| - 'terraform/prod/**' | |
| - '.github/workflows/deploy-prod.yml' | |
| workflow_dispatch: | |
| inputs: | |
| rollback_version: | |
| description: '롤백할 버전을 입력하세요 (예: 1.3.0). 비워두면 일반 배포를 실행합니다.' | |
| required: false | |
| default: '' | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| issues: write | |
| concurrency: | |
| group: terraform | |
| cancel-in-progress: false | |
| env: | |
| AWS_REGION: ap-northeast-2 | |
| jobs: | |
| terraform-apply-common: | |
| name: Terraform Apply COMMON | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: hashicorp/setup-terraform@v3 | |
| - 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: ${{ env.AWS_REGION }} | |
| - name: Terraform Init (common) | |
| run: terraform init | |
| working-directory: ./terraform/common | |
| - name: Terraform Apply COMMON | |
| run: terraform apply -auto-approve | |
| working-directory: ./terraform/common | |
| terraform-apply-prod: | |
| name: Terraform Apply prod | |
| runs-on: ubuntu-latest | |
| needs: terraform-apply-common | |
| outputs: | |
| tf_outputs_json: ${{ steps.get-outputs.outputs.data }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: hashicorp/setup-terraform@v3 | |
| with: | |
| terraform_wrapper: false | |
| - 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: ${{ env.AWS_REGION }} | |
| - name: Terraform Init (prod) | |
| run: terraform init | |
| working-directory: ./terraform/prod | |
| - name: Terraform Apply prod | |
| run: terraform apply -auto-approve | |
| working-directory: ./terraform/prod | |
| - name: Get Terraform Outputs | |
| id: get-outputs | |
| run: | | |
| echo "data<<EOF" >> $GITHUB_OUTPUT | |
| terraform output -json >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| working-directory: ./terraform/prod | |
| deploy-service: | |
| name: Deploy to Amazon ECS | |
| runs-on: ubuntu-latest | |
| environment: production | |
| needs: terraform-apply-prod | |
| outputs: | |
| outcome: ${{ job.status }} | |
| has_version: ${{ steps.get_version.outputs.HAS_VERSION }} | |
| version: ${{ steps.get_version.outputs.VERSION }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Parse Terraform Outputs and Set Environment Variables | |
| run: | | |
| TF_OUTPUTS='${{ needs.terraform-apply-prod.outputs.tf_outputs_json }}' | |
| echo "ECR_REPOSITORY=$(echo "$TF_OUTPUTS" | jq -r '.ecr_repository_name.value')" >> $GITHUB_ENV | |
| echo "ECS_CLUSTER=$(echo "$TF_OUTPUTS" | jq -r '.ecs_cluster_name.value')" >> $GITHUB_ENV | |
| echo "ECS_SERVICE=$(echo "$TF_OUTPUTS" | jq -r '.ecs_api_service_name.value')" >> $GITHUB_ENV | |
| echo "CONTAINER_NAME=$(echo "$TF_OUTPUTS" | jq -r '.ecs_api_container_name.value')" >> $GITHUB_ENV | |
| echo "RDS_ENDPOINT=$(echo "$TF_OUTPUTS" | jq -r '.rds_endpoint.value')" >> $GITHUB_ENV | |
| echo "CLOUDFRONT_DOMAIN_NAME=$(echo "$TF_OUTPUTS" | jq -r '.cloudfront_domain_name.value')" >> $GITHUB_ENV | |
| - 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: ${{ env.AWS_REGION }} | |
| - name: Update CLOUDFRONT_DOMAIN_NAME in Parameter Store | |
| run: | | |
| aws ssm put-parameter \ | |
| --name "/prod/CLOUDFRONT_DOMAIN_NAME" \ | |
| --type "SecureString" \ | |
| --value "$CLOUDFRONT_DOMAIN_NAME" \ | |
| --overwrite | |
| - name: Update DB URL in Parameter Store | |
| run: | | |
| aws ssm put-parameter --name "/prod/RDS_ENDPOINT" --value "jdbc:mysql://${{ env.RDS_ENDPOINT }}/eatda?useUnicode=true&characterEncoding=UTF-8" --type SecureString --overwrite | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '22' | |
| - name: Install Semantic Release dependencies | |
| run: | | |
| npm install \ | |
| semantic-release \ | |
| @semantic-release/commit-analyzer \ | |
| @semantic-release/release-notes-generator \ | |
| @semantic-release/github \ | |
| @semantic-release/changelog \ | |
| @semantic-release/git \ | |
| conventional-changelog-conventionalcommits | |
| - name: Get Next Version or Set Rollback | |
| id: get_version | |
| run: | | |
| if [[ -n "${{ github.event.inputs.rollback_version }}" ]]; then | |
| echo "수동 롤백을 시작합니다. 버전: ${{ github.event.inputs.rollback_version }}" | |
| echo "HAS_VERSION=true" >> $GITHUB_OUTPUT | |
| echo "VERSION=${{ github.event.inputs.rollback_version }}" >> $GITHUB_OUTPUT | |
| echo "SEMANTIC_VERSION=${{ github.event.inputs.rollback_version }}" >> $GITHUB_ENV | |
| echo "SKIP_BUILD=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "배포할 버전을 결정하기 위해 dry-run을 실행합니다..." | |
| OUTPUT=$(npm exec --no -- semantic-release --dry-run --no-ci) | |
| echo "$OUTPUT" | |
| VERSION=$(echo "$OUTPUT" | grep -oP 'The next release version is \K[0-9.a-z-]+' | head -n 1) | |
| if [[ -z "$VERSION" ]]; then | |
| echo "릴리즈할 새로운 버전이 없습니다. 배포를 건너뜁니다." | |
| echo "HAS_VERSION=false" >> $GITHUB_OUTPUT | |
| echo "SKIP_BUILD=false" >> $GITHUB_OUTPUT | |
| else | |
| echo "배포 예정 버전: $VERSION" | |
| echo "HAS_VERSION=true" >> $GITHUB_OUTPUT | |
| echo "VERSION=$VERSION" >> $GITHUB_OUTPUT | |
| echo "SEMANTIC_VERSION=$VERSION" >> $GITHUB_ENV | |
| echo "SKIP_BUILD=false" >> $GITHUB_OUTPUT | |
| fi | |
| fi | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up JDK 21 | |
| if: steps.get_version.outputs.HAS_VERSION == 'true' && steps.get_version.outputs.SKIP_BUILD != 'true' | |
| uses: actions/setup-java@v4 | |
| with: | |
| distribution: 'temurin' | |
| java-version: 21 | |
| java-package: jdk | |
| architecture: 'x64' | |
| cache: 'gradle' | |
| - name: Get TEST_JWT_SECRET_KEY from SSM | |
| if: steps.get_version.outputs.HAS_VERSION == 'true' && steps.get_version.outputs.SKIP_BUILD != 'true' | |
| id: get-test-secret | |
| run: | | |
| SECRET_VALUE=$(aws ssm get-parameter --name "/common/TEST_JWT_SECRET_KEY" --with-decryption --region "${{ env.AWS_REGION }}" --query "Parameter.Value" --output text) | |
| echo "TEST_JWT_SECRET_KEY=$SECRET_VALUE" >> $GITHUB_ENV | |
| - name: Build with Gradle | |
| if: steps.get_version.outputs.HAS_VERSION == 'true' && steps.get_version.outputs.SKIP_BUILD != 'true' | |
| run: | | |
| cd ${{ github.workspace }} | |
| chmod +x gradlew | |
| ./gradlew clean build -Dspring.profiles.active=prod | |
| env: | |
| TEST_JWT_SECRET_KEY: ${{ env.TEST_JWT_SECRET_KEY }} | |
| - name: Login to Amazon ECR | |
| if: steps.get_version.outputs.HAS_VERSION == 'true' | |
| id: login-ecr | |
| uses: aws-actions/amazon-ecr-login@v2 | |
| with: | |
| mask-password: 'true' | |
| - name: Build, tag, and push image to Amazon ECR | |
| if: steps.get_version.outputs.HAS_VERSION == 'true' && steps.get_version.outputs.SKIP_BUILD != 'true' | |
| env: | |
| ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
| run: | | |
| docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$SEMANTIC_VERSION . | |
| docker push $ECR_REGISTRY/$ECR_REPOSITORY:$SEMANTIC_VERSION | |
| - name: Set Image URI | |
| if: steps.get_version.outputs.HAS_VERSION == 'true' | |
| id: set-image | |
| run: | | |
| ECR_REGISTRY=$(aws ecr describe-repositories --repository-names ${{ env.ECR_REPOSITORY }} --query "repositories[0].registryId" --output text).dkr.ecr.${{ env.AWS_REGION }}.amazonaws.com | |
| echo "image=$ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:${{ env.SEMANTIC_VERSION }}" >> $GITHUB_OUTPUT | |
| - name: Get latest ECS task definition | |
| if: steps.get_version.outputs.HAS_VERSION == 'true' | |
| id: get-latest-task-def | |
| run: | | |
| if [[ -n "${{ github.event.inputs.rollback_version }}" ]]; then | |
| echo "롤백 모드: 현재 서비스에서 실행 중인 안정적인 Task Definition을 사용합니다." | |
| TASK_DEF_ARN=$(aws ecs describe-services --cluster "${{ env.ECS_CLUSTER }}" --services "${{ env.ECS_SERVICE }}" --region "${{ env.AWS_REGION }}" --query "services[0].taskDefinition" --output text) | |
| else | |
| echo "일반 배포 모드: 가장 최신 버전의 Task Definition을 사용합니다." | |
| TASK_DEF_ARN=$(aws ecs describe-task-definition --task-definition "${{ env.CONTAINER_NAME }}" --query "taskDefinition.taskDefinitionArn" --output text) | |
| fi | |
| echo "사용할 Task Definition ARN: $TASK_DEF_ARN" | |
| aws ecs describe-task-definition --task-definition "$TASK_DEF_ARN" --region "${{ env.AWS_REGION }}" --query "taskDefinition" --output json > task-definition.json | |
| - name: Fill in the new image ID in the Amazon ECS task definition | |
| if: steps.get_version.outputs.HAS_VERSION == 'true' | |
| id: task-def | |
| uses: aws-actions/amazon-ecs-render-task-definition@v1 | |
| with: | |
| task-definition: task-definition.json | |
| container-name: ${{ env.CONTAINER_NAME }} | |
| image: ${{ steps.set-image.outputs.image }} | |
| - name: Deploy Amazon ECS task definition and wait for stability | |
| if: steps.get_version.outputs.HAS_VERSION == 'true' | |
| id: deploy-ecs | |
| uses: aws-actions/amazon-ecs-deploy-task-definition@v2 | |
| with: | |
| task-definition: ${{ steps.task-def.outputs.task-definition }} | |
| service: ${{ env.ECS_SERVICE }} | |
| cluster: ${{ env.ECS_CLUSTER }} | |
| wait-for-service-stability: true | |
| - name: Verify deployment by comparing Task Definition ARNs | |
| if: steps.get_version.outputs.HAS_VERSION == 'true' | |
| run: | | |
| DEPLOYED_ARN="${{ steps.deploy-ecs.outputs.task-definition-arn }}" | |
| FINAL_ARN=$(aws ecs describe-services --cluster "${{ env.ECS_CLUSTER }}" --services "${{ env.ECS_SERVICE }}" --region "${{ env.AWS_REGION }}" --query "services[0].taskDefinition" --output text) | |
| echo "배포 시도 ARN: $DEPLOYED_ARN" | |
| echo "실제 적용된 ARN: $FINAL_ARN" | |
| if [[ "$DEPLOYED_ARN" == "$FINAL_ARN" ]]; then | |
| echo "✅ 검증 성공. 서비스가 올바른 새 태스크 정의로 실행 중입니다." | |
| else | |
| echo "❌ 검증 실패. 롤백이 발생했습니다." | |
| echo "서비스가 다른 태스크 정의($FINAL_ARN)로 안정화되었습니다." | |
| exit 1 | |
| fi | |
| - name: Create GitHub Release (Final) | |
| if: success() && steps.get_version.outputs.HAS_VERSION == 'true' && steps.get_version.outputs.SKIP_BUILD != 'true' | |
| run: npm exec --no -- semantic-release --no-ci | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| notify: | |
| name: Send Discord Notification | |
| runs-on: ubuntu-latest | |
| needs: deploy-service | |
| if: always() | |
| steps: | |
| - name: Prepare Notification Info | |
| id: vars | |
| run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
| - name: Discord Notify (Success) | |
| if: needs.deploy-service.outputs.has_version == 'true' && needs.deploy-service.outputs.outcome == 'success' | |
| uses: tsickert/[email protected] | |
| with: | |
| webhook-url: ${{ secrets.DISCORD_WEBHOOK }} | |
| embed-title: "✅ 프로덕션 서버 배포 성공!" | |
| embed-color: 65280 | |
| embed-description: | | |
| 새로운 버전이 성공적으로 배포 및 릴리즈되었습니다. | |
| **버전**: [v${{ needs.deploy-service.outputs.version }}](${{ github.server_url }}/${{ github.repository }}/releases/tag/v${{ needs.deploy-service.outputs.version }}) | |
| **커밋**: [${{ steps.vars.outputs.sha_short }}](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}) | |
| **배포자**: ${{ github.actor }} | |
| embed-url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" | |
| - name: Discord Notify (Failure) | |
| if: needs.deploy-service.outputs.has_version == 'true' && needs.deploy-service.outputs.outcome == 'failure' | |
| uses: tsickert/[email protected] | |
| with: | |
| webhook-url: ${{ secrets.DISCORD_WEBHOOK }} | |
| embed-title: "❌ 프로덕션 서버 배포 실패!" | |
| embed-color: 16711680 | |
| embed-description: | | |
| 배포 과정 중 오류가 발생했거나 롤백되었습니다. GitHub 릴리즈는 생성되지 않았습니다. | |
| **시도 버전**: v${{ needs.deploy-service.outputs.version }} | |
| **커밋**: [${{ steps.vars.outputs.sha_short }}](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}) | |
| **요청자**: ${{ github.actor }} | |
| embed-url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" | |
| - name: Discord Notify (No Version to Release) | |
| if: needs.deploy-service.outputs.has_version == 'false' | |
| uses: tsickert/[email protected] | |
| with: | |
| webhook-url: ${{ secrets.DISCORD_WEBHOOK }} | |
| embed-title: "ℹ️ 프로덕션 서버 배포 건너뜀" | |
| embed-color: 8421504 | |
| embed-description: | | |
| 릴리즈할 새로운 버전이 없어 배포를 진행하지 않았습니다. | |
| **커밋**: [${{ steps.vars.outputs.sha_short }}](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}) | |
| **요청자**: ${{ github.actor }} | |
| embed-url: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |