-
Notifications
You must be signed in to change notification settings - Fork 0
273 lines (238 loc) · 11.4 KB
/
CICD.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
name: Java CICD with Gradle
on:
push:
branches:
- feature/CICD-test
pull_request:
branches:
- main
types: [closed]
permissions:
contents: read
jobs:
set-variable:
runs-on: [self-hosted, loadbalancer-server]
outputs:
is_blue_env: ${{ steps.check-blue.outputs.is_blue_env }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Check Blue Environment
id: check-blue
run: |
IS_BLUE_ENV=$(sh /usr/local/bin/scripts/is_blue_env.sh)
echo "::set-output name=is_blue_env::$IS_BLUE_ENV"
shell: bash
- name: Print if Environment is Blue
run: |
if [[ "${{ steps.check-blue.outputs.is_blue_env }}" == "true" ]]; then
echo "Environment is BLUE"
else
echo "Environment is NOT BLUE"
fi
shell: bash
build-docker-image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Set YML
run: |
mkdir -p lime-api/src/main/resources
echo "${{ secrets.APPLICATION_YML }}" | base64 --decode > lime-api/src/main/resources/application.yml
echo "${{ secrets.APPLICATION_API_YML }}" | base64 --decode > lime-api/src/main/resources/application-api.yml
find lime-api/src
mkdir -p lime-domain/src/main/resources
echo "${{ secrets.APPLICATION_DOMAIN_YML }}" | base64 --decode > lime-domain/src/main/resources/application-domain.yml
find lime-domain/src
mkdir -p lime-infrastructure/src/main/resources
echo "${{ secrets.APPLICATION_INFRA_YML }}" | base64 --decode > lime-infrastructure/src/main/resources/application-infra.yml
find lime-infrastructure/src
- name: Build with Gradle
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
with:
arguments: clean bootJar
- name: docker image build
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/lime-image .
- name: docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: docker Hub push
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/lime-image
run-docker-image-on-server-1:
needs: [ build-docker-image, set-variable ]
runs-on: [ self-hosted, lime-was-server-1 ]
outputs:
passed: ${{ steps.test_response.outputs.passed }}
steps:
- name: docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/lime-image
- name: Run Docker Container
run: |
PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }}
# 해당 포트에서 실행 중인 컨테이너 확인
RUNNING_CONTAINER=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$PORT->" | awk '{print $1}')
# 실행 중인 컨테이너가 있다면 중지
if [ ! -z "$RUNNING_CONTAINER" ]; then
echo "Stopping existing container on port $PORT..."
sudo docker stop $RUNNING_CONTAINER
fi
echo "Running new container on port $PORT..."
sudo docker run --rm -d -p $PORT:8080 ${{ secrets.DOCKERHUB_USERNAME }}/lime-image
- name: Test Application Response from /actuator
id: test_response
continue-on-error: true
run: |
PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }}
success=false
for i in {1..24}; do # 최대 1분(120초) 동안 5초 간격으로 반복
response=$(curl -s http://localhost:$PORT/actuator || true)
if [[ -z "$response" ]]; then
echo "No response yet. Waiting for 5 seconds..."
sleep 5
else
echo "Actuator response received: $response"
echo "::set-output name=passed::true"
success=true
break # 성공적으로 응답을 받으면 루프 종료
fi
done
if [[ $success == false ]]; then
echo "::set-output name=passed::false"
fi
- name: Stop TEST Docker Container If Test Failed
run: |
TEST_PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }}
STOP_PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.BLUE_PORT || secrets.GREEN_PORT }}
# 테스트 결과에 따라 컨테이너 중지
if [[ ${{ steps.test_response.outputs.passed }} != 'true' ]]; then # 테스트 성공 시
echo "Test failed. Not stopping container on port $STOP_PORT."
container_id=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$TEST_PORT->" | awk '{print $1}')
if [ ! -z "$container_id" ]; then # 테스트 실패 시 테스트 포트에 실행 중인 컨테이너가 있다면 중지
sudo docker stop $container_id
echo "Container on port $TEST_PORT stopped."
else
echo "No container found running on port $TEST_PORT"
fi
fi
run-docker-image-on-server-2:
needs: [ build-docker-image, set-variable ]
runs-on: [ self-hosted, lime-was-server-2 ]
outputs:
passed: ${{ steps.test_response.outputs.passed }}
steps:
- name: docker pull
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/lime-image
- name: Run Docker Container
run: |
PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }}
RUNNING_CONTAINER=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$PORT->" | awk '{print $1}')
# 실행 중인 컨테이너가 있다면 중지
if [ ! -z "$RUNNING_CONTAINER" ]; then
echo "Stopping existing container on port $PORT..."
sudo docker stop $RUNNING_CONTAINER
fi
echo "Running new container on port $PORT..."
sudo docker run --rm -d -p $PORT:8080 ${{ secrets.DOCKERHUB_USERNAME }}/lime-image
- name: Test Application Response from /actuator
id: test_response
continue-on-error: true
run: |
PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }}
success=false
echo "Testing application response from /actuator on port $PORT..."
for i in {1..24}; do # 최대 1분(120초) 동안 5초 간격으로 반복
echo "Attempt1 $i..."
response=$(curl -s http://localhost:$PORT/actuator || true)
echo "Attempt2 $i..."
if [[ -z "$response" ]]; then
echo "No response yet. Waiting for 5 seconds..."
sleep 5
else
echo "Actuator response received: $response"
echo "::set-output name=passed::true"
success=true
break # 성공적으로 응답을 받으면 루프 종료
fi
done
if [[ $success == false ]]; then
echo "::set-output name=passed::false"
fi
- name: Stop TEST Docker Container If Test Failed
run: |
TEST_PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.GREEN_PORT || secrets.BLUE_PORT }}
STOP_PORT=${{ needs.set-variable.outputs.is_blue_env == 'true' && secrets.BLUE_PORT || secrets.GREEN_PORT }}
# 테스트 결과에 따라 컨테이너 중지
if [[ ${{ steps.test_response.outputs.passed }} != 'true' ]]; then # 테스트 성공 시
echo "Test failed. Not stopping container on port $STOP_PORT."
container_id=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$TEST_PORT->" | awk '{print $1}')
if [ ! -z "$container_id" ]; then # 테스트 실패 시 테스트 포트에 실행 중인 컨테이너가 있다면 중지
sudo docker stop $container_id
echo "Container on port $TEST_PORT stopped."
else
echo "No container found running on port $TEST_PORT"
fi
fi
switch-environment:
needs: [ run-docker-image-on-server-1, run-docker-image-on-server-2]
runs-on: [ self-hosted, loadbalancer-server ]
outputs:
is_blue_env: ${{ steps.check-blue.outputs.is_blue_env }}
if: >
needs.run-docker-image-on-server-1.outputs.passed == 'true' &&
needs.run-docker-image-on-server-2.outputs.passed == 'true'
steps:
- name: Check Blue Environment
id: check-blue
run: |
IS_BLUE_ENV=$(sh /usr/local/bin/scripts/is_blue_env.sh)
echo "::set-output name=is_blue_env::$IS_BLUE_ENV"
shell: bash
- name: Switch to Green Environment
if: ${{ steps.check-blue.outputs.is_blue_env == 'true' }}
run: sh /usr/local/bin/scripts/switch_to_green_env.sh
- name: Switch to Blue Environment
if: ${{ steps.check-blue.outputs.is_blue_env != 'true' }}
run: sh /usr/local/bin/scripts/switch_to_blue_env.sh
stop-exist-container-in-was-1:
needs: [ run-docker-image-on-server-1, run-docker-image-on-server-2, switch-environment ]
runs-on: [ self-hosted, lime-was-server-1 ]
if: >
needs.run-docker-image-on-server-1.outputs.passed == 'true' &&
needs.run-docker-image-on-server-2.outputs.passed == 'true'
steps:
- name: Stop Existing Container in WAS-1
run: |
STOP_PORT=${{ needs.switch-environment.outputs.is_blue_env == 'true' && secrets.BLUE_PORT || secrets.GREEN_PORT }}
echo "Stopping Docker container on port $STOP_PORT..."
container_id=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$STOP_PORT->" | awk '{print $1}')
if [ ! -z "$container_id" ]; then # 테스트 성공 시 중지할 포트에 실행 중인 컨테이너가 있다면 중지
sudo docker stop $container_id
echo "Container on port $STOP_PORT stopped."
else
echo "No container found running on port $STOP_PORT"
fi
stop-exist-container-in-was-2:
needs: [ run-docker-image-on-server-1, run-docker-image-on-server-2, switch-environment ]
runs-on: [ self-hosted, lime-was-server-2 ]
if: >
needs.run-docker-image-on-server-1.outputs.passed == 'true' &&
needs.run-docker-image-on-server-2.outputs.passed == 'true'
steps:
- name: Stop Existing Container in WAS-2
run: |
STOP_PORT=${{ needs.switch-environment.outputs.is_blue_env == 'true' && secrets.BLUE_PORT || secrets.GREEN_PORT }}
echo "Stopping Docker container on port $STOP_PORT..."
container_id=$(sudo docker ps --filter "status=running" --format '{{.ID}} {{.Ports}}' | grep ":$STOP_PORT->" | awk '{print $1}')
if [ ! -z "$container_id" ]; then # 테스트 성공 시 중지할 포트에 실행 중인 컨테이너가 있다면 중지
sudo docker stop $container_id
echo "Container on port $STOP_PORT stopped."
else
echo "No container found running on port $STOP_PORT"
fi