-
Notifications
You must be signed in to change notification settings - Fork 5
179 lines (147 loc) · 6.22 KB
/
cd-dev.yml
File metadata and controls
179 lines (147 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
name: CD - Deploy to EC2
on:
push:
branches: [develop]
workflow_dispatch: # 수동 실행 가능
env:
REGISTRY: ghcr.io
IMAGE_PREFIX: ghcr.io/${{ github.repository_owner }}
jobs:
# ==========================================
# Build & Push Docker Images
# ==========================================
build-and-push:
name: Build and Push Images
runs-on: ubuntu-latest
permissions:
contents: read
packages: write # GHCR 푸시 권한
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Convert repository name to lowercase
id: repo
run: echo "repo_lower=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT
- name: Build and push Backend image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
target: production
push: true
tags: |
ghcr.io/${{ steps.repo.outputs.repo_lower }}/backend:latest
ghcr.io/${{ steps.repo.outputs.repo_lower }}/backend:${{ github.sha }}
cache-from: type=registry,ref=ghcr.io/${{ steps.repo.outputs.repo_lower }}/backend:latest
cache-to: type=inline
- name: Build and push FastAPI image
uses: docker/build-push-action@v5
with:
context: ./fastapi-server
file: ./fastapi-server/Dockerfile
push: true
tags: |
ghcr.io/${{ steps.repo.outputs.repo_lower }}/fastapi:latest
ghcr.io/${{ steps.repo.outputs.repo_lower }}/fastapi:${{ github.sha }}
cache-from: type=registry,ref=ghcr.io/${{ steps.repo.outputs.repo_lower }}/fastapi:latest
cache-to: type=inline
# ==========================================
# Deploy to EC2 Server
# ==========================================
deploy:
name: Deploy to EC2
runs-on: ubuntu-latest
needs: build-and-push
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Convert repository name to lowercase
id: repo
run: echo "repo_lower=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT
- name: Deploy to EC2 via SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_SSH_KEY }}
port: 22
script: |
# 프로젝트 디렉토리로 이동
cd /home/ubuntu/moduwa-server || exit 1
# 최신 코드 pull
git fetch origin
git checkout develop
git pull origin develop
# GHCR 로그인 (GitHub Token 사용)
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
# 최신 이미지 pull
docker pull ghcr.io/${{ steps.repo.outputs.repo_lower }}/backend:latest
docker pull ghcr.io/${{ steps.repo.outputs.repo_lower }}/fastapi:latest
# docker-compose.yml에서 이미지 이름 업데이트
export BACKEND_IMAGE=ghcr.io/${{ steps.repo.outputs.repo_lower }}/backend:latest
export FASTAPI_IMAGE=ghcr.io/${{ steps.repo.outputs.repo_lower }}/fastapi:latest
# 기존 컨테이너 중지 및 제거
docker-compose down
# 컨테이너 시작
docker-compose up -d
# 컨테이너 시작 대기
echo "컨테이너 시작 대기 중..."
sleep 15
# 컨테이너 상태 확인
echo "컨테이너 상태:"
docker-compose ps
# docker-compose.yml에 정의된 서비스 개수 확인
EXPECTED_COUNT=$(docker-compose config --services | wc -l)
RUNNING_COUNT=$(docker-compose ps | grep "Up" | wc -l)
echo "예상 컨테이너 개수: $EXPECTED_COUNT"
echo "실행 중인 컨테이너 개수: $RUNNING_COUNT"
if [ "$RUNNING_COUNT" -lt "$EXPECTED_COUNT" ]; then
echo "에러: 일부 컨테이너가 실행되지 않았습니다."
echo "컨테이너 로그:"
docker-compose logs --tail=50
exit 1
fi
# Prisma 마이그레이션 실행
echo "Prisma 마이그레이션 실행 중..."
docker-compose exec -T backend npx prisma migrate deploy || {
echo "Prisma 마이그레이션 실패"
echo "Backend 로그:"
docker-compose logs --tail=50 backend
exit 1
}
echo "Prisma 마이그레이션 완료"
# 최종 헬스체크
echo "헬스체크 진행 중..."
sleep 5
# Backend 헬스체크 (포트 3000)
if ! curl -f http://localhost:3000/ > /dev/null 2>&1; then
echo "Backend 헬스체크 실패 (포트 3000이 응답하지 않음)"
else
echo "Backend 정상 작동"
fi
# FastAPI 헬스체크 (포트 8000)
if ! curl -f http://localhost:8000/health > /dev/null 2>&1; then
echo "FastAPI 헬스체크 실패 (포트 8000이 응답하지 않음)"
else
echo "FastAPI 정상 작동"
fi
echo "배포 완료: develop-${{ github.sha }}"
# 구버전 이미지 정리
docker image prune -af --filter "until=24h"
- name: Deployment Status
if: always()
run: |
if [ "${{ job.status }}" == "success" ]; then
echo "배포 성공 - 최신 이미지로 업데이트되었습니다"
else
echo "배포 실패 - 로그를 확인하세요"
exit 1
fi