-
Notifications
You must be signed in to change notification settings - Fork 1
278 lines (251 loc) · 10.1 KB
/
dev.kodemy.deploy.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
274
275
276
277
278
name: DEV - KodemyBackend deploy
on:
push:
branches: [ "dev-deploy" ]
paths-ignore:
- '.gitignore'
- 'docker-compose.deploy.yml'
- 'docker-compose.yml'
- 'README.md'
- 'config'
env:
WORKING_DIRECTORY: ${{ vars.WORKING_DIRECTORY }}
NETWORK_NAME: ${{ vars.NETWORK_NAME }}
DATASOURCE_CONTAINER: ${{ vars.DATASOURCE_CONTAINER }}
DATASOURCE_DB: ${{ vars.DATASOURCE_DB }}
RABBITMQ_HOST: ${{ vars.RABBITMQ_HOST }}
RABBITMQ_PORT: ${{ vars.RABBITMQ_PORT }}
ELASTICSEARCH_HOSTS: ${{ vars.RABBITMQ_PASSWORD }}
EUREKA_URL: ${{ vars.EUREKA_URL }}
FRONTEND_PUBLIC_HOST: ${{ vars.FRONTEND_PUBLIC_HOST }}
CORS_ALLOWED_ORIGINS: ${{ vars.CORS_ALLOWED_ORIGINS }}
GATEWAY_PUBLIC_HOST: ${{ vars.GATEWAY_PUBLIC_HOST }}
OAUTH_GITHUB_ID: ${{ vars.OAUTH_GITHUB_ID }}
REDIS_HOST: ${{ vars.REDIS_HOST }}
REDIS_PORT: ${{ vars.REDIS_PORT }}
DATASOURCE_USERNAME: ${{ secrets.DATASOURCE_USERNAME }}
DATASOURCE_PASSWORD: ${{ secrets.DATASOURCE_PASSWORD }}
RABBITMQ_USERNAME: ${{ secrets.RABBITMQ_USERNAME }}
RABBITMQ_PASSWORD: ${{ secrets.RABBITMQ_PASSWORD }}
ELASTICSEARCH_USERNAME: ${{ secrets.ELASTICSEARCH_USERNAME }}
ELASTICSEARCH_PASSWORD: ${{ secrets.ELASTICSEARCH_PASSWORD }}
OAUTH_GITHUB_KEY: ${{ secrets.OAUTH_GITHUB_KEY }}
JWT_KEY: ${{ secrets.JWT_KEY }}
jobs:
prepare:
name: Prepare
runs-on: self-hosted
environment: dev
steps:
- name: Check environments
run: |
REQUIRED_VARS=\
(
"WORKING_DIRECTORY"
"NETWORK_NAME"
"DATASOURCE_CONTAINER"
"DATASOURCE_USERNAME"
"DATASOURCE_PASSWORD"
"DATASOURCE_DB"
"RABBITMQ_HOST"
"RABBITMQ_PORT"
"RABBITMQ_USERNAME"
"RABBITMQ_PASSWORD"
"RABBITMQ_PASSWORD"
"ELASTICSEARCH_USERNAME"
"ELASTICSEARCH_PASSWORD"
"EUREKA_URL"
"FRONTEND_PUBLIC_HOST"
"CORS_ALLOWED_ORIGINS"
"GATEWAY_PUBLIC_HOST"
"OAUTH_GITHUB_ID"
"OAUTH_GITHUB_KEY"
"JWT_KEY"
"REDIS_HOST"
"REDIS_PORT"
)
EXIT=0
for VAR in "${REQUIRED_VARS[@]}"; do
[ -z "${!VAR}" ] && echo "Environment variable $VAR is missing." && EXIT=1
done
exit $EXIT
update-branch:
name: Update branch
needs: [ prepare ]
runs-on: self-hosted
environment: dev
steps:
- name: Update ${{ github.ref_name }}
uses: appleboy/ssh-action@master
env:
BRANCH: ${{ github.ref_name }}
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: WORKING_DIRECTORY,BRANCH
script: |
cd $WORKING_DIRECTORY
git checkout $BRANCH || exit 1
git pull origin $BRANCH || exit 1
validate:
name: Validate services
runs-on: self-hosted
needs: [ update-branch ]
environment: dev
steps:
- name: Validate services
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: WORKING_DIRECTORY,NETWORK_NAME,DATASOURCE_CONTAINER,DATASOURCE_USERNAME,DATASOURCE_DB
script: |
echo "Validate network"
docker network inspect $NETWORK_NAME > /dev/null 2>&1 \
|| { echo "Network $NETWORK_NAME is not available."; exit 1; }
echo "Validate database - container"
docker exec -t $DATASOURCE_CONTAINER pg_isready \
|| { echo "PostgreSQL is not running."; exit 1; }
echo "Validate database - database"
docker exec -t $DATASOURCE_CONTAINER psql -lqt -U $DATASOURCE_USERNAME | grep -q $DATASOURCE_DB \
|| { echo "Database $DATASOURCE_DB does not exist."; exit 1; }
echo "Validate database - schemas"
DATASOURCE_SCHEMAS=("kodemy-auth" "kodemy-backend" "kodemy-notification")
for SCHEMA in "${DATASOURCE_SCHEMAS[@]}"
do
docker exec -t $DATASOURCE_CONTAINER psql \
-U $DATASOURCE_USERNAME \
-d $DATASOURCE_DB \
-tAc "SELECT 1 FROM information_schema.schemata WHERE schema_name = '$SCHEMA'" \
| grep -q 1 || { echo "Schema $SCHEMA does not exist."; exit 1; }
done
build-and-deploy:
name: Build, Test, Deploy
runs-on: self-hosted
needs: [ validate ]
environment: dev
steps:
- name: Build, Test, Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: |
WORKING_DIRECTORY,
DATASOURCE_CONTAINER,DATASOURCE_USERNAME,DATASOURCE_PASSWORD,DATASOURCE_DB,
RABBITMQ_HOST,RABBITMQ_PORT,RABBITMQ_USERNAME,RABBITMQ_PASSWORD,
ELASTICSEARCH_HOSTS,ELASTICSEARCH_USERNAME,ELASTICSEARCH_PASSWORD,
EUREKA_URL,
FRONTEND_PUBLIC_HOST,CORS_ALLOWED_ORIGINS,
GATEWAY_PUBLIC_HOST,
OAUTH_GITHUB_ID,OAUTH_GITHUB_KEY,
JWT_KEY,
REDIS_HOST,REDIS_PORT
script: |
cd $WORKING_DIRECTORY/commons
./gradlew clean assemble
cd $WORKING_DIRECTORY/kodemy-api-gateway
./gradlew clean assemble -x test
cd $WORKING_DIRECTORY/kodemy-auth
./gradlew clean assemble
cd $WORKING_DIRECTORY/kodemy-backend
./gradlew clean assemble -x test
cd $WORKING_DIRECTORY/kodemy-notification
./gradlew clean assemble -x test
cd $WORKING_DIRECTORY/kodemy-search
./gradlew clean assemble -x test
cd $WORKING_DIRECTORY/kodemy-service-registry
./gradlew clean assemble -x test
export DATASOURCE_URL=jdbc:postgresql://$DATASOURCE_CONTAINER/$DATASOURCE_DB
cd $WORKING_DIRECTORY
docker compose -f docker-compose.app.yml -f docker-compose.app.expose.yml up --build -d
export BACKEND_CONTAINERS=$(docker-compose -f docker-compose.app.yml ps -q)
- name: Validate running
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: |
WORKING_DIRECTORY,
DATASOURCE_CONTAINER,DATASOURCE_USERNAME,DATASOURCE_PASSWORD,DATASOURCE_DB,
RABBITMQ_HOST,RABBITMQ_PORT,RABBITMQ_USERNAME,RABBITMQ_PASSWORD,
ELASTICSEARCH_HOSTS,ELASTICSEARCH_USERNAME,ELASTICSEARCH_PASSWORD,
EUREKA_URL,
FRONTEND_PUBLIC_HOST,CORS_ALLOWED_ORIGINS,
GATEWAY_PUBLIC_HOST,
OAUTH_GITHUB_ID,OAUTH_GITHUB_KEY,
JWT_KEY,
REDIS_HOST,REDIS_PORT
script: |
cd $WORKING_DIRECTORY
echo "Waiting 1 minute..."
sleep 1m
for container in $BACKEND_CONTAINERS; do
status=$(docker inspect --format='{{.State.Status}}' $container)
if [ "$status" != "running" ]; then
echo "$container not running"
docker compose -f docker-compose.app.yml -f docker-compose.app.expose.yml -f docker-compose.backup.yml up -d
exit 1
fi
done
echo "All containers validated successfully."
docker commit kodemybackend-kodemy-service-registry-1 kodemy-service-registry:backup
docker commit kodemybackend-kodemy-api-gateway-1 kodemy-api-gateway:backup
docker commit kodemybackend-kodemy-auth-1 kodemy-auth:backup
docker commit kodemybackend-kodemy-backend-1 kodemy-backend:backup
docker commit kodemybackend-kodemy-notification-1 kodemy-notification:backup
docker commit kodemybackend-kodemy-search-1 kodemy-search:backup
- name: Cleanup
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
docker system prune -f
create-tag:
name: Create tag
runs-on: self-hosted
environment: dev
needs: [ build-and-deploy ]
steps:
- name: Create tag
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.REMOTE_HOST }}
username: ${{ secrets.REMOTE_USER }}
port: ${{ secrets.REMOTE_PORT }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: |
WORKING_DIRECTORY
script: |
cd $WORKING_DIRECTORY
git tag -l | xargs git tag -d
git fetch --tags
tags=$(git tag -l)
latest_tag=$(echo "$tags" | grep -E '^v?0\.[0-9]+\.[0-9]+(-.*)?$' | sort -V | tail -n 1)
if [ -n "$latest_tag" ]; then
if [[ "$latest_tag" == v* ]]; then
latest_tag="${latest_tag#v}"
fi
base_version=$(echo "$latest_tag" | sed 's/-.*//')
major=$(echo "$base_version" | cut -d'.' -f1)
minor=$(echo "$base_version" | cut -d'.' -f2)
patch=$(echo "$base_version" | cut -d'.' -f3)
new_patch=$((patch + 1))
new_tag="v${major}.${minor}.${new_patch}-dev"
else
new_tag="0.0.1"
fi
echo "New tag: $new_tag"
git tag $new_tag
git push origin --tags