Skip to content

Commit e0a9d1a

Browse files
authored
Merge pull request #599 from dzcode-io/lighthouse-setup
feat: Lighthouse setup
2 parents 2df56c4 + a10d451 commit e0a9d1a

File tree

12 files changed

+248
-47
lines changed

12 files changed

+248
-47
lines changed

.github/workflows/cd.deploy.stg.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ on:
44
push:
55
tags:
66
- "stg-v*"
7+
# @TODO-ZM: remove this
8+
branches:
9+
- "lighthouse-setup"
710
workflow_dispatch:
811

912
jobs:
@@ -50,3 +53,14 @@ jobs:
5053
run: npm run generate:sentry-release $DEPLOY_VERSION stage ${{ secrets.SENTRY_AUTH_TOKEN }}
5154
- name: "Deploy"
5255
run: npm run deploy:stg
56+
57+
lighthouse:
58+
needs: bundle-deploy
59+
uses: ./.github/workflows/ci.reusable.lighthouse.yml
60+
with:
61+
serverBaseUrl: "https://lh-stage.dzcode.io"
62+
testBaseUrl: "https://stage.dzcode.io"
63+
stage: "staging"
64+
secrets:
65+
LH_SERVER_TOKEN_STG: ${{ secrets.LH_SERVER_TOKEN_STG }}
66+
LH_SERVER_TOKEN_PRD: ${{ secrets.LH_SERVER_TOKEN_PRD }}

.github/workflows/cd.deploy.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,14 @@ jobs:
4949
run: npm run generate:sentry-release $DEPLOY_VERSION production ${{ secrets.SENTRY_AUTH_TOKEN }}
5050
- name: "Deploy"
5151
run: npm run deploy
52+
53+
lighthouse:
54+
needs: bundle-deploy
55+
uses: ./.github/workflows/ci.reusable.lighthouse.yml
56+
with:
57+
serverBaseUrl: "https://lh.dzcode.io"
58+
testBaseUrl: "https://www.dzcode.io"
59+
stage: "production"
60+
secrets:
61+
LH_SERVER_TOKEN_STG: ${{ secrets.LH_SERVER_TOKEN_STG }}
62+
LH_SERVER_TOKEN_PRD: ${{ secrets.LH_SERVER_TOKEN_PRD }}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
on:
2+
workflow_call:
3+
inputs:
4+
testBaseUrl:
5+
required: true
6+
type: string
7+
serverBaseUrl:
8+
required: true
9+
type: string
10+
stage:
11+
required: true
12+
type: string
13+
description: "staging or production"
14+
secrets:
15+
LH_SERVER_TOKEN_STG:
16+
required: true
17+
LH_SERVER_TOKEN_PRD:
18+
required: true
19+
20+
jobs:
21+
urls-for-lighthouse:
22+
runs-on: ubuntu-latest
23+
env:
24+
LH_TEST_BASE_URL: ${{ inputs.testBaseUrl }}
25+
steps:
26+
- name: "Git"
27+
uses: actions/checkout@v4
28+
- name: "Nodejs"
29+
uses: actions/setup-node@v4
30+
with:
31+
node-version: "20"
32+
cache: "npm"
33+
- run: npm ci
34+
- name: Download artifact
35+
uses: actions/download-artifact@v4
36+
with:
37+
name: build output (ubuntu-latest, 20)
38+
- name: Define Urls
39+
id: define-urls
40+
run: |
41+
node web/lighthouserc.cjs --output-urls >> "$GITHUB_OUTPUT"
42+
outputs:
43+
urls: ${{ steps.define-urls.outputs.urls }}
44+
45+
lighthouse-collect:
46+
needs: urls-for-lighthouse
47+
runs-on: ubuntu-latest
48+
strategy:
49+
matrix:
50+
url: ${{fromJson(needs.urls-for-lighthouse.outputs.urls)}}
51+
env:
52+
LH_TEST_BASE_URL: ${{ inputs.testBaseUrl }}
53+
LH_OVERWRITE_URL: ${{ matrix.url }}
54+
55+
steps:
56+
- name: "Git"
57+
uses: actions/checkout@v4
58+
- name: "Nodejs"
59+
uses: actions/setup-node@v4
60+
with:
61+
node-version: "20"
62+
cache: "npm"
63+
- run: npm ci
64+
- name: Download artifact
65+
uses: actions/download-artifact@v4
66+
with:
67+
name: build output (ubuntu-latest, 20)
68+
- run: npx lerna run lh:collect --scope "@dzcode.io/web"
69+
- name: Upload artifact
70+
uses: actions/upload-artifact@v4
71+
with:
72+
name: lighthouse output (${{ hashFiles('web/.lighthouseci/**') }})
73+
path: ./web/.lighthouseci/**
74+
if-no-files-found: error
75+
include-hidden-files: true
76+
retention-days: 1
77+
78+
lighthouse-upload:
79+
needs: lighthouse-collect
80+
runs-on: ubuntu-latest
81+
env:
82+
LH_SERVER_BASE_URL: ${{ inputs.serverBaseUrl }}
83+
LH_SERVER_TOKEN: ${{ inputs.stage == 'staging' && secrets.LH_SERVER_TOKEN_STG || secrets.LH_SERVER_TOKEN_PRD }}
84+
steps:
85+
- name: "Git"
86+
uses: actions/checkout@v4
87+
- name: "Nodejs"
88+
uses: actions/setup-node@v4
89+
with:
90+
node-version: "20"
91+
cache: "npm"
92+
- run: npm ci
93+
- name: Download artifact
94+
uses: actions/download-artifact@v4
95+
with:
96+
name: build output (ubuntu-latest, 20)
97+
- name: Download artifact
98+
uses: actions/download-artifact@v4
99+
with:
100+
pattern: lighthouse*
101+
merge-multiple: true
102+
path: ./web/.lighthouseci
103+
- run: npx lerna run lh:upload --scope "@dzcode.io/web"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ src/_e2e-test/videos
2929
src/_e2e-test/screenshots
3030
cloudflare/public
3131
**/cloudflare/public
32+
**.lighthouseci
3233

