2121 CONTAINER_1_NAME : relife_1 # 슬롯1(고정 이름)
2222 CONTAINER_2_NAME : relife_2 # 슬롯2(고정 이름)
2323 CONTAINER_PORT : 8080 # 컨테이너 내부 포트(스프링부트)
24+ HEALTH_CHECK_PORT : 8090 # 헬스체크용 포트(임시, 추후 필요 시)
2425 EC2_INSTANCE_TAG_NAME : relife-ec2-1 # 배포 대상 EC2 Name 태그
2526 DOCKER_NETWORK : common # 도커 네트워크
2627 BACKEND_DIR : back # Dockerfile 위치
@@ -87,15 +88,73 @@ jobs:
8788 prerelease : false
8889
8990 # ---------------------------------------------------------
90- # 2) 도커 이미지 빌드/푸시 (캐시 최대 활용)
91+ # 2) 커밋 해시 값으로부터 PR 당시 Artifact의 run_id 추출
92+ # ---------------------------------------------------------
93+ findCorrespondingCIrun :
94+ runs-on : ubuntu-latest
95+ outputs :
96+ run_id : ${{ steps.find_run.outputs.run_id }}
97+ steps :
98+ - uses : actions/checkout@v4
99+ with :
100+ fetch-depth : 0 # .git 히스토리 가져오기
101+
102+ - name : 연관있는 PR 및 CI workflow 실행 찾기
103+ id : find_run
104+ env :
105+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
106+ run : |
107+ # 1. 현재 push를 유발한 커밋(merge commit) SHA 가져오기
108+ MERGE_COMMIT_SHA=${{ github.sha }}
109+ echo "Merge commit SHA: $MERGE_COMMIT_SHA"
110+
111+ # 2. 커밋 SHA를 이용해 병합된 PR 번호 찾기
112+ PR_NUMBER=$(gh pr list --search "$MERGE_COMMIT_SHA" --state merged --json number --jq '.[0].number')
113+ if [ -z "$PR_NUMBER" ]; then
114+ echo "⚠️ Could not find a merged PR for this commit"
115+ exit 1
116+ fi
117+ echo "✅ Found PR number: $PR_NUMBER"
118+
119+ # 3. PR의 마지막 커밋(head SHA) 알아내기
120+ PR_HEAD_SHA=$(gh pr view "$PR_NUMBER" --json headRefOid --jq '.headRefOid')
121+ echo "PR head SHA: $PR_HEAD_SHA"
122+
123+ # 4. PR의 마지막 커밋과 CI 워크플로우 파일 이름을 이용해 성공한 CI 실행(run)의 ID 찾기
124+ # CI 워크플로우 파일명과 일치
125+ RUN_ID=$(gh run list --workflow="Backend-CI.yml" --commit="$PR_HEAD_SHA" --status=success --json databaseId --jq '.[0].databaseId')
126+ if [ -z "$RUN_ID" ]; then
127+ echo "⚠️ Could not find a successful CI run for this PR."
128+ exit 1
129+ fi
130+ echo "✅ Found CI run ID: $RUN_ID"
131+
132+ # 5. 찾은 RUN_ID를 output으로 내보내기
133+ echo "run_id=$RUN_ID" >> $GITHUB_OUTPUT
134+
135+ # ---------------------------------------------------------
136+ # 3) 도커 이미지 빌드/푸시
91137 # ---------------------------------------------------------
92138 buildImageAndPush :
93139 name : 도커 이미지 빌드와 푸시
94- needs : createTagAndRelease
140+ needs : [ createTagAndRelease, findCorrespondingCIrun]
95141 runs-on : ubuntu-latest
96142 steps :
97143 - uses : actions/checkout@v4
98144
145+ - name : Create dist directory
146+ working-directory : back
147+ run : mkdir -p dist
148+
149+ - name : CI 상에서 나온 Artifact 다운로드
150+ uses : dawidd6/action-download-artifact@v6
151+ with :
152+ # 앞 단계에서 찾은 run_id 사용
153+ workflow : Backend-CI.yml
154+ run_id : ${{ needs.findCorrespondingCIrun.outputs.run_id }}
155+ name : relife-backend-jar
156+ path : ' ${{ env.BACKEND_DIR }}/dist' # BACKEND_DIR 기준
157+
99158 - name : Docker Buildx 설치
100159 uses : docker/setup-buildx-action@v3
101160
@@ -126,7 +185,7 @@ jobs:
126185 ghcr.io/${{ env.OWNER_LC }}/${{ env.IMAGE_REPOSITORY }}:latest
127186
128187 # ---------------------------------------------------------
129- # 3 ) Blue/Green 무중단 배포 (EC2 + NPM 스위치)
188+ # 4 ) Blue/Green 무중단 배포 (EC2 + NPM 스위치)
130189 # ---------------------------------------------------------
131190 deploy :
132191 name : Blue/Green 무중단 배포
@@ -187,6 +246,7 @@ jobs:
187246 SLOT1="${{ env.CONTAINER_1_NAME }}"
188247 SLOT2="${{ env.CONTAINER_2_NAME }}"
189248 PORT_IN="${{ env.CONTAINER_PORT }}"
249+ HEALTH_PORT="${{ env.HEALTH_CHECK_PORT }}"
190250 NET="${{ env.DOCKER_NETWORK }}"
191251 ENV_FILE="/tmp/relife.env"
192252
@@ -268,7 +328,7 @@ jobs:
268328 "${IMAGE}"
269329
270330 # ---------------------------------------------------------
271- # 5) 헬스체크 (/actuator/health 200 OK까지 대기)
331+ # 5) 헬스체크 (/actuator/health/readiness 200 OK까지 대기)
272332 # ---------------------------------------------------------
273333 echo "⏱ health-check: ${GREEN}"
274334 TIMEOUT=120
@@ -277,7 +337,7 @@ jobs:
277337 sleep 8 # 초기 부팅 여유
278338
279339 while (( ELAPSED < TIMEOUT )); do
280- CODE=$(docker exec "${GREEN}" curl -s -o /dev/null -w "%{http_code}" "http://127.0.0.1:${PORT_IN }/actuator/health" || echo 000)
340+ CODE=$(docker exec "${GREEN}" curl -s -o /dev/null -w "%{http_code}" "http://127.0.0.1:${HEALTH_PORT }/actuator/health/readiness " || echo 000)
281341 if [[ "${CODE}" == "200" ]]; then
282342 echo "✅ ${GREEN} is healthy"
283343 break
0 commit comments