-
Notifications
You must be signed in to change notification settings - Fork 0
157 lines (130 loc) ยท 5.71 KB
/
dev.yml
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
name: Weeth-BE DEV CI/CD
on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "develop" ]
types: [opened, synchronize, reopened]
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
# gradle caching
- name: Gradle Caching
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*gradle*','**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# gradle ๋น๋
- name: Build with Gradle Wrapper
run: ./gradlew build
# ๋น๋๋ JAR ํ์ผ ํ์ธ
- name: List JAR files
run: ls build/libs
# Docker build & push
- name: Docker build & push
run: |
docker login -u ${{ secrets.DEV_DOCKER_USER_EMAIL }} -p ${{ secrets.DEV_DOCKER_USER_TOKEN }}
docker build -f Dockerfile-dev -t ${{ secrets.DEV_DOCKER_USER_NAME }}/weeth .
docker push ${{ secrets.DEV_DOCKER_USER_NAME }}/weeth
deploy:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v4
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DEV_EC2_SECRET_HOST }}
username: ubuntu
key: ${{ secrets.DEV_EC2_SECRET_PEM }}
envs: GITHUB_SHA
script: |
# Blue-Green Deployment ํฌํธ ๋ฐ ์ปจํ
์ด๋ ์ด๋ฆ ์ค์
BLUE_PORT=8080
GREEN_PORT=8081
BLUE_NAME="blue"
GREEN_NAME="green"
# ํ์ฌ ์คํ ์ค์ธ ์ปจํ
์ด๋ ํ์ธ
IS_BLUE_ON=$(sudo docker ps --filter "name=$BLUE_NAME" --filter "status=running" -q)
IS_GREEN_ON=$(sudo docker ps --filter "name=$GREEN_NAME" --filter "status=running" -q)
# ์คํ ์ค์ธ ์ปจํ
์ด๋์ ๋ฐ๋ผ blue/green ์ปจํ
์ด๋ ์คํ
if [ -n "$IS_BLUE_ON" ]; then
echo "** ${GREEN_PORT} ํฌํธ์์ GREEN ์ปจํ
์ด๋ ์คํ ์ค๋น"
# Docker ์ด๋ฏธ์ง pull
sudo docker pull ${{ secrets.DEV_DOCKER_USER_NAME }}/weeth
sudo docker run --name $GREEN_NAME -d -p $GREEN_PORT:$GREEN_PORT \
--env-file ./weeth-dev.env -e TZ=Asia/Seoul -e SERVER_PORT=$GREEN_PORT \
${{ secrets.DEV_DOCKER_USER_NAME }}/weeth
BEFORE_NAME=$BLUE_NAME
AFTER_NAME=$GREEN_NAME
BEFORE_PORT=$BLUE_PORT
AFTER_PORT=$GREEN_PORT
elif [ -n "$IS_GREEN_ON" ]; then
echo "** ${BLUE_PORT} ํฌํธ์์ BLUE ์ปจํ
์ด๋ ์คํ ์ค๋น"
# Docker ์ด๋ฏธ์ง pull
sudo docker pull ${{ secrets.DEV_DOCKER_USER_NAME }}/weeth
sudo docker run --name $BLUE_NAME -d -p $BLUE_PORT:$BLUE_PORT \
--env-file ./weeth-dev.env -e TZ=Asia/Seoul -e SERVER_PORT=$BLUE_PORT \
${{ secrets.DEV_DOCKER_USER_NAME }}/weeth
BEFORE_NAME=$GREEN_NAME
AFTER_NAME=$BLUE_NAME
BEFORE_PORT=$GREEN_PORT
AFTER_PORT=$BLUE_PORT
else
echo "** ์ด๊ธฐ ์ํ: BLUE ์ปจํ
์ด๋ ์คํ ์ค๋น"
# Docker ์ด๋ฏธ์ง pull
sudo docker pull ${{ secrets.DEV_DOCKER_USER_NAME }}/weeth
sudo docker run --name $BLUE_NAME -d -p $BLUE_PORT:$BLUE_PORT \
--env-file ./weeth-dev.env -e TZ=Asia/Seoul ${{ secrets.DEV_DOCKER_USER_NAME }}/weeth
BEFORE_NAME=$GREEN_NAME
AFTER_NAME=$BLUE_NAME
BEFORE_PORT=$GREEN_PORT
AFTER_PORT=$BLUE_PORT
fi
# ์ปจํ
์ด๋ ์คํ ํ ๋๊ธฐ
echo "** ์ปจํ
์ด๋ ์ด๊ธฐํ ์ค... 30์ด ๋๊ธฐ"
sleep 30
# Health-Check ์ํ
for i in {1..5}; do
echo "** Health-Check ์์ฒญ: curl -i -s http://localhost:${AFTER_PORT}/health-check"
RESPONSE=$(curl -i -s "http://localhost:${AFTER_PORT}/health-check" | grep "HTTP/1.1 200" || echo "fail")
echo "** Health-Check ์๋ต: $RESPONSE"
if [[ "$RESPONSE" == *"HTTP/1.1 200"* ]]; then
echo "** Health-Check ์ฑ๊ณต: ์๋ฒ๊ฐ ์ ์์ ์ผ๋ก ์๋ ์ค์
๋๋ค."
HEALTHY=true
break
else
echo "** Health-Check ์คํจ, ์ฌ์๋ ์ค... (${i}/5)"
HEALTHY=false
sleep 5
fi
done
# Health-Check ๊ฒฐ๊ณผ ํ์ธ
if [ "$HEALTHY" = true ]; then
echo "** Health-Check ์ฑ๊ณต: ${AFTER_NAME} ์ปจํ
์ด๋ ์ ์ ์๋"
# ์ด์ ์ปจํ
์ด๋ ์ค์ง ๋ฐ ์ญ์
echo "** ์ด์ ์ปจํ
์ด๋(${BEFORE_NAME}) ์ข
๋ฃ ๋ฐ ์ญ์ "
sudo docker stop $BEFORE_NAME || true
sudo docker rm $BEFORE_NAME || true
else
echo "** Health-Check ์คํจ: ${AFTER_NAME} ์ปจํ
์ด๋ ์ ๊ฑฐ"
sudo docker stop $AFTER_NAME || true
sudo docker rm $AFTER_NAME || true
echo "** ์ด์ ์ปจํ
์ด๋(${BEFORE_NAME})๋ฅผ ์ ์งํฉ๋๋ค"
fi
# ์ฌ์ฉํ์ง ์๋ ์ด๋ฏธ์ง ์ ๋ฆฌ
echo "** ์ฌ์ฉํ์ง ์๋ Docker ์ด๋ฏธ์ง ์ ๋ฆฌ"
sudo docker image prune -a -f