@@ -114,10 +114,21 @@ jobs:
114114 echo "Deploy Path: $DEPLOY_PATH"
115115
116116 # S3에 파일 업로드 (경로별 배포)
117- aws s3 sync dist/ s3://$BUCKET$DEPLOY_PATH --delete --cache-control "public, max-age=31536000"
117+ # 정적 파일들 업로드 (캐시 설정, HTML/JSON 제외)
118+ aws s3 sync dist/ s3://$BUCKET$DEPLOY_PATH --delete \
119+ --cache-control "public, max-age=31536000" \
120+ --exclude "*.html" \
121+ --exclude "*.json"
118122
119123 # HTML 파일은 캐시하지 않음
120- aws s3 cp dist/index.html s3://$BUCKET$DEPLOY_PATH/index.html --cache-control "no-cache, no-store, must-revalidate"
124+ aws s3 cp dist/index.html s3://$BUCKET$DEPLOY_PATH/index.html \
125+ --cache-control "no-cache, no-store, must-revalidate"
126+
127+ # manifest.json 파일이 있으면 업로드
128+ if [ -f "dist/manifest.json" ]; then
129+ aws s3 cp dist/manifest.json s3://$BUCKET$DEPLOY_PATH/manifest.json \
130+ --cache-control "no-cache, no-store, must-revalidate"
131+ fi
121132
122133 echo "Deployment completed successfully!"
123134 echo "Application URL: https://$BUCKET.s3-website.ap-northeast-2.amazonaws.com$DEPLOY_PATH"
@@ -126,8 +137,60 @@ jobs:
126137 env :
127138 CLOUD_FRONT_ID : ${{ secrets.AWS_CLOUDFRONT_ID}}
128139 run : |
129- aws cloudfront create-invalidation \
130- --distribution-id $CLOUD_FRONT_ID --paths "/*"
140+ if [ -n "$CLOUD_FRONT_ID" ]; then
141+ echo "Invalidating CloudFront cache..."
142+
143+ # 무효화 요청 생성
144+ INVALIDATION_OUTPUT=$(aws cloudfront create-invalidation \
145+ --distribution-id $CLOUD_FRONT_ID \
146+ --paths "/*")
147+
148+ if [ $? -eq 0 ]; then
149+ # 무효화 ID 추출
150+ INVALIDATION_ID=$(echo "$INVALIDATION_OUTPUT" | grep -oP '"Id"\s*:\s*"\K[^"]+' | head -1)
151+
152+ if [ -n "$INVALIDATION_ID" ]; then
153+ echo "✅ CloudFront cache invalidation initiated: $INVALIDATION_ID"
154+
155+ # 무효화 완료까지 대기 (최대 15분)
156+ echo "Waiting for invalidation to complete (this may take up to 15 minutes)..."
157+ MAX_WAIT_TIME=900 # 15분 (초)
158+ WAIT_INTERVAL=10 # 10초마다 확인
159+ ELAPSED_TIME=0
160+
161+ while [ $ELAPSED_TIME -lt $MAX_WAIT_TIME ]; do
162+ sleep $WAIT_INTERVAL
163+ ELAPSED_TIME=$((ELAPSED_TIME + WAIT_INTERVAL))
164+
165+ STATUS_OUTPUT=$(aws cloudfront get-invalidation \
166+ --distribution-id $CLOUD_FRONT_ID \
167+ --id $INVALIDATION_ID 2>&1)
168+
169+ if [ $? -eq 0 ]; then
170+ STATUS=$(echo "$STATUS_OUTPUT" | grep -oP '"Status"\s*:\s*"\K[^"]+' | head -1)
171+
172+ if [ "$STATUS" == "Completed" ]; then
173+ echo "✅ CloudFront cache invalidation completed!"
174+ break
175+ elif [ "$STATUS" == "InProgress" ]; then
176+ PROGRESS=$((ELAPSED_TIME * 100 / MAX_WAIT_TIME))
177+ echo "Invalidation in progress... (${PROGRESS}% of max wait time elapsed)"
178+ fi
179+ fi
180+ done
181+
182+ if [ $ELAPSED_TIME -ge $MAX_WAIT_TIME ]; then
183+ echo "⚠️ Invalidation is still in progress after maximum wait time. It will continue in the background."
184+ fi
185+ else
186+ echo "⚠️ Failed to extract invalidation ID from response"
187+ fi
188+ else
189+ echo "⚠️ Failed to invalidate CloudFront cache"
190+ fi
191+ else
192+ echo "⚠️ CloudFront ID not configured, skipping invalidation"
193+ fi
131194
132195 - name : Notify deployment
133196 if : always()
0 commit comments