-
Notifications
You must be signed in to change notification settings - Fork 3
382 lines (327 loc) · 12.8 KB
/
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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
name: 🚀 Deploy
on:
push:
branches:
- main
tags:
- "*"
pull_request:
types: [opened, synchronize, reopened]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions:
actions: write
contents: read
jobs:
lint:
name: ⬣ ESLint
runs-on: ubuntu-latest
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v4
- name: ⎔ Setup node
uses: actions/setup-node@v4
with:
node-version: 20
- name: 📥 Download deps
uses: bahmutov/npm-install@v1
- name: 🔬 Lint
run: npm run lint
typecheck:
name: ʦ TypeScript
runs-on: ubuntu-latest
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v4
- name: ⎔ Setup node
uses: actions/setup-node@v4
with:
node-version: 20
- name: 📥 Download deps
uses: bahmutov/npm-install@v1
- name: 🔎 Type check
run: npm run typecheck --if-present
vitest:
name: ⚡ Vitest
runs-on: ubuntu-latest
env:
DATABASE_URL: postgresql://lab_user:lab_pass@localhost/lab_test
SESSION_SECRET: asdf_secret
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v4
- name: ⎔ Setup node
uses: actions/setup-node@v4
with:
node-version: 20
- name: 📥 Download deps
uses: bahmutov/npm-install@v1
- uses: harmon758/postgresql-action@v1
with:
postgresql version: "12"
postgresql db: lab_test
postgresql user: lab_user
postgresql password: lab_pass
- name: 🛠 check memory
run: cat /proc/meminfo
- name: 🛠 Setup Database
run: npx prisma migrate reset --force
- name: 🛠 check memory
run: cat /proc/meminfo
- name: ⚡ Run vitest
run: npm run test --coverage
playwright:
name: ⚫️ Playwright
timeout-minutes: 60
runs-on: ubuntu-latest
env:
DATABASE_URL: postgresql://lab_user:lab_pass@localhost/lab_test
SESSION_SECRET: asdf_secret
BASE_URL: http://localhost:3000
AUTH0_CLIENT_ID: ${{ secrets.AUTH0_CLIENT_ID }}
AUTH0_DOMAIN: ${{ secrets.AUTH0_DOMAIN }}
AUTH0_CLIENT_SECRET: ${{ secrets.AUTH0_CLIENT_SECRET }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: 🏄 Copy test env vars
run: cp .env.example .env
- uses: harmon758/postgresql-action@v1
with:
postgresql version: "12"
postgresql db: lab_test
postgresql user: lab_user
postgresql password: lab_pass
- name: 📥 Download deps
uses: bahmutov/npm-install@v1
- name: 🛠 Setup Database
run: npx prisma migrate reset --force
- name: create admin user
run: echo "ADMIN_COOKIE=$(npx ts-node --require tsconfig-paths/register ./cypress/support/create-user.ts 'test@example.com' ADMIN)" >> $GITHUB_ENV
- name: Install Playwright
run: npx playwright install --with-deps
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
sonarqube:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
# Disabling shallow clone is recommended for improving relevancy of reporting
fetch-depth: 0
- name: SonarQube Scan
uses: sonarsource/sonarqube-scan-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST }}
deploy:
name: 🚀 Deploy
runs-on: ubuntu-latest
environment: dev
needs: [lint, typecheck, vitest, sonarqube]
# only build/deploy main branch on pushes
if: ${{ (github.ref == 'refs/heads/main') && github.event_name == 'push' }}
env:
NODE_ENV: production
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v4
- name: 👀 Read app props
uses: Muchaszewski/read-json-action@1.0.0
id: app_props
with:
path: "package.json"
properties: "[name, engines.node]"
- name: Prepare Server For Build
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.LIGHTSAIL_IP }}
username: ${{ secrets.LIGHTSAIL_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
rm -rf ~/projectlab/tmp
mkdir -p ~/projectlab/tmp
- name: Copy Files To Lightsail Instance
uses: wlixcc/SFTP-Deploy-Action@v1.0
with:
username: ${{ secrets.LIGHTSAIL_USER }}
server: ${{ secrets.LIGHTSAIL_IP }}
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
local_path: "./*"
remote_path: "/home/admin/projectlab/tmp/"
args: "-o ConnectTimeout=60"
- name: Configure Server
uses: appleboy/ssh-action@master
env:
DB_URL: ${{ secrets.DB_URL }}
DATABASE_URL: postgresql://admin:password@localhost/projectlab
SESSION_SECRET: ${{ secrets.SESSION_SECRET }}
BASE_URL: ${{ secrets.BASE_URL }}
AUTH0_CLIENT_ID: ${{ secrets.AUTH0_CLIENT_ID }}
AUTH0_DOMAIN: ${{ secrets.AUTH0_DOMAIN }}
AUTH0_CLIENT_SECRET: ${{ secrets.AUTH0_CLIENT_SECRET }}
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
GITHUB_KEY: ${{ secrets.API_GITHUB_KEY }}
DEV_URL: ${{ secrets.DEV_URL }}
with:
timeout: 60s
command_timeout: 30m
envs: DB_URL,DATABASE_URL,SESSION_SECRET,BASE_URL,AUTH0_CLIENT_ID,AUTH0_DOMAIN,AUTH0_CLIENT_SECRET,GOOGLE_APPLICATION_CREDENTIALS,GITHUB_KEY,DEV_URL
host: ${{ secrets.LIGHTSAIL_IP }}
username: ${{ secrets.LIGHTSAIL_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: true
script: |
# ignore failure on pm2 stop (first time?)
pm2 stop all || true
echo "*** Step: *** Replace app folders"
rm -rf ~/projectlab/app
mkdir -p ~/projectlab/app
cp -R ~/projectlab/tmp/. ~/projectlab/app/
cd ~/projectlab/app
echo "*** Step: *** Create .env file"
echo DATABASE_URL="$DATABASE_URL" >> .env
echo SESSION_SECRET="$SESSION_SECRET" >> .env
echo BASE_URL="$BASE_URL" >> .env
echo AUTH0_CLIENT_ID="$AUTH0_CLIENT_ID" >> .env
echo AUTH0_DOMAIN="$AUTH0_DOMAIN" >> .env
echo AUTH0_CLIENT_SECRET="$AUTH0_CLIENT_SECRET" >> .env
echo GOOGLE_APPLICATION_CREDENTIALS="$GOOGLE_APPLICATION_CREDENTIALS" >> .env
echo GITHUB_KEY="$GITHUB_KEY" >> .env
echo DEV_URL="$DEV_URL" >> .env
echo "*** Step: *** npm ci and build"
npm ci # install
npm run build
echo "*** Step: *** Load prod database and run migrations and seeds"
pg_dump --dbname $DB_URL --clean --if-exists > db.sql
sudo -u postgres psql -c "DROP DATABASE IF EXISTS projectlab;"
sudo -u postgres psql -c "CREATE DATABASE projectlab;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE projectlab TO admin;"
psql -d "$DATABASE_URL" < db.sql
npx -y prisma migrate deploy # first npx with -y to avoid prompt to install
npx prisma db seed
echo "*** Step: *** Schedule cron to update profiles"
chmod +x /home/admin/projectlab/app/profilesmigration.sh
echo "0 13 * * * /home/admin/projectlab/app/profilesmigration.sh" | crontab -
echo "*** Cron job to get github activity ****"
chmod +x /home/admin/projectlab/app/githubActivity.sh
{
line="0 0 * * * /home/admin/projectlab/app/githubActivity.sh"
crontab -l | grep -Fv "$line"
echo "$line"
} | crontab -
echo "*** Step: *** Start pm2 service"
pm2 start ecosystem.config.js
pm2 save
# echo "*** Step: ** Run Playwright tests"
# echo "ADMIN_COOKIE=$(npx ts-node --require tsconfig-paths/register ./cypress/support/create-user.ts 'test@example.com' ADMIN)" >> .env
# npx playwright install --with-deps
# npx playwright test
echo "*** Step: ** Cleanup"
rm -rf ~/projectlab/tmp
deploy-prod:
name: 🚀 Deploy Prod
runs-on: ubuntu-latest
environment: prod
needs: [lint, typecheck, vitest, sonarqube, playwright]
# only build/deploy prod on new version tags (v1.0.0)
if: startsWith(github.ref, 'refs/tags/v')
env:
NODE_ENV: production
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v4
- name: 👀 Read app props
uses: Muchaszewski/read-json-action@1.0.0
id: app_props
with:
path: "package.json"
properties: "[name, engines.node]"
- name: Prepare Server For Build
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.LIGHTSAIL_IP }}
username: ${{ secrets.LIGHTSAIL_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
rm -rf ~/projectlab/tmp
mkdir -p ~/projectlab/tmp
- name: Copy Files To Lightsail Instance
uses: wlixcc/SFTP-Deploy-Action@v1.0
with:
username: ${{ secrets.LIGHTSAIL_USER }}
server: ${{ secrets.LIGHTSAIL_IP }}
ssh_private_key: ${{ secrets.SSH_PRIVATE_KEY }}
local_path: "./*"
remote_path: "/home/admin/projectlab/tmp/"
args: "-o ConnectTimeout=60"
- name: Configure Server
uses: appleboy/ssh-action@master
env:
DB_URL: ${{ secrets.DB_URL }}
# On prod DATABASE_URL = DB_URL
DATABASE_URL: ${{ secrets.DB_URL }}
SESSION_SECRET: ${{ secrets.SESSION_SECRET }}
BASE_URL: ${{ secrets.BASE_URL }}
AUTH0_CLIENT_ID: ${{ secrets.AUTH0_CLIENT_ID }}
AUTH0_DOMAIN: ${{ secrets.AUTH0_DOMAIN }}
AUTH0_CLIENT_SECRET: ${{ secrets.AUTH0_CLIENT_SECRET }}
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
GITHUB_KEY: ${{ secrets.API_GITHUB_KEY }}
with:
timeout: 60s
command_timeout: 30m
envs: DB_URL,DATABASE_URL,SESSION_SECRET,BASE_URL,AUTH0_CLIENT_ID,AUTH0_DOMAIN,AUTH0_CLIENT_SECRET,GOOGLE_APPLICATION_CREDENTIALS,GITHUB_KEY
host: ${{ secrets.LIGHTSAIL_IP }}
username: ${{ secrets.LIGHTSAIL_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script_stop: true
script: |
# ignore failure on pm2 delete (first time?)
# use delete to also reload .env variables
pm2 delete projectlab || true
echo "*** Step: *** Replace app folders"
rm -rf ~/projectlab/app
mkdir -p ~/projectlab/app
cp -R ~/projectlab/tmp/. ~/projectlab/app/
cd ~/projectlab/app
echo "*** Step: *** Create .env file"
echo DATABASE_URL="$DATABASE_URL" >> .env
echo SESSION_SECRET="$SESSION_SECRET" >> .env
echo BASE_URL="$BASE_URL" >> .env
echo AUTH0_CLIENT_ID="$AUTH0_CLIENT_ID" >> .env
echo AUTH0_DOMAIN="$AUTH0_DOMAIN" >> .env
echo AUTH0_CLIENT_SECRET="$AUTH0_CLIENT_SECRET" >> .env
echo GOOGLE_APPLICATION_CREDENTIALS="$GOOGLE_APPLICATION_CREDENTIALS" >> .env
echo GITHUB_KEY="$GITHUB_KEY" >> .env
echo "*** Step: *** npm ci and build"
npm ci # install
npm run build
echo "*** Step: *** Load prod database and run migrations and seeds"
pg_dump --dbname $DB_URL --clean --if-exists > db.sql ## Just for a quick backup
npx -y prisma migrate deploy # first npx with -y to avoid prompt to install
npx prisma db seed
echo "*** Step: *** Schedule cron to update profiles"
chmod +x /home/admin/projectlab/app/profilesmigration.sh
echo "0 13 * * * /home/admin/projectlab/app/profilesmigration.sh" | crontab -
echo "*** Cron job to get github activity ****"
chmod +x /home/admin/projectlab/app/githubActivity.sh
{
line="0 9,21 * * * /home/admin/projectlab/app/githubActivity.sh"
crontab -l | grep -Fv "$line"
echo "$line"
} | crontab -
echo "*** Step: *** Start pm2 service"
pm2 start ecosystem.config.js
pm2 save
echo "*** Step: ** Cleanup"
rm -rf ~/projectlab/tmp