-
Notifications
You must be signed in to change notification settings - Fork 1
200 lines (174 loc) · 6.78 KB
/
production_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
name: production(cicd)
on:
push:
tags:
- v*.*.*
jobs:
ci:
runs-on: ubuntu-latest
environment: PRODUCTION
strategy:
matrix:
java-version: [ 17 ]
outputs:
VERSION: ${{ steps.get_version.outputs.VERSION }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: ${{ matrix.java-version }}
distribution: 'adopt'
- name: Cache SonarCloud packages
uses: actions/cache@v3
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
- name: spotless check
run: ./gradlew spotlessCheck --no-daemon
- name: Start containers
run: docker compose -f docker-compose-test.yml up -d
- name: test and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: ./gradlew test sonar --info --stacktrace --no-daemon
- name: build
run: ./gradlew clean build -x test
- name: get project version
id: get_version
run: |
VERSION="$(cut -d'v' -f2 <<< ${GITHUB_REF#refs/*/})"
echo ::set-output name=VERSION::$VERSION
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build And Push
uses: docker/build-push-action@v3
with:
context: ./TodaysFail-Interface
push: true
tags: ${{secrets.AWS_ECR_REGISTRY_ID}}.dkr.ecr.${{secrets.AWS_REGION}}.amazonaws.com/${{secrets.AWS_ECR_REGISTRY_NAME}}:${{ steps.get_version.outputs.VERSION }}
cd:
runs-on: ubuntu-latest
needs: ci
environment: PRODUCTION
steps:
- name: checkout
uses: actions/checkout@master
- name: create remote directory
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: root
key: ${{ secrets.KEY }}
port: ${{ secrets.PORT }}
script: mkdir -p ~/TodaysFail-Backend
- name: copy source via ssh key
uses: burnett01/[email protected]
with:
switches: -avzr
remote_path: ~/TodaysFail-Backend
remote_host: ${{ secrets.HOST }}
remote_port: ${{ secrets.PORT }}
remote_user: root
remote_key: ${{ secrets.KEY }}
- name: connect ssh
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: root
key: ${{ secrets.KEY }}
port: ${{ secrets.PORT }}
script: |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_REGISTRY_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com
cd ~/TodaysFail-Backend
VERSION=${{ needs.ci.outputs.VERSION }}
sed -i "s/VERSION=.*/VERSION=$VERSION/" .env
EXIST_BLUE=$(docker-compose -f ~/TodaysFail-Backend/docker-compose-blue.yml ps | grep todaysfail-blue | awk '{print $1}')
if [ -z "${EXIST_BLUE}" ]
then
# GREEN이 실행중인 경우
START_CONTAINER=blue
START_PORT=8080
STOP_CONTAINER=green
STOP_PORT=8081
sed -i "s/API_SERVER_PORT=.*/API_SERVER_PORT=8080/" .env
else
# BLUE가 실행중인 경우
START_CONTAINER=green
START_PORT=8081
STOP_CONTAINER=blue
STOP_PORT=8080
sed -i "s/API_SERVER_PORT=.*/API_SERVER_PORT=8081/" .env
fi
docker-compose -f ~/TodaysFail-Backend/docker-compose-$START_CONTAINER.yml pull
docker-compose -f ~/TodaysFail-Backend/docker-compose-$START_CONTAINER.yml up --build -d
docker system prune --all -f
for cnt in {1..10}
do
DEPLOY_STATUS=$(curl -s http://localhost:$START_PORT/api/v1/examples/health | grep \"status\":200)
if [ -z "${DEPLOY_STATUS}" ]
then
echo "Deploying..."
else
break
fi
sleep 5
done
if [ $cnt -eq 10 ]
then
echo "Deploy Failed"
exit 1
fi
sed -i "s/$STOP_PORT/$START_PORT/" ~/../etc/nginx/conf.d/service_url.inc
service nginx reload
docker-compose -f ~/TodaysFail-Backend/docker-compose-$STOP_CONTAINER.yml down
- name: Notify on success
if: ${{ success() }}
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }}
SLACK_USERNAME: '오늘의 실패'
SLACK_ICON: https://avatars.slack-edge.com/2023-07-26/5653063434977_51d6e36ad6977fe04337_192.png
SLACK_COLOR: '#008000'
SLACK_MESSAGE: |
*[PROD] 배포 성공 🎉*
*배포 버전*
${{ needs.ci.outputs.VERSION }}
*배포 내역 보기*
https://github.com/TodaysFail/TodaysFail-Backend/releases/tag/v${{ needs.ci.outputs.VERSION }}
- name: Notify on failure
if: ${{ failure() }}
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }}
SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }}
SLACK_USERNAME: '오늘의 실패'
SLACK_ICON: https://avatars.slack-edge.com/2023-07-26/5653063434977_51d6e36ad6977fe04337_192.png
SLACK_COLOR: '#FF0000'
SLACK_MESSAGE: |
*[PROD] 배포 실패 😥*
*배포 버전*
${{ needs.ci.outputs.VERSION }}
*배포 내역 보기*
https://github.com/TodaysFail/TodaysFail-Backend/releases/tag/v${{ needs.ci.outputs.VERSION }}