@@ -2,18 +2,40 @@ name: Deploy Next.js to ECS
22
33on :
44 push :
5- branches : main
6-
5+ branches :
6+ - main
7+ - develop
78
89env :
910 AWS_REGION : ap-northeast-2
1011 ECR_REPOSITORY : growit-fe
1112
1213jobs :
14+ # 1. 환경 결정
15+ setup :
16+ name : Determine Environment
17+ runs-on : ubuntu-latest
18+ outputs :
19+ environment : ${{ steps.set-env.outputs.environment }}
20+ image_tag : ${{ steps.set-env.outputs.image_tag }}
21+ steps :
22+ - name : Set environment based on branch
23+ id : set-env
24+ run : |
25+ if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
26+ echo "environment=production" >> $GITHUB_OUTPUT
27+ echo "image_tag=prod-${{ github.sha }}" >> $GITHUB_OUTPUT
28+ else
29+ echo "environment=development" >> $GITHUB_OUTPUT
30+ echo "image_tag=dev-${{ github.sha }}" >> $GITHUB_OUTPUT
31+ fi
32+
33+ # 2. Docker 이미지 빌드 및 ECR 푸시
1334 build-and-push :
14- environment : production # Environments에서 Required reviewers 설정 가능
1535 name : Build and Push Docker Image
1636 runs-on : ubuntu-latest
37+ needs : setup
38+ environment : ${{ needs.setup.outputs.environment }}
1739 outputs :
1840 image : ${{ steps.build-image.outputs.image }}
1941 steps :
@@ -37,23 +59,23 @@ jobs:
3759 id : build-image
3860 env :
3961 ECR_REGISTRY : ${{ steps.login-ecr.outputs.registry }}
40- IMAGE_TAG : ${{ github.ref_name }}
62+ IMAGE_TAG : ${{ needs.setup.outputs.image_tag }}
4163 run : |
4264 IMAGE="$ECR_REGISTRY/${{ env.ECR_REPOSITORY }}:$IMAGE_TAG"
43- echo "Building $IMAGE"
65+ echo "Building $IMAGE for ${{ needs.setup.outputs.environment }} "
4466 docker build \
4567 --build-arg NEXT_PUBLIC_API_URL=${{ vars.NEXT_PUBLIC_API_URL }} \
4668 --build-arg NEXT_PUBLIC_REDIRECT_URL=${{ vars.NEXT_PUBLIC_REDIRECT_URL }} \
4769 -t "$IMAGE" .
4870 docker push "$IMAGE"
4971 echo "image=$IMAGE" >> $GITHUB_OUTPUT
5072
51- # 3) PROD 배포: (A) 릴리스 태그 or (B) hotfix-* 브랜치 푸시
52- deploy-prod :
53- name : Deploy to PROD ECS
73+ # 3. ECS 배포
74+ deploy :
75+ name : Deploy to ECS (${{ needs.setup.outputs.environment }})
5476 runs-on : ubuntu-latest
55- needs : build-and-push
56- environment : production # Environments에서 Required reviewers 설정 가능
77+ needs : [setup, build-and-push]
78+ environment : ${{ needs.setup.outputs.environment }}
5779 steps :
5880 - name : Checkout
5981 uses : actions/checkout@v4
@@ -67,37 +89,35 @@ jobs:
6789 aws-access-key-id : ${{ secrets.AWS_ACCESS_KEY_ID }}
6890 aws-secret-access-key : ${{ secrets.AWS_SECRET_ACCESS_KEY }}
6991
70-
71- - name : Set container name (with default)
72- run : echo "CONTAINER_NAME=growit-fe-container" >> $GITHUB_ENV
73-
7492 - name : Download task definition
7593 run : |
7694 aws ecs describe-task-definition \
77- --task-definition "growit-fe-task " \
95+ --task-definition "${{ vars.ECS_TASK_DEFINITION }} " \
7896 --query taskDefinition > task-definition.json
7997
80- - name : Render taskdef (prod)
98+ - name : Render task definition
8199 id : task-def
82100 uses : aws-actions/amazon-ecs-render-task-definition@v1
83101 with :
84102 task-definition : task-definition.json
85- container-name : growit-fe-container
103+ container-name : ${{ vars.ECS_CONTAINER_NAME }}
86104 image : ${{ needs.build-and-push.outputs.image }}
87105
88- - name : Deploy to ECS (PROD)
106+ - name : Deploy to ECS
89107 uses : aws-actions/amazon-ecs-deploy-task-definition@v2
90108 with :
91109 task-definition : ${{ steps.task-def.outputs.task-definition }}
92- service : growit-fe-task-service-3
110+ service : ${{ vars.ECS_SERVICE }}
93111 cluster : growit-fe-cluster
94112 wait-for-service-stability : false
95113
96- - name : Set tag info (PROD)
97- id : tag-info
114+ - name : Deployment Summary
98115 run : |
99- if [[ "$GITHUB_REF" == refs/tags/* ]]; then
100- echo "tag_info=태그: $GITHUB_REF_NAME" >> $GITHUB_OUTPUT
101- else
102- echo "tag_info=브랜치: $GITHUB_REF_NAME" >> $GITHUB_OUTPUT
103- fi
116+ echo "## 🚀 배포 완료" >> $GITHUB_STEP_SUMMARY
117+ echo "" >> $GITHUB_STEP_SUMMARY
118+ echo "| 항목 | 값 |" >> $GITHUB_STEP_SUMMARY
119+ echo "|------|-----|" >> $GITHUB_STEP_SUMMARY
120+ echo "| **환경** | ${{ needs.setup.outputs.environment }} |" >> $GITHUB_STEP_SUMMARY
121+ echo "| **브랜치** | ${{ github.ref_name }} |" >> $GITHUB_STEP_SUMMARY
122+ echo "| **커밋** | ${{ github.sha }} |" >> $GITHUB_STEP_SUMMARY
123+ echo "| **이미지** | ${{ needs.build-and-push.outputs.image }} |" >> $GITHUB_STEP_SUMMARY
0 commit comments