3334
# mobile
3435
mobile/.expo

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The code for [dzcode.io](https://dzcode.io), a website for Algerian open-source
77
**Apps:**
88

99
- [`./web`](./web) ( [dzcode.io](https://dzcode.io) or [stage.dzcode.io](https://stage.dzcode.io) )
10+
- lighthouse results are in: [lh.dzcode.io](https://lh.dzcode.io) and [lh-stage.dzcode.io](https://lh-stage.dzcode.io)
1011
- [`./api`](./api) ( [api.dzcode.io](https://api.dzcode.io) or [api-stage.dzcode.io](https://api-stage.dzcode.io) )
1112

1213
**Packages**
@@ -112,4 +113,4 @@ Follow [these steps](https://github.com/dzcode-io/dzcode.io/blob/main/data/model
112113

113114
## License
114115

115-
Copyright (c) 2021 DzCode i/o (twitter: [@dzcode_io](https://twitter.com/dzcode_io)) Licensed under the MIT license.
116+
Licensed under the MIT license (twitter: [@dzcode_io](https://twitter.com/dzcode_io)).

api/oracle-cloud/deploy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// can be ran locally from ./api:
2-
// SSH_ADDRESS_STG="root@x.x.x.x" SSH_PATH="path/to/private/ssh/key" npm run deploy:stg
2+
// SSH_ADDRESS_STG="user@x.x.x.x" SSH_PATH="path/to/private/ssh/key" npm run deploy:stg
33

44
import { execSync } from "child_process";
55
import { copySync, existsSync } from "fs-extra";
@@ -29,6 +29,7 @@ const fromToRecords = dependencies
2929
fromToRecords.push(
3030
{ from: "./oracle-cloud/docker-compose.yml", to: "./oracle-cloud/build/docker-compose.yml" },
3131
{ from: "./oracle-cloud/Dockerfile", to: "./oracle-cloud/build/Dockerfile" },
32+
{ from: "./oracle-cloud/nginx.conf", to: "./oracle-cloud/build/nginx.conf" },
3233
{ from: join(workspaceRoot, "package.json"), to: "./oracle-cloud/build/package.json" },
3334
{ from: join(workspaceRoot, "package-lock.json"), to: "./oracle-cloud/build/package-lock.json" },
3435
);

api/oracle-cloud/docker-compose.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
version: "3"
22
services:
3+
reverse-proxy:
4+
image: nginx
5+
ports:
6+
- "80:80"
7+
depends_on:
8+
- api
9+
- lhserver
10+
volumes:
11+
- ./nginx.conf:/etc/nginx/nginx.conf
312
api:
413
build: "."
514
# restart: always
615
ports:
7-
- "80:7070"
16+
- "7070:7070"
817
env_file:
918
- /home/ubuntu/app-env/api.env
1019
volumes:
1120
- /home/ubuntu/app-data/api/fetch_cache:/usr/src/repo/api/fetch_cache
1221
- /home/ubuntu/app-data/api/sqlite_db:/usr/src/repo/api/sqlite_db
22+
lhserver:
23+
image: patrickhulce/lhci-server
24+
ports:
25+
- "9001:9001"
26+
volumes:
27+
- /home/ubuntu/app-data/web/lh_data:/data

api/oracle-cloud/nginx.conf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
events {
2+
}
3+
4+
5+
http {
6+
server {
7+
client_max_body_size 10M;
8+
listen 80;
9+
# redirect traffic from lh-stage.dzcode.io, lh_stage.dzcode.io, lh.dzcode.io to lhserver:9001
10+
# and from api-stage.dzcode.io, api_stage.dzcode.io, api.dzcode.io to api:7070
11+
location / {
12+
if ($host ~* ^lh-stage.dzcode.io$) {
13+
proxy_pass http://lhserver:9001;
14+
}
15+
if ($host ~* ^lh_stage.dzcode.io$) {
16+
proxy_pass http://lhserver:9001;
17+
}
18+
if ($host ~* ^lh.dzcode.io$) {
19+
proxy_pass http://lhserver:9001;
20+
}
21+
if ($host ~* ^api-stage.dzcode.io$) {
22+
proxy_pass http://api:7070;
23+
}
24+
if ($host ~* ^api_stage.dzcode.io$) {
25+
proxy_pass http://api:7070;
26+
}
27+
if ($host ~* ^api.dzcode.io$) {
28+
proxy_pass http://api:7070;
29+
}
30+
}
31+
}
32+
}

package-lock.json

Lines changed: 30 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/tooling/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ nodemon.json
3131
*.ts-prunerc
3232
*.patch
3333
*.sql
34+
*.conf

web/lighthouserc.cjs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// eslint-disable-next-line @typescript-eslint/no-require-imports
2+
const { allLanguages } = require("@dzcode.io/models/dist/language");
3+
4+
const baseUrl = process.env.LH_TEST_BASE_URL;
5+
const serverBaseUrl = process.env.LH_SERVER_BASE_URL;
6+
const token = process.env.LH_SERVER_TOKEN;
7+
const overwriteURL = process.env.LH_OVERWRITE_URL;
8+
9+
let urls = ["", "/contribute", "/team", "/projects", "/faq"];
10+
11+
const args = process.argv.slice(2);
12+
if (args.includes("--output-urls")) {
13+
console.log(`urls=${JSON.stringify(urls)}`);
14+
} else if (typeof overwriteURL !== "undefined") {
15+
urls = [overwriteURL];
16+
}
17+
18+
module.exports = {
19+
ci: {
20+
collect: {
21+
url: urls.reduce((acc, path) => {
22+
return acc.concat(
23+
allLanguages.map(({ code }) => `${baseUrl}${code === "en" ? "" : `/${code}`}${path}`),
24+
);
25+
}, []),
26+
},
27+
upload: {
28+
target: "lhci",
29+
serverBaseUrl,
30+
token,
31+
},
32+
},
33+
};

web/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
"start:dev": "rsbuild dev --open",
8080
"test": "npm run build && npm run test:alone",
8181
"test:alone": "jest --config ../packages/tooling/jest.config.ts --rootDir .",
82-
"test:watch": "npm-run-all build --parallel build:watch \"test:alone --watch {@}\" --"
82+
"test:watch": "npm-run-all build --parallel build:watch \"test:alone --watch {@}\" --",
83+
"lh:collect": "npx --yes @lhci/cli collect",
84+
"lh:upload": "npx --yes @lhci/cli upload"
8385
}
8486
}

0 commit comments

Comments
 (0)