-
Notifications
You must be signed in to change notification settings - Fork 0
359 lines (316 loc) · 15.7 KB
/
build-n-release.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
name: build-n-release
on:
workflow_dispatch:
inputs:
build_frontend:
description: 'Build frontend image'
required: true
default: 'false'
type: boolean
build_backend:
description: 'Build backend image'
required: true
default: 'false'
type: boolean
deploy_frontend:
description: 'Deploy frontend image'
required: true
default: 'false'
type: boolean
deploy_backend:
description: 'Deploy backend image'
required: true
default: 'false'
type: boolean
deploy_postgres:
description: 'Deploy postgres'
required: true
default: 'false'
type: boolean
deploy_nginx:
description: 'Deploy nginx'
required: true
default: 'false'
type: boolean
frontend_version:
description: 'Frontend version to deploy (optional): eg: v1.0.0'
required: false
type: string
backend_version:
description: 'Backend version to deploy (optional): eg: v1.0.0'
required: false
type: string
permissions:
contents: write
packages: write
attestations: write
id-token: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
clean: true
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Read current versions from VERSION.txt
id: read_versions
run: |
FRONTEND_VERSION=$(grep "frontend=" VERSION.txt | cut -d '=' -f 2)
BACKEND_VERSION=$(grep "backend=" VERSION.txt | cut -d '=' -f 2)
echo "FRONTEND_VERSION=$FRONTEND_VERSION" >> $GITHUB_ENV
echo "BACKEND_VERSION=$BACKEND_VERSION" >> $GITHUB_ENV
- name: Increment frontend version
if: ${{ github.event.inputs.build_frontend == 'true' }}
run: |
FRONTEND_VERSION_NO_V=$(echo ${{ env.FRONTEND_VERSION }} | sed 's/^v//') # Remove leading 'v' if present
FRONTEND_NEW_VERSION="v$(echo $FRONTEND_VERSION_NO_V | cut -d'.' -f1).$(echo $FRONTEND_VERSION_NO_V | cut -d'.' -f2).$(($(echo $FRONTEND_VERSION_NO_V | cut -d'.' -f3) + 1))"
echo "New frontend version: $FRONTEND_NEW_VERSION"
echo "FRONTEND_NEW_VERSION=$FRONTEND_NEW_VERSION" >> $GITHUB_ENV
- name: Increment backend version
if: ${{ github.event.inputs.build_backend == 'true' }}
run: |
BACKEND_VERSION_NO_V=$(echo ${{ env.BACKEND_VERSION }} | sed 's/^v//') # Remove leading 'v' if present
BACKEND_NEW_VERSION="v$(echo $BACKEND_VERSION_NO_V | cut -d'.' -f1).$(echo $BACKEND_VERSION_NO_V | cut -d'.' -f2).$(($(echo $BACKEND_VERSION_NO_V | cut -d'.' -f3) + 1))"
echo "New backend version: $BACKEND_NEW_VERSION"
echo "BACKEND_NEW_VERSION=$BACKEND_NEW_VERSION" >> $GITHUB_ENV
# Manually build and push frontend image
- name: Build and push frontend image
if: ${{ github.event.inputs.build_frontend == 'true' }}
id: push_frontend
run: |
docker build -t ghcr.io/karthi209/ones-and-zeros/frontend:${{ env.FRONTEND_NEW_VERSION }} -f Dockerfile .
docker push ghcr.io/karthi209/ones-and-zeros/frontend:${{ env.FRONTEND_NEW_VERSION }}
# Manually build and push backend image
- name: Build and push backend image
if: ${{ github.event.inputs.build_backend == 'true' }}
id: push_backend
run: |
cd backend/
docker build -t ghcr.io/karthi209/ones-and-zeros/backend:${{ env.BACKEND_NEW_VERSION }} -f Dockerfile .
docker push ghcr.io/karthi209/ones-and-zeros/backend:${{ env.BACKEND_NEW_VERSION }}
# Only update the version if the push succeeded
- name: Update frontend version in VERSION.txt
if: ${{ github.event.inputs.build_frontend == 'true' && steps.push_frontend.outcome == 'success' }}
run: |
sed -i "s/frontend=.*/frontend=${{ env.FRONTEND_NEW_VERSION }}/" VERSION.txt
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
git add VERSION.txt
git commit -m "Update frontend version"
git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} HEAD:${{ github.ref }}
- name: Update backend version in VERSION.txt
if: ${{ github.event.inputs.build_backend == 'true' && steps.push_backend.outcome == 'success' }}
run: |
sed -i "s/backend=.*/backend=${{ env.BACKEND_NEW_VERSION }}/" VERSION.txt
git config --global user.name "github-actions"
git config --global user.email "github-actions@github.com"
git add VERSION.txt
git commit -m "Update backend version"
git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} HEAD:${{ github.ref }}
deploy_frontend:
if: ${{ github.event.inputs.deploy_frontend == 'true' }}
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
clean: true
- name: Pull latest changes from repository
run: git pull origin HEAD
- name: Read frontend version from VERSION.txt
run: |
FRONTEND_VERSION=$(grep "frontend=" VERSION.txt | cut -d '=' -f 2)
echo "FRONTEND_VERSION=$FRONTEND_VERSION" >> $GITHUB_ENV
- name: Use frontend version from input or VERSION.txt
run: |
if [ -z "${{ github.event.inputs.frontend_version }}" ]; then
echo "Using frontend version from VERSION.txt: ${{ env.FRONTEND_VERSION }}"
FRONTEND_VERSION_TO_DEPLOY="${{ env.FRONTEND_VERSION }}"
else
echo "Using frontend version from input: ${{ github.event.inputs.frontend_version }}"
FRONTEND_VERSION_TO_DEPLOY="${{ github.event.inputs.frontend_version }}"
fi
echo "FRONTEND_VERSION_TO_DEPLOY=$FRONTEND_VERSION_TO_DEPLOY" >> $GITHUB_ENV
- name: Deploy frontend Docker container to production server using SSH with password
run: |
sshpass -p "${{ secrets.SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_IP }} << 'EOF'
docker stop frontend-onesandzeros || true
docker rm frontend-onesandzeros || true
docker rmi $(docker images ghcr.io/karthi209/ones-and-zeros/frontend -q) || true
docker network inspect network-onesandzeros > /dev/null 2>&1 || docker network create network-onesandzeros
docker pull ghcr.io/karthi209/ones-and-zeros/frontend:${{ env.FRONTEND_VERSION_TO_DEPLOY }}
docker run -d --name frontend-onesandzeros -p 5173:5173 --network network-onesandzeros ghcr.io/karthi209/ones-and-zeros/frontend:${{ env.FRONTEND_VERSION_TO_DEPLOY }}
docker update --restart unless-stopped frontend-onesandzeros
EOF
- name: Check if frontend container is running
run: |
echo "Checking if frontend container is running on remote server..."
TIMEOUT=120 # 2 minutes in seconds
START_TIME=$(date +%s)
until sshpass -p "${{ secrets.SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_IP }} "docker ps --filter 'name=frontend-onesandzeros' --filter 'status=running' --format '{{.Names}}' | grep -w 'frontend-onesandzeros'"; do
echo "Container not found or not running yet. Retrying in 5 seconds..."
sleep 5
CURRENT_TIME=$(date +%s)
ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
echo "Frontend container did not start within 2 minutes. Exiting."
exit 1 # Exit with an error code
fi
done
echo "frontend-onesandzeros container is running!"
deploy_backend:
if: ${{ github.event.inputs.deploy_backend == 'true' }}
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
clean: true
- name: Pull latest changes from repository
run: git pull origin HEAD
- name: Read backend version from VERSION.txt
run: |
BACKEND_VERSION=$(grep "backend=" VERSION.txt | cut -d '=' -f 2)
echo "BACKEND_VERSION=$BACKEND_VERSION" >> $GITHUB_ENV
- name: Use backend version from input or VERSION.txt
run: |
if [ -z "${{ github.event.inputs.backend_version }}" ]; then
echo "Using backend version from VERSION.txt: ${{ env.BACKEND_VERSION }}"
BACKEND_VERSION_TO_DEPLOY="${{ env.BACKEND_VERSION }}"
else
echo "Using backend version from input: ${{ github.event.inputs.backend_version }}"
BACKEND_VERSION_TO_DEPLOY="${{ github.event.inputs.backend_version }}"
fi
echo "BACKEND_VERSION_TO_DEPLOY=$BACKEND_VERSION_TO_DEPLOY" >> $GITHUB_ENV
- name: Deploy backend Docker container to production server using SSH with password
run: |
sshpass -p "${{ secrets.SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_IP }} << 'EOF'
docker stop backend-onesandzeros || true
docker rm backend-onesandzeros || true
docker rmi $(docker images ghcr.io/karthi209/ones-and-zeros/backend -q) || true
docker network inspect network-onesandzeros > /dev/null 2>&1 || docker network create network-onesandzeros
docker pull ghcr.io/karthi209/ones-and-zeros/backend:${{ env.BACKEND_VERSION_TO_DEPLOY }}
docker run -d \
--name backend-onesandzeros \
--network network-onesandzeros \
-p 3000:3000 \
-v /home/karthi209/webfiles:/app/webfiles \
-e DB_USER="${{ secrets.DB_USER }}" \
-e DB_PASSWORD="${{ secrets.DB_PASSWORD }}" \
-e DB_HOST="${{ secrets.DB_HOST }}" \
-e DB_PORT="${{ secrets.DB_PORT }}" \
-e DB_NAME="${{ secrets.DB_NAME }}" \
ghcr.io/karthi209/ones-and-zeros/backend:${{ env.BACKEND_VERSION_TO_DEPLOY }}
docker update --restart unless-stopped backend-onesandzeros
EOF
- name: Check if backend container is running
run: |
echo "Checking if backend container is running on remote server..."
TIMEOUT=120 # 2 minutes in seconds
START_TIME=$(date +%s)
until sshpass -p "${{ secrets.SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_IP }} "docker ps --filter 'name=backend-onesandzeros' --filter 'status=running' --format '{{.Names}}' | grep -w 'backend-onesandzeros'"; do
echo "Container not found or not running yet. Retrying in 5 seconds..."
sleep 5
CURRENT_TIME=$(date +%s)
ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
echo "Backend container did not start within 2 minutes. Exiting."
exit 1 # Exit with an error code
fi
done
echo "Backend container is running!"
deploy_postgres:
if: ${{ github.event.inputs.deploy_postgres == 'true' }}
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
clean: true
- name: Deploy postgres
run: |
sshpass -p "${{ secrets.SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_IP }} << 'EOF'
docker stop postgres-onesandzeros || true
docker rm postgres-onesandzeros || true
docker rmi $(docker images postgres -q) || true
docker network inspect network-onesandzeros > /dev/null 2>&1 || docker network create network-onesandzeros
docker pull postgres:latest
docker run -d --name postgres-onesandzeros \
-e POSTGRES_USER="${{ secrets.DB_USER }}" \
-e POSTGRES_PASSWORD="${{ secrets.DB_PASSWORD }}" \
-e POSTGRES_DB="${{ secrets.DB_NAME }}" \
-v /home/karthi209/postgres_data:/var/lib/postgresql/data \
--network network-onesandzeros \
-p 5432:5432 \
postgres:latest
docker update --restart unless-stopped postgres-onesandzeros
EOF
- name: Check if postgres container is running
run: |
echo "Checking if postgres container is running on remote server..."
TIMEOUT=120 # 2 minutes in seconds
START_TIME=$(date +%s)
until sshpass -p "${{ secrets.SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_IP }} "docker ps --filter 'name=postgres-onesandzeros' --filter 'status=running' --format '{{.Names}}' | grep -w 'postgres-onesandzeros'"; do
echo "Container not found or not running yet. Retrying in 5 seconds..."
sleep 5
CURRENT_TIME=$(date +%s)
ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
echo "Postgres container did not start within 2 minutes. Exiting."
exit 1 # Exit with an error code
fi
done
echo "postgres-onesandzeros container is running!"
deploy_nginx:
if: ${{ github.event.inputs.deploy_nginx == 'true' }}
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
clean: true
- name: Deploy nginx
run: |
sshpass -p "${{ secrets.SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_IP }} << 'EOF'
docker stop nginx-onesandzeros || true
docker rm nginx-onesandzeros || true
docker rmi $(docker images nginx -q) || true
docker network inspect network-onesandzeros > /dev/null 2>&1 || docker network create network-onesandzeros
docker pull nginx
docker run -d --name nginx-onesandzeros \
-v /home/karthi209/nginx_conf/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /etc/letsencrypt:/etc/letsencrypt:ro \
-p 80:80 \
-p 443:443 \
--network network-onesandzeros \
--link postgres-onesandzeros:postgres-onesandzeros \
nginx:latest
docker update --restart unless-stopped nginx-onesandzeros
EOF
- name: Check if nginx container is running
run: |
echo "Checking if nginx container is running on remote server..."
TIMEOUT=120 # 2 minutes in seconds
START_TIME=$(date +%s)
until sshpass -p "${{ secrets.SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_IP }} "docker ps --filter 'name=nginx-onesandzeros' --filter 'status=running' --format '{{.Names}}' | grep -w 'nginx-onesandzeros'"; do
echo "Container not found or not running yet. Retrying in 5 seconds..."
sleep 5
CURRENT_TIME=$(date +%s)
ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
if [ $ELAPSED_TIME -ge $TIMEOUT ]; then
echo "Nginx container did not start within 2 minutes. Exiting."
exit 1 # Exit with an error code
fi
done
echo "nginx-onesandzeros container is running!"