-
Notifications
You must be signed in to change notification settings - Fork 1
219 lines (189 loc) · 8.2 KB
/
backcicd.yaml
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
name: Backend MSA CI/CD Pipeline
on:
push:
branches:
- 48-Develop브랜치-푸시시-CI/CD-구현
jobs:
build-and-push:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- module: gitfolio-auth
port: 8080
service_name: auth
instance_name: "Gitfolio BE1"
instance_index: "1"
- module: gitfolio-member
port: 8081
service_name: member
instance_name: "Gitfolio BE1"
instance_index: "1"
- module: gitfolio-payment
port: 8083
service_name: payment
instance_name: "Gitfolio BE2"
instance_index: "2"
- module: gitfolio-resume
port: 8082
service_name: resume
instance_name: "Gitfolio BE2"
instance_index: "2"
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Create .env file
run: |
cat << EOF > .env
# URL
REDIRECT_ONBOARDING_URL=${{ secrets.REDIRECT_ONBOARDING_URL }}
REDIRECT_MAIN_URL=${{ secrets.REDIRECT_MAIN_URL }}
MEMBER_SERVER_URL=${{ secrets.MEMBER_SERVER_URL }}
PAYMENT_SERVER_URL=${{ secrets.PAYMENT_SERVER_URL }}
AI_SERVER_URL=${{ secrets.AI_SERVER_URL }}
# gRPC Port
MEMBER_GRPC_PORT=${{ secrets.MEMBER_GRPC_PORT }}
# Server Port
AUTH_SERVER_PORT=${{ secrets.AUTH_SERVER_PORT }}
MEMBER_SERVER_PORT=${{ secrets.MEMBER_SERVER_PORT }}
RESUME_SERVER_PORT=${{ secrets.RESUME_SERVER_PORT }}
PAYMENT_SERVER_PORT=${{ secrets.PAYMENT_SERVER_PORT }}
# GitHub OAuth
GH_CLIENT_ID=${{ secrets.GH_CLIENT_ID }}
GH_CLIENT_SECRET=${{ secrets.GH_CLIENT_SECRET }}
GH_REDIRECT_URI=${{ secrets.GH_REDIRECT_URI }}
GH_API_TOKEN=${{ secrets.GH_API_TOKEN }}
# JWT
JWT_SECRET_KEY=${{ secrets.JWT_SECRET_KEY }}
ACCESS_TOKEN_EXPIRY=${{ secrets.ACCESS_TOKEN_EXPIRY }}
REFRESH_TOKEN_EXPIRY=${{ secrets.REFRESH_TOKEN_EXPIRY }}
# Redis Configuration
AUTH_REDIS_HOST=${{ secrets.AUTH_REDIS_HOST }}
AUTH_REDIS_PORT=${{ secrets.AUTH_REDIS_PORT }}
RESUME_REDIS_HOST=${{ secrets.RESUME_REDIS_HOST }}
RESUME_REDIS_PORT=${{ secrets.RESUME_REDIS_PORT }}
# MySQL Configuration
MEMBER_MYSQL_DB_HOST=${{ secrets.MEMBER_MYSQL_DB_HOST }}
MEMBER_MYSQL_DB_PORT=${{ secrets.MEMBER_MYSQL_DB_PORT }}
MEMBER_MYSQL_DB_NAME=${{ secrets.MEMBER_MYSQL_DB_NAME }}
MEMBER_MYSQL_DB_USERNAME=${{ secrets.MEMBER_MYSQL_DB_USERNAME }}
MEMBER_MYSQL_DB_PASSWORD=${{ secrets.MEMBER_MYSQL_DB_PASSWORD }}
LIKE_MYSQL_DB_HOST=${{ secrets.LIKE_MYSQL_DB_HOST }}
LIKE_MYSQL_DB_PORT=${{ secrets.LIKE_MYSQL_DB_PORT }}
LIKE_MYSQL_DB_NAME=${{ secrets.LIKE_MYSQL_DB_NAME }}
LIKE_MYSQL_DB_USERNAME=${{ secrets.LIKE_MYSQL_DB_USERNAME }}
LIKE_MYSQL_DB_PASSWORD=${{ secrets.LIKE_MYSQL_DB_PASSWORD }}
PAYMENT_MYSQL_DB_HOST=${{ secrets.PAYMENT_MYSQL_DB_HOST }}
PAYMENT_MYSQL_DB_PORT=${{ secrets.PAYMENT_MYSQL_DB_PORT }}
PAYMENT_MYSQL_DB_NAME=${{ secrets.PAYMENT_MYSQL_DB_NAME }}
PAYMENT_MYSQL_DB_USERNAME=${{ secrets.PAYMENT_MYSQL_DB_USERNAME }}
PAYMENT_MYSQL_DB_PASSWORD=${{ secrets.PAYMENT_MYSQL_DB_PASSWORD }}
# MongoDB Configuration
MEMBER_MONGO_DB_USERNAME=${{ secrets.MEMBER_MONGO_DB_USERNAME }}
MEMBER_MONGO_DB_PORT=${{ secrets.MEMBER_MONGO_DB_PORT }}
MEMBER_MONGO_DB_DATABASE=${{ secrets.MEMBER_MONGO_DB_DATABASE }}
RESUME_MONGO_DB_USERNAME=${{ secrets.RESUME_MONGO_DB_USERNAME }}
RESUME_MONGO_DB_PORT=${{ secrets.RESUME_MONGO_DB_PORT }}
RESUME_MONGO_DB_DATABASE=${{ secrets.RESUME_MONGO_DB_DATABASE }}
# S3 Configuration
S3_ACCESS_KEY=${{ secrets.S3_ACCESS_KEY }}
S3_SECRET_KEY=${{ secrets.S3_SECRET_KEY }}
S3_URL_PREFIX=${{ secrets.S3_URL_PREFIX }}
# Kakao Pay
KAKAO_API_URL=${{ secrets.KAKAO_API_URL }}
KAKAOPAY_SECRET_KEY=${{ secrets.KAKAOPAY_SECRET_KEY }}
# Kafka Configuration
KAFKA_HOST1=${{ secrets.KAFKA_HOST1 }}
KAFKA_PORT1=${{ secrets.KAFKA_PORT1 }}
EOF
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
# Builder 이미지 빌드
- name: Build and push builder image
run: |
docker compose build builder
docker compose push builder
# 서비스 이미지 빌드 및 푸시 (환경변수 포함)
- name: Build and push service images
run: |
docker compose build ${{ matrix.service_name }}
docker compose push ${{ matrix.service_name }}
deploy:
needs: build-and-push
runs-on: ubuntu-latest
strategy:
matrix:
include:
- module: gitfolio-auth
port: 8080
service_name: auth
instance_name: "Gitfolio BE1"
instance_index: "1"
- module: gitfolio-member
port: 8081
service_name: member
instance_name: "Gitfolio BE1"
instance_index: "1"
- module: gitfolio-payment
port: 8083
service_name: payment
instance_name: "Gitfolio BE2"
instance_index: "2"
- module: gitfolio-resume
port: 8082
service_name: resume
instance_name: "Gitfolio BE2"
instance_index: "2"
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Create .env file
run: |
# 위의 .env 파일 생성 스크립트와 동일
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2
- name: Get EC2 instance ID
id: get-instance
run: |
INSTANCE_ID=$(aws ec2 describe-instances \
--filters \
"Name=tag:Service,Values=back" \
"Name=tag:Environment,Values=dev" \
"Name=tag:Name,Values=${{ matrix.instance_name }}" \
"Name=tag:Index,Values=${{ matrix.instance_index }}" \
"Name=instance-state-name,Values=running" \
--query "Reservations[*].Instances[*].InstanceId" \
--output text)
echo "instance_id=$INSTANCE_ID" >> $GITHUB_OUTPUT
- name: Install Session Manager Plugin
run: |
curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb"
sudo dpkg -i session-manager-plugin.deb
- name: Copy .env and docker-compose.yml to EC2
run: |
aws ssm start-session \
--target "${{ steps.get-instance.outputs.instance_id }}" \
--document-name "AWS-StartInteractiveCommand" \
--parameters "{\"command\":[\"cat > /home/ec2-user/.env << 'EOL'\n$(cat .env)\nEOL\"]}"
aws ssm start-session \
--target "${{ steps.get-instance.outputs.instance_id }}" \
--document-name "AWS-StartInteractiveCommand" \
--parameters "{\"command\":[\"cat > /home/ec2-user/docker-compose.yml << 'EOL'\n$(cat docker-compose.yml)\nEOL\"]}"
- name: Deploy to EC2 using SSM and Docker Compose
run: |
aws ssm start-session \
--target "${{ steps.get-instance.outputs.instance_id }}" \
--document-name "AWS-StartInteractiveCommand" \
--parameters "{\"command\":[\"cd /home/ec2-user && \
docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} && \
docker compose pull ${{ matrix.service_name }} && \
docker compose up -d ${{ matrix.service_name }}\"]}"